-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup message model, fix reporting listener issues
- Loading branch information
1 parent
cefd8a2
commit f74a659
Showing
8 changed files
with
108 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
#for reports | ||
#TODO fix the redis config | ||
uri = URI.parse("redis://localhost:6379") | ||
uri = URI.parse(ENV['REDIS_URL']) | ||
redis = Redis.new(:url => uri) | ||
Nightfury.redis = Redis::Namespace.new("reports", redis: redis) | ||
|
||
=begin | ||
Alfred - Used currently for Round Robin. Add here as you use it for more features | ||
=end | ||
$alfred = Redis::Namespace.new("alfred", :redis => redis, :warning => true) | ||
|