2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: conversations
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# additional_attributes :jsonb
|
|
|
|
# agent_last_seen_at :datetime
|
2020-09-10 13:49:15 +00:00
|
|
|
# contact_last_seen_at :datetime
|
2020-06-12 17:42:47 +00:00
|
|
|
# identifier :string
|
2020-10-05 17:22:43 +00:00
|
|
|
# last_activity_at :datetime not null
|
2019-11-30 13:39:55 +00:00
|
|
|
# locked :boolean default(FALSE)
|
|
|
|
# status :integer default("open"), not null
|
2020-04-30 14:50:26 +00:00
|
|
|
# uuid :uuid not null
|
2019-11-30 13:39:55 +00:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# assignee_id :integer
|
|
|
|
# contact_id :bigint
|
2020-01-09 07:36:40 +00:00
|
|
|
# contact_inbox_id :bigint
|
2019-11-30 13:39:55 +00:00
|
|
|
# display_id :integer not null
|
|
|
|
# inbox_id :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_conversations_on_account_id (account_id)
|
|
|
|
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
|
2020-01-09 07:36:40 +00:00
|
|
|
# index_conversations_on_contact_inbox_id (contact_inbox_id)
|
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (contact_inbox_id => contact_inboxes.id)
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Conversation < ApplicationRecord
|
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :inbox_id, presence: true
|
|
|
|
|
2020-03-05 20:13:12 +00:00
|
|
|
enum status: { open: 0, resolved: 1, bot: 2 }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-10-05 17:22:43 +00:00
|
|
|
scope :latest, -> { order(last_activity_at: :desc) }
|
2019-08-14 09:48:44 +00:00
|
|
|
scope :unassigned, -> { where(assignee_id: nil) }
|
2019-10-08 19:45:04 +00:00
|
|
|
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
has_many :messages, dependent: :destroy, autosave: true
|
|
|
|
|
2020-03-05 20:13:12 +00:00
|
|
|
before_create :set_bot_conversation
|
2020-09-08 05:54:08 +00:00
|
|
|
before_create :set_display_id, unless: :display_id?
|
2020-08-06 09:51:06 +00:00
|
|
|
# wanted to change this to after_update commit. But it ended up creating a loop
|
|
|
|
# reinvestigate in future and identity the implications
|
2020-05-01 09:23:43 +00:00
|
|
|
after_update :notify_status_change, :create_activity
|
2020-09-08 05:54:08 +00:00
|
|
|
after_create_commit :notify_conversation_creation
|
|
|
|
after_save :run_round_robin
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
acts_as_taggable_on :labels
|
|
|
|
|
2020-07-25 17:24:45 +00:00
|
|
|
def can_reply?
|
|
|
|
return true unless inbox&.channel&.has_24_hour_messaging_window?
|
|
|
|
|
|
|
|
last_incoming_message = messages.incoming.last
|
|
|
|
|
|
|
|
return false if last_incoming_message.nil?
|
|
|
|
|
|
|
|
Time.current < last_incoming_message.created_at + 24.hours
|
|
|
|
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
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def update_labels(labels = nil)
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(label_list: labels)
|
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
|
2020-03-10 18:32:15 +00:00
|
|
|
self.status = :open if bot?
|
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)
|
|
|
|
end
|
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
def unmute!
|
|
|
|
Redis::Alfred.delete(mute_key)
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def muted?
|
|
|
|
!Redis::Alfred.get(mute_key).nil?
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def lock!
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(locked: true)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unlock!
|
2019-10-12 18:08:41 +00:00
|
|
|
update!(locked: false)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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 if self_assign?(assignee_id)
|
|
|
|
return false unless saved_change_to_assignee_id?
|
|
|
|
return false if assignee_id.blank?
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_bot_conversation
|
|
|
|
self.status = :bot if inbox.agent_bot_inbox&.active?
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
self.display_id = loop do
|
2019-10-12 18:08:41 +00:00
|
|
|
next_display_id = account.conversations.maximum('display_id').to_i + 1
|
|
|
|
break next_display_id unless account.conversations.exists?(display_id: next_display_id)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_activity
|
2019-10-02 13:03:07 +00:00
|
|
|
return unless Current.user
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-10-13 07:33:55 +00:00
|
|
|
user_name = Current.user.name
|
2019-10-08 19:45:04 +00:00
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
create_status_change_message(user_name) if saved_change_to_status?
|
2019-10-12 18:08:41 +00:00
|
|
|
create_assignee_change(user_name) if saved_change_to_assignee_id?
|
2020-10-02 11:03:59 +00:00
|
|
|
create_label_change(user_name) if saved_change_to_label_list?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-08 19:45:04 +00:00
|
|
|
def activity_message_params(content)
|
2019-10-12 18:08:41 +00:00
|
|
|
{ account_id: account_id, inbox_id: inbox_id, message_type: :activity, content: content }
|
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? },
|
2020-09-10 13:49:15 +00:00
|
|
|
CONVERSATION_READ => -> { saved_change_to_contact_last_seen_at? },
|
2019-10-12 18:08:41 +00:00
|
|
|
CONVERSATION_LOCK_TOGGLE => -> { saved_change_to_locked? },
|
2020-06-02 17:29:02 +00:00
|
|
|
ASSIGNEE_CHANGED => -> { saved_change_to_assignee_id? },
|
|
|
|
CONVERSATION_CONTACT_CHANGED => -> { saved_change_to_contact_id? }
|
2019-10-12 18:08:41 +00:00
|
|
|
}.each do |event, condition|
|
|
|
|
condition.call && dispatcher_dispatch(event)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-05 09:12:50 +00:00
|
|
|
def dispatcher_dispatch(event_name)
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self)
|
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 should_round_robin?
|
|
|
|
return false unless inbox.enable_auto_assignment?
|
|
|
|
|
|
|
|
# run only if assignee is blank or doesn't have access to inbox
|
|
|
|
assignee.blank? || inbox.members.exclude?(assignee)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def run_round_robin
|
2020-06-02 18:20:39 +00:00
|
|
|
# Round robin kicks in on conversation create & update
|
|
|
|
# run it only when conversation status changes to open
|
|
|
|
return unless conversation_status_changed_to_open?
|
|
|
|
return unless should_round_robin?
|
2019-10-08 19:45:04 +00:00
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
::RoundRobin::AssignmentService.new(conversation: self).perform
|
2019-10-08 19:45:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_status_change_message(user_name)
|
2019-10-12 18:08:41 +00:00
|
|
|
content = I18n.t("conversations.activity.status.#{status}", user_name: user_name)
|
2019-10-08 19:45:04 +00:00
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
def create_assignee_change(user_name)
|
2020-10-13 18:46:35 +00:00
|
|
|
params = { assignee_name: assignee.name, user_name: user_name }.compact
|
2019-10-12 18:08:41 +00:00
|
|
|
key = assignee_id ? 'assigned' : 'removed'
|
2020-10-01 19:11:23 +00:00
|
|
|
key = 'self_assigned' if self_assign? assignee_id
|
2020-05-08 06:43:23 +00:00
|
|
|
content = I18n.t("conversations.activity.assignee.#{key}", **params)
|
2019-10-08 19:45:04 +00:00
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
2020-05-26 12:13:59 +00:00
|
|
|
|
2020-10-02 11:03:59 +00:00
|
|
|
def create_label_change(user_name)
|
|
|
|
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
|
|
|
|
|
|
|
|
def create_label_added(user_name, labels = [])
|
|
|
|
return unless labels.size.positive?
|
|
|
|
|
|
|
|
params = { user_name: user_name, labels: labels.join(', ') }
|
|
|
|
content = I18n.t('conversations.activity.labels.added', **params)
|
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_label_removed(user_name, labels = [])
|
|
|
|
return unless labels.size.positive?
|
|
|
|
|
|
|
|
params = { user_name: user_name, labels: labels.join(', ') }
|
|
|
|
content = I18n.t('conversations.activity.labels.removed', **params)
|
|
|
|
|
|
|
|
messages.create(activity_message_params(content))
|
|
|
|
end
|
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
def mute_key
|
|
|
|
format('CONVERSATION::%<id>d::MUTED', id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mute_period
|
|
|
|
6.hours
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|