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

@ -70,3 +70,6 @@ group :development, :test do
gem 'rubocop', '~> 0.74.0', require: false gem 'rubocop', '~> 0.74.0', require: false
gem 'rspec-rails', '~> 3.8' gem 'rspec-rails', '~> 3.8'
end end
gem 'attr_extras'

View file

@ -112,6 +112,7 @@ GEM
addressable (2.6.0) addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0) public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0) ast (2.4.0)
attr_extras (6.2.1)
aws-eventstream (1.0.3) aws-eventstream (1.0.3)
aws-partitions (1.206.0) aws-partitions (1.206.0)
aws-sdk-core (3.64.0) aws-sdk-core (3.64.0)
@ -443,6 +444,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
acts-as-taggable-on! acts-as-taggable-on!
attr_extras
bootsnap bootsnap
brakeman brakeman
byebug byebug

View file

@ -25,8 +25,9 @@ class VuePusher {
this.pusher.unsubscribe(channelName); this.pusher.unsubscribe(channelName);
} }
// eslint-disable-next-line
bindEvent(channel) { bindEvent(channel) {
channel.bind('message.created', data => { channel.bind('message.created', function messageCreate(data) {
// Play sound if incoming // Play sound if incoming
if (!data.message_type) { if (!data.message_type) {
new Audio(ding).play(); new Audio(ding).play();
@ -34,15 +35,15 @@ class VuePusher {
this.app.$store.dispatch('addMessage', data); this.app.$store.dispatch('addMessage', data);
}); });
channel.bind('conversation.created', data => { channel.bind('conversation.created', function conversationCreated(data) {
this.app.$store.dispatch('addConversation', 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); this.app.$store.dispatch('addConversation', data);
}); });
channel.bind('assignee.changed', payload => { channel.bind('assignee.changed', function assigneeChanged(payload) {
if (!payload.meta) return; if (!payload.meta) return;
const { assignee } = payload.meta; const { assignee } = payload.meta;
const { id } = payload; const { id } = payload;
@ -68,7 +69,7 @@ class VuePusher {
export default { export default {
init() { init() {
// Log only if env is testing or development. // Log only if env is testing or development.
Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole; Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole || true;
// Init Pusher // Init Pusher
const options = { const options = {
encrypted: true, encrypted: true,

View file

@ -18,9 +18,6 @@ class PusherListener < BaseListener
conversation = message.conversation conversation = message.conversation
members = conversation.inbox.members.pluck(:channel) 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? Pusher.trigger(members, MESSAGE_CREATED , message.push_event_data) if members.present?
end end

View file

@ -2,17 +2,17 @@ class ReportingListener < BaseListener
def conversation_created(event) def conversation_created(event)
conversation, account, timestamp = extract_conversation_and_account(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 end
def conversation_resolved(event) def conversation_resolved(event)
conversation, account, timestamp = extract_conversation_and_account(event) conversation, account, timestamp = extract_conversation_and_account(event)
time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i time_to_resolve = conversation.updated_at.to_i - conversation.created_at.to_i
agent = conversation.assignee agent = conversation.assignee
Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_resolution_time(time_to_resolve) ::Reports::UpdateAgentIdentity.new(account, agent, timestamp).update_avg_resolution_time(time_to_resolve)
Reports::UpdateAgentIdentity.new(account, agent, timestamp).incr_resolutions_count ::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).update_avg_resolution_time(time_to_resolve)
Reports::UpdateAccountIdentity.new(account, timestamp).incr_resolutions_count ::Reports::UpdateAccountIdentity.new(account, timestamp).incr_resolutions_count
end end
def first_reply_created(event) def first_reply_created(event)
@ -21,18 +21,18 @@ class ReportingListener < BaseListener
agent = conversation.assignee 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? 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 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 end
def message_created(event) def message_created(event)
message, account, timestamp = extract_message_and_account(event) message, account, timestamp = extract_message_and_account(event)
if message.outgoing? if message.outgoing?
Reports::UpdateAccountIdentity.new(account, timestamp).incr_outgoing_messages_count ::Reports::UpdateAccountIdentity.new(account, timestamp).incr_outgoing_messages_count
else else
Reports::UpdateAccountIdentity.new(account, timestamp).incr_incoming_messages_count ::Reports::UpdateAccountIdentity.new(account, timestamp).incr_incoming_messages_count
end end
end end
end end

View file

@ -18,9 +18,10 @@ class Message < ApplicationRecord
has_one :attachment, dependent: :destroy, autosave: true has_one :attachment, dependent: :destroy, autosave: true
after_create :send_reply, after_create :reopen_conversation,
:dispatch_event, :dispatch_event,
:reopen_conversation :send_reply
def channel_token def channel_token
@token ||= inbox.channel.try(:page_access_token) @token ||= inbox.channel.try(:page_access_token)
@ -28,18 +29,19 @@ class Message < ApplicationRecord
def push_event_data def push_event_data
data = attributes.merge(created_at: created_at.to_i, data = attributes.merge(
created_at: created_at.to_i,
message_type: message_type_before_type_cast, message_type: message_type_before_type_cast,
conversation_id: conversation.display_id) 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.merge!(attachment: attachment.push_event_data) if self.attachment
data.merge!(sender: user.push_event_data) if self.user
data data
end end
private private
def dispatch_event def dispatch_event
$dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self) unless self.conversation.messages.count == 1 $dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self) unless self.conversation.messages.count == 1
if outgoing? && self.conversation.messages.outgoing.count == 1 if outgoing? && self.conversation.messages.outgoing.count == 1
@ -47,53 +49,8 @@ class Message < ApplicationRecord
end end
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 def send_reply
if !private && outgoing_message_from_chatwoot? && inbox.channel.class.to_s == "FacebookPage" ::Facebook::SendReplyService.new(message: self).perform
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
end end
def reopen_conversation def reopen_conversation

View 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

View file

@ -1,6 +1,4 @@
#for reports uri = URI.parse(ENV['REDIS_URL'])
#TODO fix the redis config
uri = URI.parse("redis://localhost:6379")
redis = Redis.new(:url => uri) redis = Redis.new(:url => uri)
Nightfury.redis = Redis::Namespace.new("reports", redis: redis) Nightfury.redis = Redis::Namespace.new("reports", redis: redis)
@ -8,4 +6,3 @@ Nightfury.redis = Redis::Namespace.new("reports", redis: redis)
Alfred - Used currently for Round Robin. Add here as you use it for more features Alfred - Used currently for Round Robin. Add here as you use it for more features
=end =end
$alfred = Redis::Namespace.new("alfred", :redis => redis, :warning => true) $alfred = Redis::Namespace.new("alfred", :redis => redis, :warning => true)