Skip to content

A platform for community discussion. Free, open, simple.

FIX: Skip users who mute or ignore the inviter in chat channel invites [pt-3a6286331ff83159f0b9] (#44398)

Gabriel Grubbacfc6e9200b6d
diff --git a/plugins/chat/app/services/chat/invite_users_to_channel.rb b/plugins/chat/app/services/chat/invite_users_to_channel.rb
index 5edcc1db798..9d1d933f807 100644
--- a/plugins/chat/app/services/chat/invite_users_to_channel.rb
+++ b/plugins/chat/app/services/chat/invite_users_to_channel.rb
@@ -29,6 +29,7 @@ module Chat
model :channel
policy :can_join_channel
model :users, optional: true
+ model :user_comm_screener
step :send_invite_notifications
private
@@ -47,11 +48,17 @@ module Chat
.where(user_options: { chat_enabled: true })
.not_suspended
.where(id: params.user_ids)
+ .order(:id)
.limit(50)
end
- def send_invite_notifications(channel:, guardian:, users:, params:)
+ def fetch_user_comm_screener(users:, guardian:)
+ UserCommScreener.new(acting_user: guardian.user, target_user_ids: users.map(&:id))
+ end
+
+ def send_invite_notifications(channel:, guardian:, users:, user_comm_screener:, params:)
users&.each do |invited_user|
+ next if user_comm_screener.ignoring_or_muting_actor?(invited_user.id)
next if !invited_user.guardian.can_join_chat_channel?(channel)
data = {
diff --git a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
index 80a9cb52ef4..3a73e29a018 100644
--- a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
+++ b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
@@ -57,6 +57,15 @@ RSpec.describe Chat::InviteUsersToChannel do
expect(notification).to be_nil
end
+ it "notifies users who mute a staff inviter" do
+ Fabricate(:muted_user, user: user_1, muted_user: current_user)
+
+ result
+
+ notification = user_1.reload.notifications.last
+ expect(notification.notification_type).to eq(::Notification.types[:chat_invitation])
+ end
+
context "when message id is provided" do
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
@@ -71,6 +80,43 @@ RSpec.describe Chat::InviteUsersToChannel do
end
end
+ context "when invited users mute or ignore the inviter" do
+ fab!(:current_user, :user)
+ fab!(:muting_user, :user)
+ fab!(:ignoring_user, :user)
+
+ let(:user_ids) { [user_1.id, muting_user.id, ignoring_user.id] }
+ let(:invited_user_ids) do
+ ::Notification.where(notification_type: ::Notification.types[:chat_invitation]).pluck(
+ :user_id,
+ )
+ end
+
+ before do
+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
+ Fabricate(:muted_user, user: muting_user, muted_user: current_user)
+ Fabricate(:ignored_user, user: ignoring_user, ignored_user: current_user)
+ end
+
+ it "skips them but still notifies users who only disallow private messages" do
+ user_1.user_option.update!(allow_private_messages: false)
+
+ result
+
+ expect(invited_user_ids).to contain_exactly(user_1.id)
+ end
+
+ it "skips them when more users are invited than are fetched" do
+ params[:user_ids] = Fabricate.times(51, :muted_user, muted_user: current_user).map(
+ &:user_id
+ )
+
+ result
+
+ expect(invited_user_ids).to be_empty
+ end
+ end
+
context "when contract is invalid" do
let(:user_ids) { nil }