2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: messages
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# content :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
|
|
|
|
# conversation_id :integer not null
|
|
|
|
# fb_id :string
|
|
|
|
# inbox_id :integer not null
|
|
|
|
# user_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_messages_on_conversation_id (conversation_id)
|
|
|
|
#
|
|
|
|
|
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
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
enum message_type: [:incoming, :outgoing, :activity]
|
|
|
|
enum status: [:sent, :delivered, :read, :failed]
|
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) }
|
2019-11-25 18:25:18 +00:00
|
|
|
scope :chat, -> { where.not(message_type: :activity).where.not(private: true) }
|
2019-08-14 09:48:44 +00:00
|
|
|
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
|
|
|
|
)
|
2019-10-20 08:47:26 +00:00
|
|
|
data.merge!(attachment: attachment.push_event_data) if attachment
|
|
|
|
data.merge!(sender: user.push_event_data) if user
|
2019-08-14 09:48:44 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dispatch_event
|
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
|
|
|
|
|
|
|
|
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
|
2019-10-20 08:47:26 +00:00
|
|
|
if incoming? && conversation.resolved?
|
|
|
|
conversation.toggle_status
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONVERSATION_REOPENED, Time.zone.now, conversation: conversation)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|