FEATURE: Enforce mention limits for chat messages (#19034)

* FEATURE: Enforce mention limits for chat messages

The first part of these changes adds a new setting called `max_mentions_per_chat_message`, which skips notifications when the message contains too many mentions. It also respects the `max_users_notified_per_group_mention` setting
and skips notifications if expanding a group mention would exceed it.

We also include a new component to display JIT warning for these limits to the user while composing a message.

* Simplify ignoring/muting filter in chat_notifier

* Post-send warnings for unsent warnings

* Improve pluralization

* Address review feedback

* Fix test

* Address second feedback round

* Third round of feedback

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
This commit is contained in:
Roman Rizzi
2022-12-06 14:54:04 -03:00
committed by GitHub
parent 4e92a6e804
commit 9c8043a4d2
23 changed files with 851 additions and 136 deletions

View File

@@ -0,0 +1,39 @@
# frozen_string_literal: true
class Chat::Api::HintsController < ApplicationController
before_action :ensure_logged_in
def check_group_mentions
RateLimiter.new(current_user, "group_mention_hints", 5, 10.seconds).performed!
group_names = params[:mentions]
raise Discourse::InvalidParameters.new(:mentions) if group_names.blank?
visible_groups = Group
.where("LOWER(name) IN (?)", group_names)
.visible_groups(current_user)
.pluck(:name)
mentionable_groups = filter_mentionable_groups(visible_groups)
result = {
unreachable: visible_groups - mentionable_groups.map(&:name),
over_members_limit: mentionable_groups.select { |g| g.user_count > SiteSetting.max_users_notified_per_group_mention }.map(&:name),
}
result[:invalid] = (group_names - result[:unreachable]) - result[:over_members_limit]
render json: result
end
private
def filter_mentionable_groups(group_names)
return [] if group_names.empty?
Group
.select(:name, :user_count)
.where(name: group_names)
.mentionable(current_user, include_public: false)
end
end

View File

@@ -153,23 +153,19 @@ module ChatPublisher
user_id,
chat_message,
cannot_chat_users,
without_membership
without_membership,
too_many_members,
mentions_disabled
)
MessageBus.publish(
"/chat/#{chat_message.chat_channel_id}",
{
type: :mention_warning,
chat_message_id: chat_message.id,
cannot_see:
ActiveModel::ArraySerializer.new(
cannot_chat_users,
each_serializer: BasicUserSerializer,
).as_json,
without_membership:
ActiveModel::ArraySerializer.new(
without_membership,
each_serializer: BasicUserSerializer,
).as_json,
cannot_see: cannot_chat_users.map { |u| { username: u.username, id: u.id } }.as_json,
without_membership: without_membership.map { |u| { username: u.username, id: u.id } }.as_json,
groups_with_too_many_members: too_many_members.map(&:name).as_json,
group_mentions_disabled: mentions_disabled.map(&:name).as_json
},
user_ids: [user_id],
)