2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: messages
|
|
|
|
#
|
2020-01-09 07:36:40 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# content :text
|
|
|
|
# content_attributes :json
|
|
|
|
# content_type :integer default("text")
|
|
|
|
# message_type :integer not null
|
|
|
|
# private :boolean default(FALSE)
|
|
|
|
# status :integer default("sent")
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
2020-02-09 10:17:48 +00:00
|
|
|
# contact_id :bigint
|
2020-01-09 07:36:40 +00:00
|
|
|
# conversation_id :integer not null
|
|
|
|
# inbox_id :integer not null
|
2020-02-09 10:17:48 +00:00
|
|
|
# source_id :string
|
2020-01-09 07:36:40 +00:00
|
|
|
# user_id :integer
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2020-03-18 11:23:35 +00:00
|
|
|
# index_messages_on_account_id (account_id)
|
2020-02-09 10:17:48 +00:00
|
|
|
# index_messages_on_contact_id (contact_id)
|
2019-11-30 13:39:55 +00:00
|
|
|
# index_messages_on_conversation_id (conversation_id)
|
2020-03-18 11:23:35 +00:00
|
|
|
# index_messages_on_inbox_id (inbox_id)
|
2020-02-09 10:17:48 +00:00
|
|
|
# index_messages_on_source_id (source_id)
|
2020-03-18 11:23:35 +00:00
|
|
|
# index_messages_on_user_id (user_id)
|
2020-02-09 10:17:48 +00:00
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (contact_id => contacts.id)
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Message < ApplicationRecord
|
|
|
|
include Events::Types
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
|
|
|
|
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
|
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,
|
|
|
|
incoming_email: 8
|
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-04-30 14:50:26 +00:00
|
|
|
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email], coder: JSON
|
2019-08-14 09:48:44 +00:00
|
|
|
|
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) }
|
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
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
after_create :reopen_conversation,
|
2020-01-23 17:29:07 +00:00
|
|
|
:notify_via_mail
|
2019-08-30 22:38:00 +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
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
after_update :dispatch_update_event
|
|
|
|
|
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,
|
|
|
|
conversation_id: conversation.display_id
|
|
|
|
)
|
2020-04-17 15:45:20 +00:00
|
|
|
data.merge!(attachments: attachments.map(&:push_event_data)) if attachments.present?
|
2019-10-20 08:47:26 +00:00
|
|
|
data.merge!(sender: user.push_event_data) if user
|
2019-08-14 09:48:44 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2020-01-13 19:20:18 +00:00
|
|
|
def reportable?
|
|
|
|
incoming? || outgoing?
|
|
|
|
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,
|
|
|
|
content_attributes: content_attributes,
|
2020-02-26 04:14:24 +00:00
|
|
|
source_id: source_id,
|
|
|
|
sender: user.try(:webhook_data),
|
|
|
|
contact: contact.try(:webhook_data),
|
|
|
|
inbox: inbox.webhook_data,
|
|
|
|
conversation: conversation.webhook_data,
|
|
|
|
account: account.webhook_data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
def execute_after_create_commit_callbacks
|
|
|
|
# rails issue with order of active record callbacks being executed
|
|
|
|
# https://github.com/rails/rails/issues/20911
|
|
|
|
dispatch_create_events
|
|
|
|
send_reply
|
|
|
|
execute_message_template_hooks
|
|
|
|
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
|
2020-02-02 19:09:00 +00:00
|
|
|
channel_name = conversation.inbox.channel.class.to_s
|
|
|
|
if channel_name == 'Channel::FacebookPage'
|
2020-06-25 18:05:16 +00:00
|
|
|
::Facebook::SendOnFacebookService.new(message: self).perform
|
2020-02-02 19:09:00 +00:00
|
|
|
elsif channel_name == 'Channel::TwitterProfile'
|
2020-06-25 18:05:16 +00:00
|
|
|
::Twitter::SendOnTwitterService.new(message: self).perform
|
2020-04-05 16:41:27 +00:00
|
|
|
elsif channel_name == 'Channel::TwilioSms'
|
2020-06-25 18:05:16 +00:00
|
|
|
::Twilio::SendOnTwilioService.new(message: self).perform
|
2020-02-02 19:09:00 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reopen_conversation
|
2020-05-26 12:13:59 +00:00
|
|
|
conversation.open! if incoming? && conversation.resolved? && !conversation.muted?
|
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
|
|
|
|
|
|
|
def notify_via_mail
|
|
|
|
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % conversation.id
|
|
|
|
if Redis::Alfred.get(conversation_mail_key).nil? && conversation.contact.email? && outgoing?
|
|
|
|
# set a redis key for the conversation so that we don't need to send email for every
|
|
|
|
# new message that comes in and we dont enque the delayed sidekiq job for every message
|
|
|
|
Redis::Alfred.setex(conversation_mail_key, Time.zone.now)
|
|
|
|
|
|
|
|
# Since this is live chat, send the email after few minutes so the only one email with
|
|
|
|
# last few messages coupled together is sent rather than email for each message
|
2020-03-01 13:36:13 +00:00
|
|
|
ConversationReplyEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
|
|
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|