PERF: auto join & leave chat channels (#29193)

Chat channels that are linked to a category can be set to automatically join users.

This is handled by subscribing to the following events

- group_destroyed
- user_seen
- user_confirmed_email
- user_added_to_group
- user_removed_from_group
- category_updated
- site_setting_changed (for `chat_allowed_groups`)

As well as a

- hourly background job (`AutoJoinUsers`)
- `CreateCategoryChannel` service
- `UpdateChannel` service

There was however two issues with the current implementation

1. We were triggering a lot of background jobs, mostly because it was decided to batch to auto join/leave into groups of 1000 users, adding a lot of stress to the system
2. We had one "class" (a service or a background job) per "event" and all of them had slightly different ways to select users to join/leave, making it hard to keep everything in sync

This PR "simply" adds two new servicesL `AutoJoinChannels` and `AutoLeaveChannels` that takes care, in an efficient way, of all the cases when users might automatically join a leave a chat channel.

Every other changes come from the fact that we're now always calling either one of those services, depending on the event that happened.

In the making of these classes, a few bugs were encountered and fixed, notably

- A user is only ever able to access chat channels if and only if they're part of a group listed in the `chat_allowed_group` site setting
- A category that has no associated "category groups" is only accessible to staff members (and not "Everyone")
- A silenced user should not be able to automatically join channels
- We should not attempt to automatically join users to deleted chat channels
- There is no need to automatically join users to chat channels that have already more than `max_chat_auto_joined_users` users

Internal - t/135259 & t/70607

* DEV: add specs for auto join/leave channels services

* DEV: less hacky specs

* DEV: no instance variables in specs
This commit is contained in:
Régis Hanol
2024-11-12 05:00:59 +01:00
committed by GitHub
parent 34ed35e174
commit 6dfe2fbe16
47 changed files with 835 additions and 2321 deletions

View File

@ -289,10 +289,7 @@ after_initialize do
end
if name == :chat_allowed_groups
Jobs.enqueue(
Jobs::Chat::AutoRemoveMembershipHandleChatAllowedGroupsChange,
new_allowed_groups: new_value,
)
Jobs.enqueue(Jobs::Chat::AutoJoinUsers, event: "chat_allowed_groups_changed")
end
end
@ -301,11 +298,8 @@ after_initialize do
Chat::PostNotificationHandler.new(post, notified).handle
end
on(:group_destroyed) do |group, user_ids|
Jobs.enqueue(
Jobs::Chat::AutoRemoveMembershipHandleDestroyedGroup,
destroyed_group_user_ids: user_ids,
)
on(:group_destroyed) do |group, _user_ids|
Chat::AutoLeaveChannels.call(params: { group_id: group.id, event: :group_destroyed })
end
register_presence_channel_prefix("chat") do |channel_name|
@ -358,52 +352,31 @@ after_initialize do
on(:user_seen) do |user|
if user.last_seen_at == user.first_seen_at
Chat::Channel
.where(auto_join_users: true)
.each do |channel|
Chat::ChannelMembershipManager.new(channel).enforce_automatic_user_membership(user)
end
Chat::AutoJoinChannels.call(params: { user_id: user.id })
end
end
on(:user_confirmed_email) do |user|
if user.active?
Chat::Channel
.where(auto_join_users: true)
.each do |channel|
Chat::ChannelMembershipManager.new(channel).enforce_automatic_user_membership(user)
end
end
Chat::AutoJoinChannels.call(params: { user_id: user.id }) if user.active?
end
on(:user_added_to_group) do |user, group|
channels_to_add =
Chat::Channel
.distinct
.where(auto_join_users: true, chatable_type: "Category")
.joins(
"INNER JOIN category_groups ON category_groups.category_id = chat_channels.chatable_id",
)
.where(category_groups: { group_id: group.id })
channels_to_add.each do |channel|
Chat::ChannelMembershipManager.new(channel).enforce_automatic_user_membership(user)
end
on(:user_added_to_group) do |user, _group|
Chat::AutoJoinChannels.call(params: { user_id: user.id })
end
on(:user_removed_from_group) do |user, group|
Jobs.enqueue(Jobs::Chat::AutoRemoveMembershipHandleUserRemovedFromGroup, user_id: user.id)
on(:user_removed_from_group) do |user, _group|
Chat::AutoLeaveChannels.call(params: { user_id: user.id, event: :user_removed_from_group })
end
on(:category_updated) do |category|
# There's a bug on core where this event is triggered with an `#update` result (true/false)
if category.is_a?(Category) && category_channel = Chat::Channel.find_by(chatable: category)
if category_channel.auto_join_users
Chat::ChannelMembershipManager.new(category_channel).enforce_automatic_channel_memberships
end
next unless category.is_a?(Category)
next unless category_channel = Chat::Channel.find_by(chatable: category)
Jobs.enqueue(Jobs::Chat::AutoRemoveMembershipHandleCategoryUpdated, category_id: category.id)
if category_channel.auto_join_users
Chat::AutoJoinChannels.call(params: { category_id: category.id })
end
Chat::AutoLeaveChannels.call(params: { category_id: category.id, event: :category_updated })
end
# outgoing webhook events