From f11f2b983f1887dcfa35f16dfc4bcff2cdf349ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Thu, 22 Aug 2024 11:58:55 +0200 Subject: [PATCH] DEV: Put back the `model` step in HandleCategoryUpdated service Now that `ActiveRecord` relations are properly handled in a `model` step, putting this step back will allow the service to stops its execution if there are no users to be removed without calling the pretty big SQL query contained in the `CalculateMembershipsForRemoval` action. --- .../auto_remove/handle_category_updated.rb | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb b/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb index 030f2da38d9..fff835bc986 100644 --- a/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb +++ b/plugins/chat/app/services/chat/auto_remove/handle_category_updated.rb @@ -19,6 +19,7 @@ module Chat policy :chat_enabled model :category model :category_channel_ids + model :users step :remove_users_without_channel_permission step :publish @@ -46,18 +47,21 @@ module Chat Chat::Channel.where(chatable: category).pluck(:id) end - def remove_users_without_channel_permission(category_channel_ids:) + def fetch_users(category_channel_ids:) + User + .real + .activated + .not_suspended + .not_staged + .joins(:user_chat_channel_memberships) + .where("user_chat_channel_memberships.chat_channel_id IN (?)", category_channel_ids) + .where("NOT admin AND NOT moderator") + end + + def remove_users_without_channel_permission(users:, category_channel_ids:) memberships_to_remove = Chat::Action::CalculateMembershipsForRemoval.call( - scoped_users_query: - User - .real - .activated - .not_suspended - .not_staged - .joins(:user_chat_channel_memberships) - .where("user_chat_channel_memberships.chat_channel_id IN (?)", category_channel_ids) - .where("NOT admin AND NOT moderator"), + scoped_users_query: users, channel_ids: category_channel_ids, )