FEATURE: in:likes and in:posted search filters

This commit is contained in:
Sam 2014-10-18 15:19:08 +11:00
parent cbc132eca9
commit 8afd7a7f21
3 changed files with 36 additions and 1 deletions

View File

@ -2131,5 +2131,8 @@ en:
`status:archived` Only show archived topics `status:archived` Only show archived topics
`status:noreplies` Show posts with a single participant `status:noreplies` Show posts with a single participant
`category:foo` Only show topics in the category `foo` `category:foo` Only show topics in the category `foo`
`user:foo` Only show posts by the user `foo`
`in:likes` Only show posts you liked
`in:posted` Only show posts you created
Example: `bears category:test status:open order:latest` will search for topics in the category bears that are not closed or archived ordered by last post date. Example: `bears category:test status:open order:latest` will search for topics in the category bears that are not closed or archived ordered by last post date.

View File

@ -162,6 +162,15 @@ class Search
elsif word =~ /category:(.+)/ elsif word =~ /category:(.+)/
@category_id = Category.find_by('name ilike ?', $1).try(:id) @category_id = Category.find_by('name ilike ?', $1).try(:id)
nil nil
elsif word =~ /user:(.+)/
@user_id = User.find_by('username_lower = ?', $1.downcase).try(:id)
nil
elsif word == 'in:likes'
@liked_only = true
nil
elsif word == 'in:posted'
@posted_only = true
nil
else else
word word
end end
@ -280,6 +289,25 @@ class Search
posts = posts.where("topics.featured_user1_id IS NULL AND topics.last_post_user_id = topics.user_id") posts = posts.where("topics.featured_user1_id IS NULL AND topics.last_post_user_id = topics.user_id")
end end
if @user_id
posts = posts.where("posts.user_id = #{@user_id}")
end
if @guardian.user
if @liked_only
posts = posts.where("posts.id IN (
SELECT pa.post_id FROM post_actions pa
WHERE pa.user_id = #{@guardian.user.id} AND
pa.post_action_type_id = #{PostActionType.types[:like]}
)")
end
if @posted_only
posts = posts.where("posts.user_id = #{@guardian.user.id}")
end
end
# If we have a search context, prioritize those posts first # If we have a search context, prioritize those posts first
if @search_context.present? if @search_context.present?

View File

@ -170,7 +170,7 @@ describe Search do
context 'security' do context 'security' do
def result(current_user) def result(current_user)
Search.execute('hello', guardian: current_user) Search.execute('hello', guardian: Guardian.new(current_user))
end end
it 'secures results correctly' do it 'secures results correctly' do
@ -324,6 +324,10 @@ describe Search do
Search.execute('test status:noreplies').posts.length.should == 1 Search.execute('test status:noreplies').posts.length.should == 1
Search.execute('test in:likes', guardian: Guardian.new(topic.user)).posts.length.should == 0
Search.execute('test in:posted', guardian: Guardian.new(topic.user)).posts.length.should == 1
end end
it 'can find by latest' do it 'can find by latest' do