2020-05-01 09:23:43 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: notifications
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# notification_type :integer not null
|
|
|
|
# primary_actor_type :string not null
|
|
|
|
# read_at :datetime
|
|
|
|
# secondary_actor_type :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :bigint not null
|
|
|
|
# primary_actor_id :bigint not null
|
|
|
|
# secondary_actor_id :bigint
|
|
|
|
# user_id :bigint not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_notifications_on_account_id (account_id)
|
|
|
|
# index_notifications_on_user_id (user_id)
|
|
|
|
# uniq_primary_actor_per_account_notifications (primary_actor_type,primary_actor_id)
|
|
|
|
# uniq_secondary_actor_per_account_notifications (secondary_actor_type,secondary_actor_id)
|
|
|
|
#
|
|
|
|
|
|
|
|
class Notification < ApplicationRecord
|
2021-06-29 09:02:57 +00:00
|
|
|
include MessageFormatHelper
|
2020-05-01 09:23:43 +00:00
|
|
|
belongs_to :account
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
belongs_to :primary_actor, polymorphic: true
|
|
|
|
belongs_to :secondary_actor, polymorphic: true, optional: true
|
|
|
|
|
|
|
|
NOTIFICATION_TYPES = {
|
|
|
|
conversation_creation: 1,
|
2020-09-10 13:49:15 +00:00
|
|
|
conversation_assignment: 2,
|
2021-01-26 18:34:11 +00:00
|
|
|
assigned_conversation_new_message: 3,
|
|
|
|
conversation_mention: 4
|
2020-05-01 09:23:43 +00:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
enum notification_type: NOTIFICATION_TYPES
|
|
|
|
|
2022-03-17 13:26:36 +00:00
|
|
|
after_create_commit :process_notification_delivery, :dispatch_create_event
|
|
|
|
|
2020-06-15 08:06:56 +00:00
|
|
|
default_scope { order(id: :desc) }
|
|
|
|
|
|
|
|
PRIMARY_ACTORS = ['Conversation'].freeze
|
2020-05-01 09:23:43 +00:00
|
|
|
|
2020-06-09 12:12:18 +00:00
|
|
|
def push_event_data
|
2021-02-19 13:05:21 +00:00
|
|
|
# Secondary actor could be nil for cases like system assigning conversation
|
2020-06-09 12:12:18 +00:00
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
notification_type: notification_type,
|
2020-06-15 08:06:56 +00:00
|
|
|
primary_actor_type: primary_actor_type,
|
|
|
|
primary_actor_id: primary_actor_id,
|
2022-03-28 14:31:23 +00:00
|
|
|
primary_actor: primary_actor_data,
|
2020-06-09 12:12:18 +00:00
|
|
|
read_at: read_at,
|
|
|
|
secondary_actor: secondary_actor&.push_event_data,
|
|
|
|
user: user&.push_event_data,
|
2022-03-28 14:31:23 +00:00
|
|
|
created_at: created_at.to_i,
|
|
|
|
account_id: account_id,
|
|
|
|
push_message_title: push_message_title
|
2020-06-09 12:12:18 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:31:23 +00:00
|
|
|
def primary_actor_data
|
|
|
|
if %w[assigned_conversation_new_message conversation_mention].include? notification_type
|
|
|
|
primary_actor.conversation.push_event_data
|
|
|
|
else
|
|
|
|
primary_actor.push_event_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-09 08:29:42 +00:00
|
|
|
def fcm_push_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
notification_type: notification_type,
|
|
|
|
primary_actor_id: primary_actor_id,
|
|
|
|
primary_actor_type: primary_actor_type,
|
2021-12-21 14:29:22 +00:00
|
|
|
primary_actor: primary_actor.push_event_data.with_indifferent_access.slice('conversation_id', 'id')
|
2021-09-09 08:29:42 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-06-09 12:12:18 +00:00
|
|
|
# TODO: move to a data presenter
|
2022-01-14 05:38:10 +00:00
|
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
2020-06-09 12:12:18 +00:00
|
|
|
def push_message_title
|
2021-02-12 17:26:12 +00:00
|
|
|
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)
|
|
|
|
when 'conversation_assignment'
|
|
|
|
I18n.t('notifications.notification_title.conversation_assignment', display_id: primary_actor.display_id)
|
|
|
|
when 'assigned_conversation_new_message'
|
|
|
|
I18n.t(
|
|
|
|
'notifications.notification_title.assigned_conversation_new_message',
|
2021-02-19 13:05:21 +00:00
|
|
|
display_id: conversation.display_id,
|
2022-01-14 05:38:10 +00:00
|
|
|
content: primary_actor&.content&.truncate_words(10)
|
2021-02-12 17:26:12 +00:00
|
|
|
)
|
|
|
|
when 'conversation_mention'
|
2022-01-14 05:38:10 +00:00
|
|
|
"[##{conversation&.display_id}] #{transform_user_mention_content primary_actor&.content}"
|
2021-02-12 17:26:12 +00:00
|
|
|
else
|
|
|
|
''
|
2020-06-09 12:12:18 +00:00
|
|
|
end
|
|
|
|
end
|
2022-01-14 05:38:10 +00:00
|
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
2020-06-09 12:12:18 +00:00
|
|
|
|
2021-02-15 18:45:45 +00:00
|
|
|
def conversation
|
2021-02-19 13:05:21 +00:00
|
|
|
return primary_actor.conversation if %w[assigned_conversation_new_message conversation_mention].include? notification_type
|
2021-02-15 18:45:45 +00:00
|
|
|
|
|
|
|
primary_actor
|
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def process_notification_delivery
|
2020-05-05 18:40:56 +00:00
|
|
|
Notification::PushNotificationJob.perform_later(self)
|
2020-09-10 13:49:15 +00:00
|
|
|
|
2020-09-08 05:54:08 +00:00
|
|
|
# Should we do something about the case where user subscribed to both push and email ?
|
|
|
|
# In future, we could probably add condition here to enqueue the job for 30 seconds later
|
|
|
|
# when push enabled and then check in email job whether notification has been read already.
|
|
|
|
Notification::EmailNotificationJob.perform_later(self)
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|
2022-03-17 13:26:36 +00:00
|
|
|
|
|
|
|
def dispatch_create_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(NOTIFICATION_CREATED, Time.zone.now, notification: self)
|
|
|
|
end
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|