mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 02:29:31 +08:00
FEATURE: search for all posts of users that have a badge
with_badge:"badge name" will allow you to search only posts of users with a particular badge
This commit is contained in:
parent
38adb6b6fb
commit
17d999ab12
|
@ -103,6 +103,7 @@ class Search
|
||||||
@limit = Search.per_facet
|
@limit = Search.per_facet
|
||||||
|
|
||||||
term = process_advanced_search!(term)
|
term = process_advanced_search!(term)
|
||||||
|
|
||||||
if term.present?
|
if term.present?
|
||||||
@term = Search.prepare_data(term.to_s)
|
@term = Search.prepare_data(term.to_s)
|
||||||
@original_term = PG::Connection.escape_string(@term)
|
@original_term = PG::Connection.escape_string(@term)
|
||||||
|
@ -184,6 +185,15 @@ class Search
|
||||||
posts.where("posts.post_number = 1")
|
posts.where("posts.post_number = 1")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
advanced_filter(/with_badge:(.*)/) do |posts,match|
|
||||||
|
badge_id = Badge.where('name ilike ? OR id = ?', match, match.to_i).pluck(:id).first
|
||||||
|
if badge_id
|
||||||
|
posts.where('posts.user_id IN (SELECT ub.user_id FROM user_badges ub WHERE ub.badge_id = ?)', badge_id)
|
||||||
|
else
|
||||||
|
posts.where("1 = 0")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
advanced_filter(/in:(likes|bookmarks)/) do |posts, match|
|
advanced_filter(/in:(likes|bookmarks)/) do |posts, match|
|
||||||
if @guardian.user
|
if @guardian.user
|
||||||
post_action_type = PostActionType.types[:like] if match == "likes"
|
post_action_type = PostActionType.types[:like] if match == "likes"
|
||||||
|
@ -247,12 +257,14 @@ class Search
|
||||||
|
|
||||||
def process_advanced_search!(term)
|
def process_advanced_search!(term)
|
||||||
|
|
||||||
term.to_s.split(/\s+/).map do |word|
|
term.to_s.scan(/(([^" \t\n\x0B\f\r]+)?(("[^"]+")?))/).to_a.map do |(word,_)|
|
||||||
|
next if word.blank?
|
||||||
|
|
||||||
found = false
|
found = false
|
||||||
|
|
||||||
Search.advanced_filters.each do |matcher, block|
|
Search.advanced_filters.each do |matcher, block|
|
||||||
if word =~ matcher
|
cleaned = word.gsub(/["']/,"")
|
||||||
|
if cleaned =~ matcher
|
||||||
(@filters ||= []) << [block, $1]
|
(@filters ||= []) << [block, $1]
|
||||||
found = true
|
found = true
|
||||||
end
|
end
|
||||||
|
@ -273,6 +285,9 @@ class Search
|
||||||
elsif word == 'order:views'
|
elsif word == 'order:views'
|
||||||
@order = :views
|
@order = :views
|
||||||
nil
|
nil
|
||||||
|
elsif word == 'order:likes'
|
||||||
|
@order = :likes
|
||||||
|
nil
|
||||||
elsif word == 'in:private'
|
elsif word == 'in:private'
|
||||||
@search_pms = true
|
@search_pms = true
|
||||||
nil
|
nil
|
||||||
|
@ -440,6 +455,12 @@ class Search
|
||||||
else
|
else
|
||||||
posts = posts.order("topics.views DESC")
|
posts = posts.order("topics.views DESC")
|
||||||
end
|
end
|
||||||
|
elsif @order == :likes
|
||||||
|
if opts[:aggregate_search]
|
||||||
|
posts = posts.order("MAX(posts.like_count) DESC")
|
||||||
|
else
|
||||||
|
posts = posts.order("posts.like_count DESC")
|
||||||
|
end
|
||||||
else
|
else
|
||||||
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
|
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
|
||||||
|
|
||||||
|
|
|
@ -391,6 +391,17 @@ describe Search do
|
||||||
expect(Search.execute("user:#{_post.user.username}").posts.length).to eq(1)
|
expect(Search.execute("user:#{_post.user.username}").posts.length).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'supports with_badge' do
|
||||||
|
|
||||||
|
topic = Fabricate(:topic, created_at: 3.months.ago)
|
||||||
|
post = Fabricate(:post, raw: 'hi this is a test 123 123', topic: topic)
|
||||||
|
|
||||||
|
badge = Badge.create!(name: "Like a Boss", badge_type_id: 1)
|
||||||
|
UserBadge.create!(user_id: post.user_id, badge_id: badge.id, granted_at: 1.minute.ago, granted_by_id: -1)
|
||||||
|
|
||||||
|
expect(Search.execute('with_badge:"like a boss"').posts.length).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
it 'can search numbers correctly, and match exact phrases' do
|
it 'can search numbers correctly, and match exact phrases' do
|
||||||
topic = Fabricate(:topic, created_at: 3.months.ago)
|
topic = Fabricate(:topic, created_at: 3.months.ago)
|
||||||
Fabricate(:post, raw: '3.0 eta is in 2 days horrah', topic: topic)
|
Fabricate(:post, raw: '3.0 eta is in 2 days horrah', topic: topic)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user