mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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 ```
This commit is contained in:
parent
9e08b45f9b
commit
77028e3675
@ -22,9 +22,9 @@ module Chat
|
|||||||
# @option params_to_create [Array<String>] target_groups
|
# @option params_to_create [Array<String>] target_groups
|
||||||
# @return [Service::Base::Context]
|
# @return [Service::Base::Context]
|
||||||
|
|
||||||
policy :can_create_direct_message
|
|
||||||
contract
|
contract
|
||||||
model :target_users
|
model :target_users
|
||||||
|
policy :can_create_direct_message
|
||||||
policy :satisfies_dms_max_users_limit,
|
policy :satisfies_dms_max_users_limit,
|
||||||
class_name: Chat::DirectMessageChannel::MaxUsersExcessPolicy
|
class_name: Chat::DirectMessageChannel::MaxUsersExcessPolicy
|
||||||
model :user_comm_screener
|
model :user_comm_screener
|
||||||
@ -53,8 +53,13 @@ module Chat
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def can_create_direct_message(guardian:, **)
|
def can_create_direct_message(guardian:, target_users:, **)
|
||||||
guardian.can_create_direct_message?
|
guardian.can_create_direct_message? &&
|
||||||
|
DiscoursePluginRegistry.apply_modifier(
|
||||||
|
:chat_can_create_direct_message_channel,
|
||||||
|
guardian.user,
|
||||||
|
target_users,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_target_users(guardian:, contract:, **)
|
def fetch_target_users(guardian:, contract:, **)
|
||||||
|
@ -178,6 +178,22 @@ RSpec.describe Chat::CreateDirectMessageChannel do
|
|||||||
it { is_expected.to fail_a_policy(:can_create_direct_message) }
|
it { is_expected.to fail_a_policy(:can_create_direct_message) }
|
||||||
end
|
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
|
context "when the actor is not allowing anyone to message them" do
|
||||||
before { current_user.user_option.update!(allow_private_messages: false) }
|
before { current_user.user_option.update!(allow_private_messages: false) }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user