From 3349ce2c7996f851fa266410941175523bfaf7b3 Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Thu, 20 Jul 2023 22:06:36 +0400 Subject: [PATCH] DEV: extract channel. joined_by method (#21752) This only moves code around and doesn't change any behavior. This does two things: 1. Extracts the `channel.joined_by?` methods 2. Uses term "members" instead of "participants" for chat members --- plugins/chat/app/models/chat/channel.rb | 8 ++++++++ plugins/chat/lib/chat/notifier.rb | 18 ++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/plugins/chat/app/models/chat/channel.rb b/plugins/chat/app/models/chat/channel.rb index 1b6f69f7c99..977e82bd6c4 100644 --- a/plugins/chat/app/models/chat/channel.rb +++ b/plugins/chat/app/models/chat/channel.rb @@ -119,6 +119,14 @@ module Chat update_user_counts end + def joined_by?(user) + user.user_chat_channel_memberships.strict_loading.any? do |membership| + predicate = membership.chat_channel_id == id + predicate = predicate && membership.following if public_channel? + predicate + end + end + def self.update_message_counts # NOTE: Chat::Channel#messages_count is not updated every time # a message is created or deleted in a channel, so it should not diff --git a/plugins/chat/lib/chat/notifier.rb b/plugins/chat/lib/chat/notifier.rb index 5a0dc633710..052d222b4da 100644 --- a/plugins/chat/lib/chat/notifier.rb +++ b/plugins/chat/lib/chat/notifier.rb @@ -158,20 +158,14 @@ module Chat end def group_users_to_notify(users) - potential_participants, unreachable = + potential_members, unreachable = users.partition { |user| user.guardian.can_join_chat_channel?(@chat_channel) } - participants, welcome_to_join = - potential_participants.partition do |participant| - participant.user_chat_channel_memberships.any? do |m| - predicate = m.chat_channel_id == @chat_channel.id - predicate = predicate && m.following == true if @chat_channel.public_channel? - predicate - end - end + members, welcome_to_join = + potential_members.partition { |user| @chat_channel.joined_by?(user) } { - already_participating: participants || [], + members: members || [], welcome_to_join: welcome_to_join || [], unreachable: unreachable || [], } @@ -191,7 +185,7 @@ module Chat grouped = group_users_to_notify(direct_mentions) - to_notify[:direct_mentions] = grouped[:already_participating].map(&:id) + to_notify[:direct_mentions] = grouped[:members].map(&:id) inaccessible[:welcome_to_join] = grouped[:welcome_to_join] inaccessible[:unreachable] = grouped[:unreachable] already_covered_ids.concat(to_notify[:direct_mentions]) @@ -211,7 +205,7 @@ module Chat @parsed_mentions.groups_to_mention.each { |g| to_notify[g.name.downcase] = [] } grouped = group_users_to_notify(reached_by_group) - grouped[:already_participating].each do |user| + grouped[:members].each do |user| # When a user is a member of multiple mentioned groups, # the most far to the left should take precedence. ordered_group_names =