From ea5dec82a602e0e22ea231f0288ed477f10d4681 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Fri, 21 Apr 2023 17:54:02 +0400 Subject: [PATCH] DEV: Extract mentions.count method (#21116) --- plugins/chat/lib/chat/message_mentions.rb | 10 ++++++++++ plugins/chat/lib/chat/notifier.rb | 7 +------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/plugins/chat/lib/chat/message_mentions.rb b/plugins/chat/lib/chat/message_mentions.rb index 613b8aead1f..cd8f3d78dc8 100644 --- a/plugins/chat/lib/chat/message_mentions.rb +++ b/plugins/chat/lib/chat/message_mentions.rb @@ -31,6 +31,16 @@ module Chat end end + def count + @count ||= + begin + result = @parsed_direct_mentions.length + @parsed_group_mentions.length + result += 1 if @has_global_mention + result += 1 if @has_here_mention + result + end + end + def global_mentions return User.none unless @has_global_mention channel_members.where.not(username_lower: @parsed_direct_mentions) diff --git a/plugins/chat/lib/chat/notifier.rb b/plugins/chat/lib/chat/notifier.rb index d06d75dc7b3..7d4e879a4f5 100644 --- a/plugins/chat/lib/chat/notifier.rb +++ b/plugins/chat/lib/chat/notifier.rb @@ -108,12 +108,7 @@ module Chat private def list_users_to_notify - mentions_count = - @mentions.parsed_direct_mentions.length + @mentions.parsed_group_mentions.length - mentions_count += 1 if @mentions.has_global_mention - mentions_count += 1 if @mentions.has_here_mention - - skip_notifications = mentions_count > SiteSetting.max_mentions_per_chat_message + skip_notifications = @mentions.count > SiteSetting.max_mentions_per_chat_message {}.tap do |to_notify| # The order of these methods is the precedence