Cleanup message model, fix reporting listener issues

This commit is contained in:
Pranav Raj Sreepuram
2019-08-31 04:08:00 +05:30
parent cefd8a287a
commit f74a6593ef
8 changed files with 108 additions and 77 deletions

View File

@@ -18,9 +18,10 @@ class Message < ApplicationRecord
has_one :attachment, dependent: :destroy, autosave: true
after_create :send_reply,
after_create :reopen_conversation,
:dispatch_event,
:reopen_conversation
:send_reply
def channel_token
@token ||= inbox.channel.try(:page_access_token)
@@ -28,18 +29,19 @@ class Message < ApplicationRecord
def push_event_data
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
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
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
@@ -47,53 +49,8 @@ class Message < ApplicationRecord
end
end
def outgoing_message_from_chatwoot?
#messages sent directly from chatwoot won't have fb_id.
outgoing? && !fb_id
end
def reopen_lock
if incoming? && self.conversation.locked?
self.conversation.unlock!
end
end
def send_reply
if !private && outgoing_message_from_chatwoot? && inbox.channel.class.to_s == "FacebookPage"
Bot.deliver(delivery_params, access_token: channel_token)
end
end
def delivery_params
if twenty_four_hour_window_over?
{ recipient: {id: conversation.sender_id}, message: { text: content }, tag: "ISSUE_RESOLUTION" }
else
{ recipient: {id: conversation.sender_id}, message: { text: content }}
end
end
def twenty_four_hour_window_over?
#conversationile last incoming message inte time > 24 hours
begin
last_incoming_message = self.conversation.messages.incoming.last
is_after_24_hours = (Time.diff(last_incoming_message.try(:created_at) || Time.now, Time.now, '%h')[:diff]).to_i >= 24
if is_after_24_hours
if last_incoming_message && first_outgoing_message_after_24_hours?(last_incoming_message.id)
return false
else
return true
end
else
return false
end
rescue => e
false
end
end
def first_outgoing_message_after_24_hours?(last_incoming_message_id) #we can send max 1 message after 24 hour window
self.conversation.messages.outgoing.where("id > ?", last_incoming_message_id).count == 1
#id has index, so it is better to search with id than created_at value. Anyway id is also created in the same order as created_at
::Facebook::SendReplyService.new(message: self).perform
end
def reopen_conversation