2019-08-14 09:48:44 +00:00
|
|
|
class Message < ApplicationRecord
|
|
|
|
include Events::Types
|
|
|
|
|
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :inbox_id, presence: true
|
|
|
|
validates :conversation_id, presence: true
|
|
|
|
|
|
|
|
enum message_type: [ :incoming, :outgoing, :activity ]
|
|
|
|
enum status: [ :sent, :delivered, :read, :failed ]
|
|
|
|
|
|
|
|
scope :chat, -> { where.not(message_type: :activity, private: true) }
|
|
|
|
default_scope { order(created_at: :asc) }
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :inbox
|
|
|
|
belongs_to :conversation
|
|
|
|
belongs_to :user, required: false
|
|
|
|
|
|
|
|
has_one :attachment, dependent: :destroy, autosave: true
|
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
after_create :reopen_conversation,
|
2019-08-14 09:48:44 +00:00
|
|
|
:dispatch_event,
|
2019-08-30 22:38:00 +00:00
|
|
|
:send_reply
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
data.merge!(attachment: attachment.push_event_data) if self.attachment
|
|
|
|
data.merge!(sender: user.push_event_data) if self.user
|
2019-08-14 09:48:44 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dispatch_event
|
|
|
|
$dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self) unless self.conversation.messages.count == 1
|
|
|
|
|
|
|
|
if outgoing? && self.conversation.messages.outgoing.count == 1
|
|
|
|
$dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_reply
|
2019-08-30 22:38:00 +00:00
|
|
|
::Facebook::SendReplyService.new(message: self).perform
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reopen_conversation
|
|
|
|
if incoming? && self.conversation.resolved?
|
|
|
|
self.conversation.toggle_status
|
|
|
|
$dispatcher.dispatch(CONVERSATION_REOPENED, Time.zone.now, conversation: self.conversation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|