chore: Change primary actor to Conversation for all the notification types. (#8435)

This commit is contained in:
Muhsin Keloth
2023-12-06 10:43:09 +05:30
committed by GitHub
parent 17faa6c3b2
commit 76711d95ff
12 changed files with 91 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
class NotificationBuilder
pattr_initialize [:notification_type!, :user!, :account!, :primary_actor!]
pattr_initialize [:notification_type!, :user!, :account!, :primary_actor!, :secondary_actor]
def perform
return unless user_subscribed_to_notification?
@@ -9,7 +9,7 @@ class NotificationBuilder
private
def secondary_actor
def current_user
Current.user
end
@@ -29,7 +29,8 @@ class NotificationBuilder
notification_type: notification_type,
account: account,
primary_actor: primary_actor,
secondary_actor: secondary_actor
# secondary_actor is secondary_actor if present, else current_user
secondary_actor: secondary_actor || current_user
)
end
end

View File

@@ -0,0 +1,8 @@
# Delete migration and spec after 2 consecutive releases.
class Migration::RemoveMessageNotifications < ApplicationJob
queue_as :scheduled_jobs
def perform
Notification.where(primary_actor_type: 'Message').in_batches(of: 100).each_record(&:destroy)
end
end

View File

@@ -68,17 +68,10 @@ class Notification < ApplicationRecord
end
def primary_actor_data
if %w[assigned_conversation_new_message conversation_mention].include? notification_type
{
id: primary_actor.conversation.push_event_data[:id],
meta: primary_actor.conversation.push_event_data[:meta]
}
else
{
id: primary_actor.push_event_data[:id],
meta: primary_actor.push_event_data[:meta]
}
end
{
id: primary_actor.push_event_data[:id],
meta: primary_actor.push_event_data[:meta]
}
end
def fcm_push_data
@@ -92,37 +85,33 @@ class Notification < ApplicationRecord
end
# TODO: move to a data presenter
# rubocop:disable Metrics/CyclomaticComplexity
def push_message_title
case notification_type
when 'conversation_creation'
I18n.t('notifications.notification_title.conversation_creation', display_id: primary_actor.display_id, inbox_name: primary_actor.inbox.name)
I18n.t('notifications.notification_title.conversation_creation', display_id: conversation.display_id, inbox_name: primary_actor.inbox.name)
when 'conversation_assignment'
I18n.t('notifications.notification_title.conversation_assignment', display_id: primary_actor.display_id)
I18n.t('notifications.notification_title.conversation_assignment', display_id: conversation.display_id)
when 'assigned_conversation_new_message', 'participating_conversation_new_message'
I18n.t(
'notifications.notification_title.assigned_conversation_new_message',
display_id: conversation.display_id,
content: transform_user_mention_content(primary_actor&.content&.truncate_words(10))
content: content
)
when 'conversation_mention'
"[##{conversation&.display_id}] #{transform_user_mention_content primary_actor&.content}"
"[##{conversation&.display_id}] #{transform_user_mention_content content}"
else
''
end
end
# rubocop:enable Metrics/CyclomaticComplexity
def conversation
return primary_actor.conversation if %w[
assigned_conversation_new_message
participating_conversation_new_message
conversation_mention
].include? notification_type
primary_actor
end
def content
transform_user_mention_content(secondary_actor&.content&.truncate_words(10) || '')
end
private
def process_notification_delivery

View File

@@ -35,7 +35,8 @@ class Messages::MentionService
notification_type: 'conversation_mention',
user: User.find(user_id),
account: message.account,
primary_actor: message
primary_actor: message.conversation,
secondary_actor: message
).perform
end
end

View File

@@ -21,7 +21,8 @@ class Messages::NewMessageNotificationService
notification_type: 'participating_conversation_new_message',
user: participant,
account: account,
primary_actor: message
primary_actor: message.conversation,
secondary_actor: message
).perform
end
end
@@ -35,7 +36,8 @@ class Messages::NewMessageNotificationService
notification_type: 'assigned_conversation_new_message',
user: conversation.assignee,
account: account,
primary_actor: message
primary_actor: message.conversation,
secondary_actor: message
).perform
end

View File

@@ -11,19 +11,9 @@ json.data do
json.notification_type notification.notification_type
json.push_message_title notification.push_message_title
# TODO: front end assumes primary actor to be conversation. should fix in future
if %w[
assigned_conversation_new_message
participating_conversation_new_message
conversation_mention
].include? notification.notification_type
json.primary_actor_type 'Conversation'
json.primary_actor_id notification.conversation.id
json.primary_actor notification.conversation.push_event_data
else
json.primary_actor_type notification.primary_actor_type
json.primary_actor_id notification.primary_actor_id
json.primary_actor notification.primary_actor.push_event_data
end
json.primary_actor_type notification.primary_actor_type
json.primary_actor_id notification.primary_actor_id
json.primary_actor notification.primary_actor.push_event_data
json.read_at notification.read_at
# Secondary actor could be nil for cases like system assigning conversation
json.secondary_actor notification.secondary_actor&.push_event_data