chore: Refactor the notification service for participants (#6461)
This commit is contained in:
@@ -14,7 +14,7 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController
|
||||
|
||||
# Deprecated: This API will be removed in 2.7.0
|
||||
def assignable_agents
|
||||
@assignable_agents = (Current.account.users.where(id: @inbox.members.select(:user_id)) + Current.account.administrators).uniq
|
||||
@assignable_agents = @inbox.assignable_agents
|
||||
end
|
||||
|
||||
def campaigns
|
||||
|
||||
@@ -28,50 +28,9 @@ class NotificationListener < BaseListener
|
||||
end
|
||||
|
||||
def message_created(event)
|
||||
message, account = extract_message_and_account(event)
|
||||
conversation = message.conversation
|
||||
message = extract_message_and_account(event)[0]
|
||||
|
||||
generate_notifications_for_mentions(message, account)
|
||||
|
||||
# only want to notify agents about customer messages
|
||||
return unless message.incoming?
|
||||
return unless conversation.assignee
|
||||
|
||||
NotificationBuilder.new(
|
||||
notification_type: 'assigned_conversation_new_message',
|
||||
user: conversation.assignee,
|
||||
account: account,
|
||||
primary_actor: message
|
||||
).perform
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_valid_mentioned_ids(mentioned_ids, inbox)
|
||||
valid_mentionable_ids = inbox.account.administrators.map(&:id) + inbox.members.map(&:id)
|
||||
# Intersection of ids
|
||||
mentioned_ids & valid_mentionable_ids.uniq.map(&:to_s)
|
||||
end
|
||||
|
||||
def generate_notifications_for_mentions(message, account)
|
||||
return unless message.private?
|
||||
|
||||
return if message.content.blank?
|
||||
|
||||
mentioned_ids = message.content.scan(%r{\(mention://(user|team)/(\d+)/(.+?)\)}).map(&:second).uniq
|
||||
|
||||
return if mentioned_ids.blank?
|
||||
|
||||
valid_mentioned_ids = get_valid_mentioned_ids(mentioned_ids, message.inbox)
|
||||
Conversations::UserMentionJob.perform_later(valid_mentioned_ids, message.conversation.id, account.id)
|
||||
|
||||
valid_mentioned_ids.each do |user_id|
|
||||
NotificationBuilder.new(
|
||||
notification_type: 'conversation_mention',
|
||||
user: User.find(user_id),
|
||||
account: account,
|
||||
primary_actor: message
|
||||
).perform
|
||||
end
|
||||
Messages::MentionService.new(message: message).perform
|
||||
Messages::NewMessageNotificationService.new(message: message).perform
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,10 @@ module MessageFilterHelpers
|
||||
incoming? || outgoing?
|
||||
end
|
||||
|
||||
def notifiable?
|
||||
incoming? || outgoing?
|
||||
end
|
||||
|
||||
def conversation_transcriptable?
|
||||
incoming? || outgoing?
|
||||
end
|
||||
|
||||
@@ -106,6 +106,10 @@ class Inbox < ApplicationRecord
|
||||
channel_type == 'Channel::Whatsapp'
|
||||
end
|
||||
|
||||
def assignable_agents
|
||||
(account.users.where(id: members.select(:user_id)) + account.administrators).uniq
|
||||
end
|
||||
|
||||
def active_bot?
|
||||
agent_bot_inbox&.active? || hooks.pluck(:app_id).include?('dialogflow')
|
||||
end
|
||||
|
||||
41
app/services/messages/mention_service.rb
Normal file
41
app/services/messages/mention_service.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
class Messages::MentionService
|
||||
pattr_initialize [:message!]
|
||||
|
||||
def perform
|
||||
return unless valid_mention_message?(message)
|
||||
|
||||
validated_mentioned_ids = filter_mentioned_ids_by_inbox
|
||||
return if validated_mentioned_ids.blank?
|
||||
|
||||
Conversations::UserMentionJob.perform_later(validated_mentioned_ids, message.conversation.id, message.account.id)
|
||||
generate_notifications_for_mentions(validated_mentioned_ids)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_mention_message?(message)
|
||||
message.private? && message.content.present? && mentioned_ids.present?
|
||||
end
|
||||
|
||||
def mentioned_ids
|
||||
@mentioned_ids ||= message.content.scan(%r{\(mention://(user|team)/(\d+)/(.+?)\)}).map(&:second).uniq
|
||||
end
|
||||
|
||||
def filter_mentioned_ids_by_inbox
|
||||
inbox = message.inbox
|
||||
valid_mentionable_ids = inbox.account.administrators.map(&:id) + inbox.members.map(&:id)
|
||||
# Intersection of ids
|
||||
mentioned_ids & valid_mentionable_ids.uniq.map(&:to_s)
|
||||
end
|
||||
|
||||
def generate_notifications_for_mentions(validated_mentioned_ids)
|
||||
validated_mentioned_ids.each do |user_id|
|
||||
NotificationBuilder.new(
|
||||
notification_type: 'conversation_mention',
|
||||
user: User.find(user_id),
|
||||
account: message.account,
|
||||
primary_actor: message
|
||||
).perform
|
||||
end
|
||||
end
|
||||
end
|
||||
25
app/services/messages/new_message_notification_service.rb
Normal file
25
app/services/messages/new_message_notification_service.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
class Messages::NewMessageNotificationService
|
||||
pattr_initialize [:message!]
|
||||
|
||||
def perform
|
||||
return unless message.notifiable?
|
||||
|
||||
notify_conversation_assignee
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
delegate :conversation, :sender, :account, to: :message
|
||||
|
||||
def notify_conversation_assignee
|
||||
return if conversation.assignee.blank?
|
||||
return if conversation.assignee == sender
|
||||
|
||||
NotificationBuilder.new(
|
||||
notification_type: 'assigned_conversation_new_message',
|
||||
user: conversation.assignee,
|
||||
account: account,
|
||||
primary_actor: message
|
||||
).perform
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user