2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: messages
|
|
|
|
#
|
2020-08-28 20:09:41 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# content :text
|
|
|
|
# content_attributes :json
|
2021-10-05 18:40:29 +00:00
|
|
|
# content_type :integer default("text"), not null
|
2020-08-28 20:09:41 +00:00
|
|
|
# external_source_ids :jsonb
|
|
|
|
# message_type :integer not null
|
|
|
|
# private :boolean default(FALSE)
|
|
|
|
# sender_type :string
|
|
|
|
# status :integer default("sent")
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# conversation_id :integer not null
|
|
|
|
# inbox_id :integer not null
|
|
|
|
# sender_id :bigint
|
|
|
|
# source_id :string
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2020-06-27 16:04:53 +00:00
|
|
|
# index_messages_on_account_id (account_id)
|
|
|
|
# index_messages_on_conversation_id (conversation_id)
|
|
|
|
# index_messages_on_inbox_id (inbox_id)
|
|
|
|
# index_messages_on_sender_type_and_sender_id (sender_type,sender_id)
|
|
|
|
# index_messages_on_source_id (source_id)
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Message < ApplicationRecord
|
2021-08-23 16:30:47 +00:00
|
|
|
include MessageFilterHelpers
|
2020-04-17 15:45:20 +00:00
|
|
|
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
|
|
|
|
|
2021-10-05 18:40:29 +00:00
|
|
|
before_validation :ensure_content_type
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :inbox_id, presence: true
|
|
|
|
validates :conversation_id, presence: true
|
2020-04-10 11:12:37 +00:00
|
|
|
validates_with ContentAttributeValidator
|
2021-10-05 18:40:29 +00:00
|
|
|
validates :content_type, presence: true
|
2022-02-02 22:21:17 +00:00
|
|
|
validates :content, length: { maximum: 150_000 }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-09-13 10:46:52 +00:00
|
|
|
# when you have a temperory id in your frontend and want it echoed back via action cable
|
|
|
|
attr_accessor :echo_id
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 }
|
2020-04-10 11:12:37 +00:00
|
|
|
enum content_type: {
|
|
|
|
text: 0,
|
|
|
|
input_text: 1,
|
|
|
|
input_textarea: 2,
|
|
|
|
input_email: 3,
|
|
|
|
input_select: 4,
|
|
|
|
cards: 5,
|
|
|
|
form: 6,
|
2020-04-30 14:50:26 +00:00
|
|
|
article: 7,
|
2021-06-22 07:31:19 +00:00
|
|
|
incoming_email: 8,
|
|
|
|
input_csat: 9
|
2020-04-10 11:12:37 +00:00
|
|
|
}
|
2020-01-09 07:36:40 +00:00
|
|
|
enum status: { sent: 0, delivered: 1, read: 2, failed: 3 }
|
2020-08-11 04:27:42 +00:00
|
|
|
# [:submitted_email, :items, :submitted_values] : Used for bot message types
|
|
|
|
# [:email] : Used by conversation_continuity incoming email messages
|
|
|
|
# [:in_reply_to] : Used to reply to a particular tweet in threads
|
2021-06-07 19:41:06 +00:00
|
|
|
# [:deleted] : Used to denote whether the message was deleted by the agent
|
|
|
|
# [:external_created_at] : Can specify if the message was created at a different timestamp externally
|
|
|
|
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted,
|
2022-03-10 14:57:30 +00:00
|
|
|
:external_created_at, :story_sender, :story_id], coder: JSON
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-08-28 20:09:41 +00:00
|
|
|
store :external_source_ids, accessors: [:slack], coder: JSON, prefix: :external_source_id
|
|
|
|
|
2019-10-12 18:08:41 +00:00
|
|
|
# .succ is a hack to avoid https://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be
|
|
|
|
scope :unread_since, ->(datetime) { where('EXTRACT(EPOCH FROM created_at) > (?)', datetime.to_i.succ) }
|
2020-05-10 07:44:57 +00:00
|
|
|
scope :chat, -> { where.not(message_type: :activity).where(private: false) }
|
2020-10-31 18:44:33 +00:00
|
|
|
scope :today, -> { where("date_trunc('day', created_at) = ?", Date.current) }
|
2019-08-14 09:48:44 +00:00
|
|
|
default_scope { order(created_at: :asc) }
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :inbox
|
2020-03-24 18:04:22 +00:00
|
|
|
belongs_to :conversation, touch: true
|
2020-06-27 16:04:53 +00:00
|
|
|
|
|
|
|
# FIXME: phase out user and contact after 1.4 since the info is there in sender
|
2019-08-14 09:48:44 +00:00
|
|
|
belongs_to :user, required: false
|
2020-02-26 04:14:24 +00:00
|
|
|
belongs_to :contact, required: false
|
2020-06-27 16:04:53 +00:00
|
|
|
belongs_to :sender, polymorphic: true, required: false
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :attachments, dependent: :destroy_async, autosave: true, before_add: :validate_attachments_limit
|
|
|
|
has_one :csat_survey_response, dependent: :destroy_async
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
after_create_commit :execute_after_create_commit_callbacks
|
2020-04-29 20:11:13 +00:00
|
|
|
|
2021-07-14 04:50:06 +00:00
|
|
|
after_update_commit :dispatch_update_event
|
2020-04-10 11:12:37 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def channel_token
|
|
|
|
@token ||= inbox.channel.try(:page_access_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def push_event_data
|
2019-08-30 22:38:00 +00:00
|
|
|
data = attributes.merge(
|
|
|
|
created_at: created_at.to_i,
|
|
|
|
message_type: message_type_before_type_cast,
|
2021-11-12 10:47:59 +00:00
|
|
|
conversation_id: conversation.display_id,
|
|
|
|
conversation: { assignee_id: conversation.assignee_id }
|
2019-08-30 22:38:00 +00:00
|
|
|
)
|
2020-09-13 10:46:52 +00:00
|
|
|
data.merge!(echo_id: echo_id) if echo_id.present?
|
2020-04-17 15:45:20 +00:00
|
|
|
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
|
2020-09-13 10:46:52 +00:00
|
|
|
merge_sender_attributes(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_sender_attributes(data)
|
2020-06-27 16:04:53 +00:00
|
|
|
data.merge!(sender: sender.push_event_data) if sender && !sender.is_a?(AgentBot)
|
2021-08-03 14:41:52 +00:00
|
|
|
data.merge!(sender: sender.push_event_data(inbox)) if sender.is_a?(AgentBot)
|
2019-08-14 09:48:44 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
content: content,
|
|
|
|
created_at: created_at,
|
|
|
|
message_type: message_type,
|
2020-04-10 11:12:37 +00:00
|
|
|
content_type: content_type,
|
2020-08-25 17:34:02 +00:00
|
|
|
private: private,
|
2020-04-10 11:12:37 +00:00
|
|
|
content_attributes: content_attributes,
|
2020-02-26 04:14:24 +00:00
|
|
|
source_id: source_id,
|
2020-06-27 16:04:53 +00:00
|
|
|
sender: sender.try(:webhook_data),
|
2020-02-26 04:14:24 +00:00
|
|
|
inbox: inbox.webhook_data,
|
|
|
|
conversation: conversation.webhook_data,
|
|
|
|
account: account.webhook_data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-08-23 16:30:47 +00:00
|
|
|
def content
|
|
|
|
# move this to a presenter
|
|
|
|
return self[:content] if !input_csat? || inbox.web_widget?
|
|
|
|
|
|
|
|
I18n.t('conversations.survey.response', link: "#{ENV['FRONTEND_URL']}/survey/responses/#{conversation.uuid}")
|
|
|
|
end
|
|
|
|
|
2022-02-15 11:41:28 +00:00
|
|
|
def email_notifiable_message?
|
2022-02-15 12:45:45 +00:00
|
|
|
return false if private?
|
|
|
|
return false if %w[outgoing template].exclude?(message_type)
|
2022-02-15 11:41:28 +00:00
|
|
|
return false if template? && %w[input_csat text].exclude?(content_type)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2021-10-05 18:40:29 +00:00
|
|
|
def ensure_content_type
|
|
|
|
self.content_type ||= Message.content_types[:text]
|
|
|
|
end
|
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
def execute_after_create_commit_callbacks
|
2021-01-17 17:13:32 +00:00
|
|
|
# rails issue with order of active record callbacks being executed https://github.com/rails/rails/issues/20911
|
2021-08-23 16:30:47 +00:00
|
|
|
reopen_conversation
|
|
|
|
notify_via_mail
|
2020-10-05 17:22:43 +00:00
|
|
|
set_conversation_activity
|
2020-06-09 18:24:35 +00:00
|
|
|
dispatch_create_events
|
|
|
|
send_reply
|
|
|
|
execute_message_template_hooks
|
2021-05-13 08:02:19 +00:00
|
|
|
update_contact_activity
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_contact_activity
|
2021-08-03 14:41:52 +00:00
|
|
|
sender.update(last_activity_at: DateTime.now) if sender.is_a?(Contact)
|
2020-06-09 18:24:35 +00:00
|
|
|
end
|
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
def dispatch_create_events
|
2019-10-30 05:13:11 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self)
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
if outgoing? && conversation.messages.outgoing.count == 1
|
2019-10-12 18:08:41 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
def dispatch_update_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(MESSAGE_UPDATED, Time.zone.now, message: self)
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def send_reply
|
2021-04-13 08:39:51 +00:00
|
|
|
# FIXME: Giving it few seconds for the attachment to be uploaded to the service
|
|
|
|
# active storage attaches the file only after commit
|
|
|
|
attachments.blank? ? ::SendReplyJob.perform_later(id) : ::SendReplyJob.set(wait: 2.seconds).perform_later(id)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reopen_conversation
|
2021-07-23 09:54:07 +00:00
|
|
|
return if conversation.muted?
|
|
|
|
return unless incoming?
|
|
|
|
|
|
|
|
conversation.open! if conversation.resolved? || conversation.snoozed?
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
|
|
|
|
def execute_message_template_hooks
|
|
|
|
::MessageTemplates::HookExecutionService.new(message: self).perform
|
|
|
|
end
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2022-01-26 23:59:48 +00:00
|
|
|
def email_notifiable_webwidget?
|
|
|
|
inbox.web_widget? && inbox.channel.continuity_via_email
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_notifiable_channel?
|
|
|
|
email_notifiable_webwidget? || %w[Email].include?(inbox.inbox_type)
|
|
|
|
end
|
|
|
|
|
2020-09-10 13:49:15 +00:00
|
|
|
def can_notify_via_mail?
|
|
|
|
return unless email_notifiable_message?
|
2022-01-26 23:59:48 +00:00
|
|
|
return unless email_notifiable_channel?
|
|
|
|
return if conversation.contact.email.blank?
|
2020-09-10 13:49:15 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-01-23 17:29:07 +00:00
|
|
|
def notify_via_mail
|
2020-09-10 13:49:15 +00:00
|
|
|
return unless can_notify_via_mail?
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2021-01-17 17:13:32 +00:00
|
|
|
trigger_notify_via_mail
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger_notify_via_mail
|
2021-10-14 07:25:46 +00:00
|
|
|
return EmailReplyWorker.perform_in(1.second, id) if inbox.inbox_type == 'Email'
|
|
|
|
|
2021-01-17 17:13:32 +00:00
|
|
|
# will set a redis key for the conversation so that we don't need to send email for every new message
|
2020-09-10 13:49:15 +00:00
|
|
|
# last few messages coupled together is sent every 2 minutes rather than one email for each message
|
2021-01-17 17:13:32 +00:00
|
|
|
# if redis key exists there is an unprocessed job that will take care of delivering the email
|
|
|
|
return if Redis::Alfred.get(conversation_mail_key).present?
|
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
Redis::Alfred.setex(conversation_mail_key, id)
|
|
|
|
ConversationReplyEmailWorker.perform_in(2.minutes, conversation.id, id)
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
2020-04-17 15:45:20 +00:00
|
|
|
|
2020-07-07 18:44:07 +00:00
|
|
|
def conversation_mail_key
|
|
|
|
format(::Redis::Alfred::CONVERSATION_MAILER_KEY, conversation_id: conversation.id)
|
|
|
|
end
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
def validate_attachments_limit(_attachment)
|
|
|
|
errors.add(attachments: 'exceeded maximum allowed') if attachments.size >= NUMBER_OF_PERMITTED_ATTACHMENTS
|
|
|
|
end
|
2020-10-05 17:22:43 +00:00
|
|
|
|
|
|
|
def set_conversation_activity
|
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
conversation.update_columns(last_activity_at: created_at)
|
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|