2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: conversations
|
|
|
|
#
|
2022-05-19 04:16:33 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# additional_attributes :jsonb
|
|
|
|
# agent_last_seen_at :datetime
|
|
|
|
# assignee_last_seen_at :datetime
|
|
|
|
# contact_last_seen_at :datetime
|
|
|
|
# custom_attributes :jsonb
|
|
|
|
# first_reply_created_at :datetime
|
|
|
|
# identifier :string
|
|
|
|
# last_activity_at :datetime not null
|
|
|
|
# snoozed_until :datetime
|
|
|
|
# status :integer default("open"), not null
|
|
|
|
# uuid :uuid not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# assignee_id :integer
|
|
|
|
# campaign_id :bigint
|
|
|
|
# contact_id :bigint
|
|
|
|
# contact_inbox_id :bigint
|
|
|
|
# display_id :integer not null
|
|
|
|
# inbox_id :integer not null
|
|
|
|
# team_id :bigint
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-07-23 13:09:24 +00:00
|
|
|
# index_conversations_on_account_id (account_id)
|
|
|
|
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
|
|
|
|
# index_conversations_on_assignee_id_and_account_id (assignee_id,account_id)
|
|
|
|
# index_conversations_on_campaign_id (campaign_id)
|
|
|
|
# index_conversations_on_contact_inbox_id (contact_inbox_id)
|
2022-05-19 04:16:33 +00:00
|
|
|
# index_conversations_on_first_reply_created_at (first_reply_created_at)
|
2022-04-28 11:28:06 +00:00
|
|
|
# index_conversations_on_last_activity_at (last_activity_at)
|
2021-07-23 13:09:24 +00:00
|
|
|
# index_conversations_on_status_and_account_id (status,account_id)
|
|
|
|
# index_conversations_on_team_id (team_id)
|
2022-08-23 13:35:30 +00:00
|
|
|
# index_conversations_on_uuid (uuid) UNIQUE
|
2020-01-09 07:36:40 +00:00
|
|
|
#
|
2019-11-30 13:39:55 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Conversation < ApplicationRecord
|
2021-01-03 14:37:57 +00:00
|
|
|
include Labelable
|
2021-03-12 09:43:58 +00:00
|
|
|
include AssignmentHandler
|
2022-08-16 11:28:23 +00:00
|
|
|
include AutoAssignmentHandler
|
2021-11-12 10:47:59 +00:00
|
|
|
include ActivityMessageHandler
|
2022-03-30 09:06:22 +00:00
|
|
|
include UrlHelper
|
2022-06-22 05:34:42 +00:00
|
|
|
include SortHandler
|
2021-01-03 14:37:57 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :inbox_id, presence: true
|
2021-01-07 08:17:38 +00:00
|
|
|
before_validation :validate_additional_attributes
|
2022-03-24 10:08:28 +00:00
|
|
|
validates :additional_attributes, jsonb_attributes_length: true
|
|
|
|
validates :custom_attributes, jsonb_attributes_length: true
|
2022-08-23 13:35:30 +00:00
|
|
|
validates :uuid, uniqueness: true
|
2022-03-30 09:06:22 +00:00
|
|
|
validate :validate_referer_url
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
scope :unassigned, -> { where(assignee_id: nil) }
|
2021-07-19 13:40:58 +00:00
|
|
|
scope :assigned, -> { where.not(assignee_id: nil) }
|
2019-10-08 19:45:04 +00:00
|
|
|
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
2021-11-27 03:56:18 +00:00
|
|
|
scope :resolvable, lambda { |auto_resolve_duration|
|
|
|
|
return [] if auto_resolve_duration.to_i.zero?
|
|
|
|
|
|
|
|
open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days)
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2022-06-22 05:34:42 +00:00
|
|
|
scope :last_user_message_at, lambda {
|
|
|
|
joins(
|
2022-07-05 19:07:43 +00:00
|
|
|
"INNER JOIN (#{last_messaged_conversations.to_sql}) AS grouped_conversations
|
2022-06-22 05:34:42 +00:00
|
|
|
ON grouped_conversations.conversation_id = conversations.id"
|
|
|
|
).sort_on_last_user_message_at
|
|
|
|
}
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
belongs_to :account
|
|
|
|
belongs_to :inbox
|
|
|
|
belongs_to :assignee, class_name: 'User', optional: true
|
2019-10-20 19:10:18 +00:00
|
|
|
belongs_to :contact
|
2020-01-09 07:36:40 +00:00
|
|
|
belongs_to :contact_inbox
|
2021-01-17 18:26:56 +00:00
|
|
|
belongs_to :team, optional: true
|
2021-04-29 16:53:32 +00:00
|
|
|
belongs_to :campaign, optional: true
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :mentions, dependent: :destroy_async
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :messages, dependent: :destroy_async, autosave: true
|
|
|
|
has_one :csat_survey_response, dependent: :destroy_async
|
2022-08-10 11:46:46 +00:00
|
|
|
has_many :notifications, as: :primary_actor, dependent: :destroy_async
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
before_save :ensure_snooze_until_reset
|
2021-07-21 16:32:43 +00:00
|
|
|
before_create :mark_conversation_pending_if_bot
|
2021-01-05 14:37:04 +00:00
|
|
|
|
2021-11-12 10:47:59 +00:00
|
|
|
after_update_commit :execute_after_update_commit_callbacks
|
2021-11-27 03:56:18 +00:00
|
|
|
after_create_commit :notify_conversation_creation
|
2021-01-05 14:37:04 +00:00
|
|
|
after_commit :set_display_id, unless: :display_id?
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-11-01 07:23:25 +00:00
|
|
|
delegate :auto_resolve_duration, to: :account
|
|
|
|
|
2020-07-25 17:24:45 +00:00
|
|
|
def can_reply?
|
2022-06-14 12:35:37 +00:00
|
|
|
channel = inbox&.channel
|
2022-04-19 06:51:20 +00:00
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
return can_reply_on_instagram? if additional_attributes['type'] == 'instagram_direct_message'
|
2020-07-25 17:24:45 +00:00
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
return true unless channel&.messaging_window_enabled?
|
2022-04-19 06:51:20 +00:00
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
messaging_window = inbox.api? ? channel.additional_attributes['agent_reply_time_window'].to_i : 24
|
|
|
|
last_message_in_messaging_window?(messaging_window)
|
2022-04-19 06:51:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_incoming_message
|
|
|
|
messages&.incoming&.last
|
|
|
|
end
|
2020-07-25 17:24:45 +00:00
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
def last_message_in_messaging_window?(time)
|
2020-07-25 17:24:45 +00:00
|
|
|
return false if last_incoming_message.nil?
|
|
|
|
|
2022-06-14 12:35:37 +00:00
|
|
|
Time.current < last_incoming_message.created_at + time.hours
|
2020-07-25 17:24:45 +00:00
|
|
|
end
|
|
|
|
|
2022-05-26 13:35:30 +00:00
|
|
|
def can_reply_on_instagram?
|
|
|
|
global_config = GlobalConfig.get('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT')
|
|
|
|
|
|
|
|
return false if last_incoming_message.nil?
|
|
|
|
|
|
|
|
if global_config['ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT']
|
|
|
|
Time.current < last_incoming_message.created_at + 7.days
|
|
|
|
else
|
2022-06-14 12:35:37 +00:00
|
|
|
last_message_in_messaging_window?(24)
|
2022-05-26 13:35:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def update_assignee(agent = nil)
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(assignee: agent)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_status
|
2020-03-10 18:32:15 +00:00
|
|
|
# FIXME: implement state machine with aasm
|
2019-10-08 19:45:04 +00:00
|
|
|
self.status = open? ? :resolved : :open
|
2021-07-23 09:54:07 +00:00
|
|
|
self.status = :open if pending? || snoozed?
|
2019-10-12 18:08:41 +00:00
|
|
|
save
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def mute!
|
|
|
|
resolved!
|
|
|
|
Redis::Alfred.setex(mute_key, 1, mute_period)
|
2020-10-30 16:57:25 +00:00
|
|
|
create_muted_message
|
2020-05-26 12:13:59 +00:00
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
def unmute!
|
|
|
|
Redis::Alfred.delete(mute_key)
|
2020-10-30 16:57:25 +00:00
|
|
|
create_unmuted_message
|
2020-10-08 06:32:08 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def muted?
|
2020-11-25 08:29:38 +00:00
|
|
|
Redis::Alfred.get(mute_key).present?
|
2020-05-26 12:13:59 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def unread_messages
|
2019-10-12 18:08:41 +00:00
|
|
|
messages.unread_since(agent_last_seen_at)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_incoming_messages
|
2019-10-12 18:08:41 +00:00
|
|
|
messages.incoming.unread_since(agent_last_seen_at)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_event_data
|
2019-10-12 18:08:41 +00:00
|
|
|
Conversations::EventDataPresenter.new(self).push_data
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def lock_event_data
|
2019-10-12 18:08:41 +00:00
|
|
|
Conversations::EventDataPresenter.new(self).lock_data
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
2020-06-12 18:49:43 +00:00
|
|
|
Conversations::EventDataPresenter.new(self).push_data
|
2020-02-26 04:14:24 +00:00
|
|
|
end
|
|
|
|
|
2020-03-05 20:13:12 +00:00
|
|
|
def notifiable_assignee_change?
|
|
|
|
return false unless saved_change_to_assignee_id?
|
|
|
|
return false if assignee_id.blank?
|
2021-12-28 16:23:13 +00:00
|
|
|
return false if self_assign?(assignee_id)
|
2020-03-05 20:13:12 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-08-23 16:30:47 +00:00
|
|
|
def tweet?
|
|
|
|
inbox.inbox_type == 'Twitter' && additional_attributes['type'] == 'tweet'
|
|
|
|
end
|
|
|
|
|
2021-11-17 12:35:53 +00:00
|
|
|
def recent_messages
|
|
|
|
messages.chat.last(5)
|
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
private
|
|
|
|
|
2021-11-12 10:47:59 +00:00
|
|
|
def execute_after_update_commit_callbacks
|
|
|
|
notify_status_change
|
|
|
|
create_activity
|
2022-02-15 18:06:29 +00:00
|
|
|
notify_conversation_updation
|
2021-11-12 10:47:59 +00:00
|
|
|
end
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
def ensure_snooze_until_reset
|
|
|
|
self.snoozed_until = nil unless snoozed?
|
|
|
|
end
|
|
|
|
|
2021-01-07 08:17:38 +00:00
|
|
|
def validate_additional_attributes
|
|
|
|
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
|
|
|
|
end
|
|
|
|
|
2021-07-21 16:32:43 +00:00
|
|
|
def mark_conversation_pending_if_bot
|
|
|
|
# TODO: make this an inbox config instead of assuming bot conversations should start as pending
|
|
|
|
self.status = :pending if inbox.agent_bot_inbox&.active? || inbox.hooks.pluck(:app_id).include?('dialogflow')
|
2020-05-01 09:23:43 +00:00
|
|
|
end
|
2019-10-12 18:08:41 +00:00
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
def notify_conversation_creation
|
|
|
|
dispatcher_dispatch(CONVERSATION_CREATED)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2022-02-15 18:06:29 +00:00
|
|
|
def notify_conversation_updation
|
2022-02-25 12:31:21 +00:00
|
|
|
return unless previous_changes.keys.present? && (previous_changes.keys & %w[team_id assignee_id status snoozed_until
|
|
|
|
custom_attributes]).present?
|
|
|
|
|
2022-03-30 06:07:36 +00:00
|
|
|
dispatcher_dispatch(CONVERSATION_UPDATED, previous_changes)
|
2022-02-15 18:06:29 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def self_assign?(assignee_id)
|
2019-10-12 18:08:41 +00:00
|
|
|
assignee_id.present? && Current.user&.id == assignee_id
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_display_id
|
2021-01-05 14:37:04 +00:00
|
|
|
reload
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_status_change
|
2019-10-12 18:08:41 +00:00
|
|
|
{
|
2020-04-18 14:55:58 +00:00
|
|
|
CONVERSATION_OPENED => -> { saved_change_to_status? && open? },
|
|
|
|
CONVERSATION_RESOLVED => -> { saved_change_to_status? && resolved? },
|
2021-04-28 15:23:23 +00:00
|
|
|
CONVERSATION_STATUS_CHANGED => -> { saved_change_to_status? },
|
2020-09-10 13:49:15 +00:00
|
|
|
CONVERSATION_READ => -> { saved_change_to_contact_last_seen_at? },
|
2020-06-02 17:29:02 +00:00
|
|
|
CONVERSATION_CONTACT_CHANGED => -> { saved_change_to_contact_id? }
|
2019-10-12 18:08:41 +00:00
|
|
|
}.each do |event, condition|
|
2022-03-30 06:07:36 +00:00
|
|
|
condition.call && dispatcher_dispatch(event, status_change)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-30 06:07:36 +00:00
|
|
|
def dispatcher_dispatch(event_name, changed_attributes = nil)
|
|
|
|
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self, notifiable_assignee_change: notifiable_assignee_change?,
|
2022-04-12 14:53:34 +00:00
|
|
|
changed_attributes: changed_attributes,
|
|
|
|
performed_by: Current.executed_by)
|
2019-10-05 09:12:50 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-06-02 18:20:39 +00:00
|
|
|
def conversation_status_changed_to_open?
|
|
|
|
return false unless open?
|
|
|
|
# saved_change_to_status? method only works in case of update
|
|
|
|
return true if previous_changes.key?(:id) || saved_change_to_status?
|
|
|
|
end
|
|
|
|
|
2020-10-02 11:03:59 +00:00
|
|
|
def create_label_change(user_name)
|
2020-11-01 07:23:25 +00:00
|
|
|
return unless user_name
|
|
|
|
|
2020-10-02 11:03:59 +00:00
|
|
|
previous_labels, current_labels = previous_changes[:label_list]
|
|
|
|
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)
|
|
|
|
|
|
|
|
create_label_added(user_name, current_labels - previous_labels)
|
|
|
|
create_label_removed(user_name, previous_labels - current_labels)
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def mute_key
|
2020-11-25 08:29:38 +00:00
|
|
|
format(Redis::RedisKeys::CONVERSATION_MUTE_KEY, id: id)
|
2020-05-26 12:13:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mute_period
|
|
|
|
6.hours
|
|
|
|
end
|
2021-01-05 14:37:04 +00:00
|
|
|
|
2022-03-30 09:06:22 +00:00
|
|
|
def validate_referer_url
|
|
|
|
return unless additional_attributes['referer']
|
|
|
|
|
|
|
|
self['additional_attributes']['referer'] = nil unless url_valid?(additional_attributes['referer'])
|
|
|
|
end
|
|
|
|
|
2021-01-05 14:37:04 +00:00
|
|
|
# creating db triggers
|
|
|
|
trigger.before(:insert).for_each(:row) do
|
|
|
|
"NEW.display_id := nextval('conv_dpid_seq_' || NEW.account_id);"
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|