DEV: enforces system user membership (#23771)

System user will automatically adds itself to the channel when trying to send a message.
This commit is contained in:
Joffrey JAFFEUX 2023-10-04 17:03:23 +02:00 committed by GitHub
parent 08df8fc1d1
commit 8447928840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -24,6 +24,7 @@ module Chat
model :channel
policy :allowed_to_join_channel
policy :allowed_to_create_message_in_channel, class_name: Chat::Channel::MessageCreationPolicy
step :enforce_system_membership
model :channel_membership
model :reply, optional: true
policy :ensure_reply_consistency
@ -72,6 +73,10 @@ module Chat
guardian.can_join_chat_channel?(channel)
end
def enforce_system_membership(guardian:, channel:, **)
channel.add(guardian.user) if guardian.user.is_system_user?
end
def fetch_channel_membership(guardian:, channel:, **)
Chat::ChannelMembershipManager.new(channel).find_for_user(guardian.user)
end

View File

@ -183,6 +183,12 @@ RSpec.describe Chat::CreateMessage do
it { is_expected.to fail_a_policy(:allowed_to_join_channel) }
end
context "when user is system" do
fab!(:user) { Discourse.system_user }
it { is_expected.to be_a_success }
end
context "when user can join channel" do
before { user.groups << Group.find(Group::AUTO_GROUPS[:trust_level_1]) }