Cleanup message model, fix reporting listener issues
This commit is contained in:
@@ -25,8 +25,9 @@ class VuePusher {
|
||||
this.pusher.unsubscribe(channelName);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line
|
||||
bindEvent(channel) {
|
||||
channel.bind('message.created', data => {
|
||||
channel.bind('message.created', function messageCreate(data) {
|
||||
// Play sound if incoming
|
||||
if (!data.message_type) {
|
||||
new Audio(ding).play();
|
||||
@@ -34,15 +35,15 @@ class VuePusher {
|
||||
this.app.$store.dispatch('addMessage', data);
|
||||
});
|
||||
|
||||
channel.bind('conversation.created', data => {
|
||||
channel.bind('conversation.created', function conversationCreated(data) {
|
||||
this.app.$store.dispatch('addConversation', data);
|
||||
});
|
||||
|
||||
channel.bind('status_change:conversation', data => {
|
||||
channel.bind('status_change:conversation', function statusChange(data) {
|
||||
this.app.$store.dispatch('addConversation', data);
|
||||
});
|
||||
|
||||
channel.bind('assignee.changed', payload => {
|
||||
channel.bind('assignee.changed', function assigneeChanged(payload) {
|
||||
if (!payload.meta) return;
|
||||
const { assignee } = payload.meta;
|
||||
const { id } = payload;
|
||||
@@ -68,7 +69,7 @@ class VuePusher {
|
||||
export default {
|
||||
init() {
|
||||
// Log only if env is testing or development.
|
||||
Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole;
|
||||
Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole || true;
|
||||
// Init Pusher
|
||||
const options = {
|
||||
encrypted: true,
|
||||
|
||||
@@ -17,10 +17,7 @@ class PusherListener < BaseListener
|
||||
message, account, timestamp = extract_message_and_account(event)
|
||||
conversation = message.conversation
|
||||
members = conversation.inbox.members.pluck(:channel)
|
||||
|
||||
# widget_user = conversation.sender.chat_channel
|
||||
# members = members << widget_user
|
||||
|
||||
|
||||
Pusher.trigger(members, MESSAGE_CREATED , message.push_event_data) if members.present?
|
||||
end
|
||||
|
||||
|
||||
@@ -2,37 +2,37 @@ class ReportingListener < BaseListener
|
||||
|
||||
def conversation_created(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).incr_conversations_count
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_conversations_count
|
||||
end
|
||||
|
||||
def conversation_resolved(event)
|
||||
conversation, account, timestamp = extract_conversation_and_account(event)
|
||||
time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i
|
||||
agent = conversation.assignee
|
||||
Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
Reports::UpdateAgentIdentity.new(account, agent, timestamp).incr_resolutions_count
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).incr_resolutions_count
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).incr_resolutions_count
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_resolution_time(time_to_resolve)
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_resolutions_count
|
||||
end
|
||||
|
||||
def first_reply_created(event)
|
||||
message, account, timestamp = extract_message_and_account(event)
|
||||
conversation = message.conversation
|
||||
agent = conversation.assignee
|
||||
first_response_time = message.created_at.to_i - conversation.created_at.to_i
|
||||
first_response_time = message.created_at.to_i - conversation.created_at.to_i
|
||||
if agent.present?
|
||||
Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_first_response_time(first_response_time)
|
||||
::Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_first_response_time(first_response_time)
|
||||
end
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_first_response_time(first_response_time)
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).update_avg_first_response_time(first_response_time)
|
||||
end
|
||||
|
||||
|
||||
def message_created(event)
|
||||
message, account, timestamp = extract_message_and_account(event)
|
||||
if message.outgoing?
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).incr_outgoing_messages_count
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_outgoing_messages_count
|
||||
else
|
||||
Reports::UpdateAccountIdentity.new(account, timestamp).incr_incoming_messages_count
|
||||
::Reports::UpdateAccountIdentity.new(account, timestamp).incr_incoming_messages_count
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
74
app/services/facebook/send_reply_service.rb
Normal file
74
app/services/facebook/send_reply_service.rb
Normal file
@@ -0,0 +1,74 @@
|
||||
module Facebook
|
||||
class SendReplyService
|
||||
pattr_initialize [:message!]
|
||||
|
||||
def perform
|
||||
return if message.private
|
||||
return if inbox.channel.class.to_s != "FacebookPage"
|
||||
return if !outgoing_message_from_chatwoot?
|
||||
|
||||
Bot.deliver(delivery_params, access_token: message.channel_token)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def inbox
|
||||
@inbox ||= message.inbox
|
||||
end
|
||||
|
||||
def conversation
|
||||
@conversation ||= message.conversation
|
||||
end
|
||||
|
||||
def sender
|
||||
conversation.sender
|
||||
end
|
||||
|
||||
def outgoing_message_from_chatwoot?
|
||||
#messages sent directly from chatwoot won't have fb_id.
|
||||
message.outgoing? && !message.fb_id
|
||||
end
|
||||
|
||||
# def reopen_lock
|
||||
# if message.incoming? && conversation.locked?
|
||||
# conversation.unlock!
|
||||
# end
|
||||
# end
|
||||
|
||||
def fb_message_params
|
||||
{
|
||||
recipient: { id: sender.source_id },
|
||||
message: { text: message.content },
|
||||
}
|
||||
end
|
||||
|
||||
def delivery_params
|
||||
if twenty_four_hour_window_over?
|
||||
fb_message_params.merge(tag: "ISSUE_RESOLUTION")
|
||||
else
|
||||
fb_message_params
|
||||
end
|
||||
end
|
||||
|
||||
def twenty_four_hour_window_over?
|
||||
last_incoming_message = conversation.messages.incoming.last
|
||||
|
||||
is_after_24_hours = (Time.current - last_incoming_message.created_at) / 3600 >= 24
|
||||
|
||||
if !is_after_24_hours
|
||||
false
|
||||
end
|
||||
|
||||
if last_incoming_message && has_sent_first_outgoing_message_after_24_hours?(last_incoming_message.id)
|
||||
false
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def has_sent_first_outgoing_message_after_24_hours?(last_incoming_message_id)
|
||||
#we can send max 1 message after 24 hour window
|
||||
conversation.messages.outgoing.where("id > ?", last_incoming_message_id).count == 1
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user