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).
This commit is contained in:
Andrei Prigorshnev
2023-03-09 22:17:18 +04:00
committed by GitHub
parent 73be7b3dd8
commit e292c45924

View File

@ -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