Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit a600428

Browse files
authored
FEATURE: add "assigned:<name>" filter (in the /filter page) (#648)
* FEATURE: add "assigned:<name>" filter (in the /filter page) The "assigned:<name>" filter was already available in the advanced search but it wasn't in the /filter page. This commit adds that filter allowing anyone to filter topics assigned to either a specific user (using their username) or to a specific group (using its name). * DEV: use argument instead of instance variable * SPEC: add spec for when the user can't assign
1 parent d070b07 commit a600428

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

plugin.rb

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -887,37 +887,47 @@ module ::DiscourseAssign
887887
Assignment.active_for_group(group).destroy_all
888888
end
889889

890-
register_search_advanced_filter(/in:assigned/) do |posts|
891-
posts.where(<<~SQL) if @guardian.can_assign?
892-
topics.id IN (
893-
SELECT a.topic_id FROM assignments a WHERE a.active
894-
)
890+
add_filter_custom_filter("assigned") do |scope, filter_values, guardian|
891+
next if !guardian.can_assign? || filter_values.blank?
892+
893+
user_or_group_name = filter_values.compact.first
894+
895+
next if user_or_group_name.blank?
896+
897+
if user_id = User.find_by_username(user_or_group_name)&.id
898+
scope.where(<<~SQL, user_id)
899+
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active)
900+
SQL
901+
elsif group_id = Group.find_by(name: user_or_group_name)&.id
902+
scope.where(<<~SQL, group_id)
903+
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active)
895904
SQL
905+
end
906+
end
907+
908+
register_search_advanced_filter(/in:assigned/) do |posts|
909+
next if !@guardian.can_assign?
910+
911+
posts.where("topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.active)")
896912
end
897913

898914
register_search_advanced_filter(/in:unassigned/) do |posts|
899-
posts.where(<<~SQL) if @guardian.can_assign?
900-
topics.id NOT IN (
901-
SELECT a.topic_id FROM assignments a WHERE a.active
902-
)
903-
SQL
915+
next if !@guardian.can_assign?
916+
917+
posts.where("topics.id NOT IN (SELECT a.topic_id FROM assignments a WHERE a.active)")
904918
end
905919

906920
register_search_advanced_filter(/assigned:(.+)$/) do |posts, match|
907-
if @guardian.can_assign?
908-
if user_id = User.find_by_username(match)&.id
909-
posts.where(<<~SQL, user_id)
910-
topics.id IN (
911-
SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active
912-
)
913-
SQL
914-
elsif group_id = Group.find_by_name(match)&.id
915-
posts.where(<<~SQL, group_id)
916-
topics.id IN (
917-
SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active
918-
)
919-
SQL
920-
end
921+
next if !@guardian.can_assign? || match.blank?
922+
923+
if user_id = User.find_by_username(match)&.id
924+
posts.where(<<~SQL, user_id)
925+
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active)
926+
SQL
927+
elsif group_id = Group.find_by(name: match)&.id
928+
posts.where(<<~SQL, group_id)
929+
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active)
930+
SQL
921931
end
922932
end
923933

spec/requests/list_controller_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,61 @@
355355
expect(JSON.parse(response.body)["topic_list"]["topics"].map { |t| t["id"] }).to eq([pm.id])
356356
end
357357
end
358+
359+
describe "#filter" do
360+
include_context "with group that is allowed to assign"
361+
362+
fab!(:group) { Fabricate(:group, assignable_level: Group::ALIAS_LEVELS[:mods_and_admins]) }
363+
364+
fab!(:topic_1) { Fabricate(:topic) }
365+
fab!(:topic_2) { Fabricate(:topic) }
366+
fab!(:topic_3) { Fabricate(:topic) }
367+
368+
fab!(:post_1) { Fabricate(:post, topic: topic_1) }
369+
fab!(:post_2) { Fabricate(:post, topic: topic_2) }
370+
fab!(:post_3) { Fabricate(:post, topic: topic_3) }
371+
372+
describe "when user cannot assign" do
373+
it "ignores the assign filter" do
374+
add_to_assign_allowed_group(user)
375+
376+
Assigner.new(topic_1, user).assign(user)
377+
378+
get "/filter", params: { q: "assigned:#{user.username_lower}", format: :json }
379+
380+
expect(response.status).to eq(200)
381+
expect(
382+
response.parsed_body.dig("topic_list", "topics").map { _1["id"] },
383+
).to contain_exactly(topic_1.id, topic_2.id, topic_3.id)
384+
end
385+
end
386+
387+
describe "when user can assign" do
388+
before { sign_in(admin) }
389+
390+
it "filters topics by assigned user" do
391+
add_to_assign_allowed_group(user)
392+
393+
Assigner.new(topic_1, admin).assign(user)
394+
395+
get "/filter", params: { q: "assigned:#{user.username_lower}", format: :json }
396+
397+
expect(response.status).to eq(200)
398+
expect(
399+
response.parsed_body.dig("topic_list", "topics").map { _1["id"] },
400+
).to contain_exactly(topic_1.id)
401+
end
402+
403+
it "filters topics by assigned group" do
404+
Assigner.new(topic_2, admin).assign(group)
405+
406+
get "/filter", params: { q: "assigned:#{group.name}", format: :json }
407+
408+
expect(response.status).to eq(200)
409+
expect(
410+
response.parsed_body.dig("topic_list", "topics").map { _1["id"] },
411+
).to contain_exactly(topic_2.id)
412+
end
413+
end
414+
end
358415
end

0 commit comments

Comments
 (0)