A platform for community discussion. Free, open, simple.
FEATURE: allow re-scoping chat user search via a plugin (#26361)
Same3a0faefc5a4
diff --git a/app/models/user.rb b/app/models/user.rbindex 7283a46d608..2e58aacc053 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -259,12 +259,19 @@ class User < ActiveRecord::Base)end- scope :human_users, -> { where("users.id > 0") }+ scope :human_users,+ ->(allowed_bot_user_ids: nil) do+ if allowed_bot_user_ids.present?+ where("users.id > 0 OR users.id IN (?)", allowed_bot_user_ids)+ else+ where("users.id > 0")+ end+ end# excluding fake users like the system user or anonymous usersscope :real,- -> do- human_users.where(+ ->(allowed_bot_user_ids: nil) do+ human_users(allowed_bot_user_ids: allowed_bot_user_ids).where("NOT EXISTS(SELECT 1FROM anonymous_users adiff --git a/plugins/chat/app/services/chat/search_chatable.rb b/plugins/chat/app/services/chat/search_chatable.rbindex b7c24d40901..e67a131890e 100644--- a/plugins/chat/app/services/chat/search_chatable.rb+++ b/plugins/chat/app/services/chat/search_chatable.rb@@ -98,11 +98,17 @@ module Chatuser_search = ::UserSearch.new(context.term, limit: 10)if context.term.blank?- user_search = user_search.scoped_users.real.includes(:user_option)+ user_search = user_search.scoped_userselse- user_search = user_search.search.real.includes(:user_option)+ user_search = user_search.searchend+ allowed_bot_user_ids =+ DiscoursePluginRegistry.apply_modifier(:chat_allowed_bot_user_ids, [], guardian)++ user_search = user_search.real(allowed_bot_user_ids: allowed_bot_user_ids)+ user_search = user_search.includes(:user_option)+if context.excluded_memberships_channel_iduser_search =user_search.where(diff --git a/plugins/chat/spec/services/chat/search_chatable_spec.rb b/plugins/chat/spec/services/chat/search_chatable_spec.rbindex f6431c3bd2d..116d9d50fe5 100644--- a/plugins/chat/spec/services/chat/search_chatable_spec.rb+++ b/plugins/chat/spec/services/chat/search_chatable_spec.rb@@ -82,6 +82,27 @@ RSpec.describe Chat::SearchChatable doexpect(result.users).to_not include(current_user)end++ context "when chat_allowed_bot_user_ids modifier exists" do+ fab!(:bot_1) { Fabricate(:user, id: -500) }+ fab!(:bot_2) { Fabricate(:user, id: -501) }++ it "alters the users returned" do+ modifier_block = Proc.new { [bot_2.id] }+ plugin_instance = Plugin::Instance.new+ plugin_instance.register_modifier(:chat_allowed_bot_user_ids, &modifier_block)++ expect(result.users).to_not include(bot_1)+ expect(result.users).to include(bot_2)+ expect(result.users).to include(current_user, sam, charlie, alain)+ ensure+ DiscoursePluginRegistry.unregister_modifier(+ plugin_instance,+ :chat_allowed_bot_user_ids,+ &modifier_block+ )+ end+ endendcontext "when not including users" dodiff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex a6b569ea041..897a42df5ee 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -1781,6 +1781,17 @@ RSpec.describe User doendend+ describe "real users" do+ it "should find system user if you allow it" do+ ids =+ User+ .real(allowed_bot_user_ids: [Discourse.system_user.id])+ .where(id: Discourse.system_user.id)+ .pluck(:id)+ expect(ids).to eq([Discourse.system_user.id])+ end+ end+describe "#purge_unactivated" dofab!(:user) { Fabricate(:user, refresh_auto_groups: true) }fab!(:unactivated) { Fabricate(:user, active: false) }