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).
This commit is contained in:
parent
d070b0791b
commit
a024e26d2d
48
plugin.rb
48
plugin.rb
|
@ -887,39 +887,49 @@ after_initialize do
|
||||||
Assignment.active_for_group(group).destroy_all
|
Assignment.active_for_group(group).destroy_all
|
||||||
end
|
end
|
||||||
|
|
||||||
register_search_advanced_filter(/in:assigned/) do |posts|
|
add_filter_custom_filter("assigned") do |scope, filter_values|
|
||||||
posts.where(<<~SQL) if @guardian.can_assign?
|
user_or_group_name = filter_values.compact.first
|
||||||
topics.id IN (
|
|
||||||
SELECT a.topic_id FROM assignments a WHERE a.active
|
return scope if user_or_group_name.blank?
|
||||||
)
|
|
||||||
|
if user_id = User.find_by_username(user_or_group_name)&.id
|
||||||
|
scope.where(<<~SQL, user_id)
|
||||||
|
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active)
|
||||||
SQL
|
SQL
|
||||||
|
elsif group_id = Group.find_by(name: user_or_group_name)&.id
|
||||||
|
scope.where(<<~SQL, group_id)
|
||||||
|
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active)
|
||||||
|
SQL
|
||||||
|
else
|
||||||
|
scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
register_search_advanced_filter(/in:assigned/) do |posts|
|
||||||
|
return if !@guardian.can_assign?
|
||||||
|
|
||||||
|
posts.where("topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.active)")
|
||||||
end
|
end
|
||||||
|
|
||||||
register_search_advanced_filter(/in:unassigned/) do |posts|
|
register_search_advanced_filter(/in:unassigned/) do |posts|
|
||||||
posts.where(<<~SQL) if @guardian.can_assign?
|
return if !@guardian.can_assign?
|
||||||
topics.id NOT IN (
|
|
||||||
SELECT a.topic_id FROM assignments a WHERE a.active
|
posts.where("topics.id NOT IN (SELECT a.topic_id FROM assignments a WHERE a.active)")
|
||||||
)
|
|
||||||
SQL
|
|
||||||
end
|
end
|
||||||
|
|
||||||
register_search_advanced_filter(/assigned:(.+)$/) do |posts, match|
|
register_search_advanced_filter(/assigned:(.+)$/) do |posts, match|
|
||||||
if @guardian.can_assign?
|
return if !@guardian.can_assign? || match.blank?
|
||||||
|
|
||||||
if user_id = User.find_by_username(match)&.id
|
if user_id = User.find_by_username(match)&.id
|
||||||
posts.where(<<~SQL, user_id)
|
posts.where(<<~SQL, user_id)
|
||||||
topics.id IN (
|
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active)
|
||||||
SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'User' AND a.active
|
|
||||||
)
|
|
||||||
SQL
|
SQL
|
||||||
elsif group_id = Group.find_by_name(match)&.id
|
elsif group_id = Group.find_by(name: match)&.id
|
||||||
posts.where(<<~SQL, group_id)
|
posts.where(<<~SQL, group_id)
|
||||||
topics.id IN (
|
topics.id IN (SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active)
|
||||||
SELECT a.topic_id FROM assignments a WHERE a.assigned_to_id = ? AND a.assigned_to_type = 'Group' AND a.active
|
|
||||||
)
|
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if defined?(DiscourseAutomation)
|
if defined?(DiscourseAutomation)
|
||||||
add_automation_scriptable("random_assign") do
|
add_automation_scriptable("random_assign") do
|
||||||
|
|
|
@ -355,4 +355,42 @@ describe ListController do
|
||||||
expect(JSON.parse(response.body)["topic_list"]["topics"].map { |t| t["id"] }).to eq([pm.id])
|
expect(JSON.parse(response.body)["topic_list"]["topics"].map { |t| t["id"] }).to eq([pm.id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#filter" do
|
||||||
|
include_context "with group that is allowed to assign"
|
||||||
|
|
||||||
|
fab!(:group) { Fabricate(:group, assignable_level: Group::ALIAS_LEVELS[:mods_and_admins]) }
|
||||||
|
|
||||||
|
fab!(:topic_1) { Fabricate(:topic) }
|
||||||
|
fab!(:topic_2) { Fabricate(:topic) }
|
||||||
|
fab!(:topic_3) { Fabricate(:topic) }
|
||||||
|
|
||||||
|
fab!(:post_1) { Fabricate(:post, topic: topic_1) }
|
||||||
|
fab!(:post_2) { Fabricate(:post, topic: topic_2) }
|
||||||
|
fab!(:post_3) { Fabricate(:post, topic: topic_3) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
sign_in(admin)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "filters topics by assigned user" do
|
||||||
|
add_to_assign_allowed_group(user)
|
||||||
|
|
||||||
|
Assigner.new(topic_1, admin).assign(user)
|
||||||
|
|
||||||
|
get "/filter", params: { q: "assigned:#{user.username_lower}", format: :json }
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(response.parsed_body.dig("topic_list", "topics").map { _1["id"] }).to contain_exactly(topic_1.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "filters topics by assigned group" do
|
||||||
|
Assigner.new(topic_2, admin).assign(group)
|
||||||
|
|
||||||
|
get "/filter", params: { q: "assigned:#{group.name}", format: :json }
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(response.parsed_body.dig("topic_list", "topics").map { _1["id"] }).to contain_exactly(topic_2.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue