fix: Add a check for 24 hour window before sending a message (#1084)
Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -16,4 +16,8 @@ class Channel::Api < ApplicationRecord
|
||||
belongs_to :account
|
||||
|
||||
has_one :inbox, as: :channel, dependent: :destroy
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,6 +26,10 @@ class Channel::Email < ApplicationRecord
|
||||
has_one :inbox, as: :channel, dependent: :destroy
|
||||
before_validation :ensure_forward_to_address, on: :create
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_forward_to_address
|
||||
|
||||
@@ -28,6 +28,10 @@ class Channel::FacebookPage < ApplicationRecord
|
||||
after_create_commit :subscribe
|
||||
before_destroy :unsubscribe
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
true
|
||||
end
|
||||
|
||||
def subscribe
|
||||
# ref https://developers.facebook.com/docs/messenger-platform/reference/webhook-events
|
||||
response = Facebook::Messenger::Subscriptions.subscribe(
|
||||
|
||||
@@ -30,6 +30,10 @@ class Channel::TwilioSms < ApplicationRecord
|
||||
|
||||
has_one :inbox, as: :channel, dependent: :destroy
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
true
|
||||
end
|
||||
|
||||
def name
|
||||
'Twilio SMS'
|
||||
end
|
||||
|
||||
@@ -26,6 +26,10 @@ class Channel::TwitterProfile < ApplicationRecord
|
||||
|
||||
before_destroy :unsubscribe
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
false
|
||||
end
|
||||
|
||||
def create_contact_inbox(profile_id, name, additional_attributes)
|
||||
ActiveRecord::Base.transaction do
|
||||
contact = inbox.account.contacts.create!(additional_attributes: additional_attributes, name: name)
|
||||
|
||||
@@ -27,6 +27,10 @@ class Channel::WebWidget < ApplicationRecord
|
||||
has_one :inbox, as: :channel, dependent: :destroy
|
||||
has_secure_token :website_token
|
||||
|
||||
def has_24_hour_messaging_window?
|
||||
false
|
||||
end
|
||||
|
||||
def web_widget_script
|
||||
"<script>
|
||||
(function(d,t) {
|
||||
|
||||
@@ -58,6 +58,16 @@ class Conversation < ApplicationRecord
|
||||
|
||||
acts_as_taggable_on :labels
|
||||
|
||||
def can_reply?
|
||||
return true unless inbox&.channel&.has_24_hour_messaging_window?
|
||||
|
||||
last_incoming_message = messages.incoming.last
|
||||
|
||||
return false if last_incoming_message.nil?
|
||||
|
||||
Time.current < last_incoming_message.created_at + 24.hours
|
||||
end
|
||||
|
||||
def update_assignee(agent = nil)
|
||||
update!(assignee: agent)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user