/
conversation.rb
257 lines (207 loc) 路 7.9 KB
/
conversation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# == Schema Information
#
# Table name: conversations
#
# 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
# 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
#
# Indexes
#
# 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)
# index_conversations_on_status_and_account_id (status,account_id)
# index_conversations_on_team_id (team_id)
#
# Foreign Keys
#
# fk_rails_... (campaign_id => campaigns.id) ON DELETE => cascade
# fk_rails_... (contact_inbox_id => contact_inboxes.id) ON DELETE => cascade
# fk_rails_... (team_id => teams.id) ON DELETE => cascade
#
class Conversation < ApplicationRecord
include Labelable
include AssignmentHandler
include RoundRobinHandler
include ActivityMessageHandler
include UrlHelper
validates :account_id, presence: true
validates :inbox_id, presence: true
before_validation :validate_additional_attributes
validates :additional_attributes, jsonb_attributes_length: true
validates :custom_attributes, jsonb_attributes_length: true
validate :validate_referer_url
enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 }
scope :latest, -> { order(last_activity_at: :desc) }
scope :unassigned, -> { where(assignee_id: nil) }
scope :assigned, -> { where.not(assignee_id: nil) }
scope :assigned_to, ->(agent) { where(assignee_id: agent.id) }
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)
}
belongs_to :account
belongs_to :inbox
belongs_to :assignee, class_name: 'User', optional: true
belongs_to :contact
belongs_to :contact_inbox
belongs_to :team, optional: true
belongs_to :campaign, optional: true
has_many :mentions, dependent: :destroy_async
has_many :messages, dependent: :destroy_async, autosave: true
has_one :csat_survey_response, dependent: :destroy_async
has_many :notifications, as: :primary_actor, dependent: :destroy
before_save :ensure_snooze_until_reset
before_create :mark_conversation_pending_if_bot
after_update_commit :execute_after_update_commit_callbacks
after_create_commit :notify_conversation_creation
after_commit :set_display_id, unless: :display_id?
delegate :auto_resolve_duration, to: :account
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
def update_assignee(agent = nil)
update!(assignee: agent)
end
def toggle_status
# FIXME: implement state machine with aasm
self.status = open? ? :resolved : :open
self.status = :open if pending? || snoozed?
save
end
def mute!
resolved!
Redis::Alfred.setex(mute_key, 1, mute_period)
create_muted_message
end
def unmute!
Redis::Alfred.delete(mute_key)
create_unmuted_message
end
def muted?
Redis::Alfred.get(mute_key).present?
end
def unread_messages
messages.unread_since(agent_last_seen_at)
end
def unread_incoming_messages
messages.incoming.unread_since(agent_last_seen_at)
end
def push_event_data
Conversations::EventDataPresenter.new(self).push_data
end
def lock_event_data
Conversations::EventDataPresenter.new(self).lock_data
end
def webhook_data
Conversations::EventDataPresenter.new(self).push_data
end
def notifiable_assignee_change?
return false unless saved_change_to_assignee_id?
return false if assignee_id.blank?
return false if self_assign?(assignee_id)
true
end
def tweet?
inbox.inbox_type == 'Twitter' && additional_attributes['type'] == 'tweet'
end
def recent_messages
messages.chat.last(5)
end
private
def execute_after_update_commit_callbacks
notify_status_change
create_activity
notify_conversation_updation
end
def ensure_snooze_until_reset
self.snoozed_until = nil unless snoozed?
end
def validate_additional_attributes
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
end
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')
end
def notify_conversation_creation
dispatcher_dispatch(CONVERSATION_CREATED)
end
def notify_conversation_updation
return unless previous_changes.keys.present? && (previous_changes.keys & %w[team_id assignee_id status snoozed_until
custom_attributes]).present?
dispatcher_dispatch(CONVERSATION_UPDATED, previous_changes)
end
def self_assign?(assignee_id)
assignee_id.present? && Current.user&.id == assignee_id
end
def set_display_id
reload
end
def notify_status_change
{
CONVERSATION_OPENED => -> { saved_change_to_status? && open? },
CONVERSATION_RESOLVED => -> { saved_change_to_status? && resolved? },
CONVERSATION_STATUS_CHANGED => -> { saved_change_to_status? },
CONVERSATION_READ => -> { saved_change_to_contact_last_seen_at? },
CONVERSATION_CONTACT_CHANGED => -> { saved_change_to_contact_id? }
}.each do |event, condition|
condition.call && dispatcher_dispatch(event, status_change)
end
end
def dispatcher_dispatch(event_name, changed_attributes = nil)
return if Current.executed_by.present? && Current.executed_by.instance_of?(AutomationRule)
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, conversation: self, notifiable_assignee_change: notifiable_assignee_change?,
changed_attributes: changed_attributes)
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
def create_label_change(user_name)
return unless 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 mute_key
format(Redis::RedisKeys::CONVERSATION_MUTE_KEY, id: id)
end
def mute_period
6.hours
end
def validate_referer_url
return unless additional_attributes['referer']
self['additional_attributes']['referer'] = nil unless url_valid?(additional_attributes['referer'])
end
# creating db triggers
trigger.before(:insert).for_each(:row) do
"NEW.display_id := nextval('conv_dpid_seq_' || NEW.account_id);"
end
end