From e292c45924bb0b0a79497e5f494afcb8af2e1efc Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Thu, 9 Mar 2023 22:17:18 +0400 Subject: [PATCH] DEV: better split create_notification! and send_notifications logic (#20562) `create_notification!` - creates a notification in the database, `send_notifications` sends desktop and mobile notifications. This PR moves some code to decouple these two tasks more explicitly. It only moves code without changing any behavior, and the job is covered with tests (see chat_notify_mentioned_spec). --- .../app/jobs/regular/chat_notify_mentioned.rb | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/plugins/chat/app/jobs/regular/chat_notify_mentioned.rb b/plugins/chat/app/jobs/regular/chat_notify_mentioned.rb index aa8feafb221..f0102aa7a68 100644 --- a/plugins/chat/app/jobs/regular/chat_notify_mentioned.rb +++ b/plugins/chat/app/jobs/regular/chat_notify_mentioned.rb @@ -100,9 +100,9 @@ module Jobs payload end - def create_notification!(membership, notification_data, mention) + def create_notification!(membership, mention, mention_type) + notification_data = build_data_for(membership, identifier_type: mention_type) is_read = Chat::ChatNotifier.user_has_seen_message?(membership, @chat_message.id) - notification = Notification.create!( notification_type: Notification.types[:chat_mention], @@ -115,22 +115,19 @@ module Jobs mention.update!(notification: notification) end - def send_notifications(membership, notification_data, os_payload) - mention = ChatMention.find_by(user: membership.user, chat_message: @chat_message) - return if mention.blank? - - create_notification!(membership, notification_data, mention) + def send_notifications(membership, mention_type) + payload = build_payload_for(membership, identifier_type: mention_type) if !membership.desktop_notifications_never? && !membership.muted? MessageBus.publish( "/chat/notification-alert/#{membership.user_id}", - os_payload, + payload, user_ids: [membership.user_id], ) end if !membership.mobile_notifications_never? && !membership.muted? - PostAlerter.push_notification(membership.user, os_payload) + PostAlerter.push_notification(membership.user, payload) end end @@ -138,10 +135,11 @@ module Jobs memberships = get_memberships(user_ids) memberships.each do |membership| - notification_data = build_data_for(membership, identifier_type: mention_type) - payload = build_payload_for(membership, identifier_type: mention_type) - - send_notifications(membership, notification_data, payload) + mention = ChatMention.find_by(user: membership.user, chat_message: @chat_message) + if mention.present? + create_notification!(membership, mention, mention_type) + send_notifications(membership, mention_type) + end end end end