From 77028e3675c4a13475feffd9df996599257b985e Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 23 Feb 2024 14:35:02 +0100 Subject: [PATCH] DEV: adds a `chat_can_create_direct_message_channel` modifier (#25840) Plugins can now register this modifier: ```ruby register_modifier(:chat_can_create_direct_message_channel) do |user, target_users| # your logic which should return true or false end ``` --- .../chat/create_direct_message_channel.rb | 11 ++++++++--- .../chat/create_direct_message_channel_spec.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/plugins/chat/app/services/chat/create_direct_message_channel.rb b/plugins/chat/app/services/chat/create_direct_message_channel.rb index d211904067b..d99fd0cb996 100644 --- a/plugins/chat/app/services/chat/create_direct_message_channel.rb +++ b/plugins/chat/app/services/chat/create_direct_message_channel.rb @@ -22,9 +22,9 @@ module Chat # @option params_to_create [Array] target_groups # @return [Service::Base::Context] - policy :can_create_direct_message contract model :target_users + policy :can_create_direct_message policy :satisfies_dms_max_users_limit, class_name: Chat::DirectMessageChannel::MaxUsersExcessPolicy model :user_comm_screener @@ -53,8 +53,13 @@ module Chat private - def can_create_direct_message(guardian:, **) - guardian.can_create_direct_message? + def can_create_direct_message(guardian:, target_users:, **) + guardian.can_create_direct_message? && + DiscoursePluginRegistry.apply_modifier( + :chat_can_create_direct_message_channel, + guardian.user, + target_users, + ) end def fetch_target_users(guardian:, contract:, **) diff --git a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb index eb8f2d35c1a..f41a8bb25d1 100644 --- a/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb +++ b/plugins/chat/spec/services/chat/create_direct_message_channel_spec.rb @@ -178,6 +178,22 @@ RSpec.describe Chat::CreateDirectMessageChannel do it { is_expected.to fail_a_policy(:can_create_direct_message) } end + context "when the plugin modifier returns false" do + it "fails a policy" do + modifier_block = Proc.new { false } + plugin_instance = Plugin::Instance.new + plugin_instance.register_modifier(:chat_can_create_direct_message_channel, &modifier_block) + + expect(result).to fail_a_policy(:can_create_direct_message) + ensure + DiscoursePluginRegistry.unregister_modifier( + plugin_instance, + :chat_can_create_direct_message_channel, + &modifier_block + ) + end + end + context "when the actor is not allowing anyone to message them" do before { current_user.user_option.update!(allow_private_messages: false) }