chore: Centralize outgoing message reply restrictions for all the channels (#11279)
This commit is contained in:
@@ -218,8 +218,14 @@ export default {
|
||||
if (additionalAttributes) {
|
||||
const {
|
||||
agent_reply_time_window_message: agentReplyTimeWindowMessage,
|
||||
agent_reply_time_window: agentReplyTimeWindow,
|
||||
} = additionalAttributes;
|
||||
return agentReplyTimeWindowMessage;
|
||||
return (
|
||||
agentReplyTimeWindowMessage ||
|
||||
this.$t('CONVERSATION.API_HOURS_WINDOW', {
|
||||
hours: agentReplyTimeWindow,
|
||||
})
|
||||
);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
"LOADING_CONVERSATIONS": "Loading Conversations",
|
||||
"CANNOT_REPLY": "You cannot reply due to",
|
||||
"24_HOURS_WINDOW": "24 hour message window restriction",
|
||||
"API_HOURS_WINDOW": "You can only reply to this conversation within {hours} hours",
|
||||
"NOT_ASSIGNED_TO_YOU": "This conversation is not assigned to you. Would you like to assign this conversation to yourself?",
|
||||
"ASSIGN_TO_ME": "Assign to me",
|
||||
"TWILIO_WHATSAPP_CAN_REPLY": "You can only reply to this conversation using a template message due to",
|
||||
|
||||
@@ -33,10 +33,6 @@ class Channel::Api < ApplicationRecord
|
||||
'API'
|
||||
end
|
||||
|
||||
def messaging_window_enabled?
|
||||
additional_attributes.present? && additional_attributes['agent_reply_time_window'].present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_valid_agent_reply_time_window
|
||||
|
||||
@@ -32,10 +32,6 @@ class Channel::FacebookPage < ApplicationRecord
|
||||
'Facebook'
|
||||
end
|
||||
|
||||
def messaging_window_enabled?
|
||||
false
|
||||
end
|
||||
|
||||
def create_contact_inbox(instagram_id, name)
|
||||
@contact_inbox = ::ContactInboxWithContactBuilder.new({
|
||||
source_id: instagram_id,
|
||||
|
||||
@@ -41,10 +41,6 @@ class Channel::TwilioSms < ApplicationRecord
|
||||
medium == 'sms' ? 'Twilio SMS' : 'Whatsapp'
|
||||
end
|
||||
|
||||
def messaging_window_enabled?
|
||||
medium == 'whatsapp'
|
||||
end
|
||||
|
||||
def send_message(to:, body:, media_url: nil)
|
||||
params = send_message_from.merge(to: to, body: body)
|
||||
params[:media_url] = media_url if media_url.present?
|
||||
|
||||
@@ -46,10 +46,6 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def messaging_window_enabled?
|
||||
true
|
||||
end
|
||||
|
||||
def mark_message_templates_updated
|
||||
# rubocop:disable Rails/SkipsModelValidations
|
||||
update_column(:message_templates_last_updated, Time.zone.now)
|
||||
|
||||
@@ -7,10 +7,6 @@ module Channelable
|
||||
after_update :create_audit_log_entry
|
||||
end
|
||||
|
||||
def messaging_window_enabled?
|
||||
false
|
||||
end
|
||||
|
||||
def create_audit_log_entry; end
|
||||
end
|
||||
|
||||
|
||||
@@ -115,14 +115,7 @@ class Conversation < ApplicationRecord
|
||||
delegate :auto_resolve_duration, to: :account
|
||||
|
||||
def can_reply?
|
||||
channel = inbox&.channel
|
||||
|
||||
return can_reply_on_instagram? if additional_attributes['type'] == 'instagram_direct_message'
|
||||
|
||||
return true unless channel&.messaging_window_enabled?
|
||||
|
||||
messaging_window = inbox.api? ? channel.additional_attributes['agent_reply_time_window'].to_i : 24
|
||||
last_message_in_messaging_window?(messaging_window)
|
||||
Conversations::MessageWindowService.new(self).can_reply?
|
||||
end
|
||||
|
||||
def language
|
||||
@@ -137,24 +130,6 @@ class Conversation < ApplicationRecord
|
||||
messages&.incoming&.last
|
||||
end
|
||||
|
||||
def last_message_in_messaging_window?(time)
|
||||
return false if last_incoming_message.nil?
|
||||
|
||||
Time.current < last_incoming_message.created_at + time.hours
|
||||
end
|
||||
|
||||
def can_reply_on_instagram?
|
||||
global_config = GlobalConfig.get('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT')
|
||||
|
||||
return false if last_incoming_message.nil?
|
||||
|
||||
if global_config['ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT']
|
||||
Time.current < last_incoming_message.created_at + 7.days
|
||||
else
|
||||
last_message_in_messaging_window?(24)
|
||||
end
|
||||
end
|
||||
|
||||
def toggle_status
|
||||
# FIXME: implement state machine with aasm
|
||||
self.status = open? ? :resolved : :open
|
||||
|
||||
64
app/services/conversations/message_window_service.rb
Normal file
64
app/services/conversations/message_window_service.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
class Conversations::MessageWindowService
|
||||
MESSAGING_WINDOW_24_HOURS = 24.hours
|
||||
MESSAGING_WINDOW_7_DAYS = 7.days
|
||||
|
||||
def initialize(conversation)
|
||||
@conversation = conversation
|
||||
end
|
||||
|
||||
def can_reply?
|
||||
return true if messaging_window.blank?
|
||||
|
||||
last_message_in_messaging_window?(messaging_window)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def messaging_window
|
||||
case @conversation.inbox.channel_type
|
||||
when 'Channel::Api'
|
||||
api_messaging_window
|
||||
when 'Channel::FacebookPage'
|
||||
messenger_messaging_window
|
||||
when 'Channel::Instagram'
|
||||
instagram_messaging_window
|
||||
when 'Channel::Whatsapp'
|
||||
MESSAGING_WINDOW_24_HOURS
|
||||
when 'Channel::TwilioSms'
|
||||
twilio_messaging_window
|
||||
end
|
||||
end
|
||||
|
||||
def last_message_in_messaging_window?(time)
|
||||
return false if last_incoming_message.nil?
|
||||
|
||||
Time.current < last_incoming_message.created_at + time
|
||||
end
|
||||
|
||||
def api_messaging_window
|
||||
return if @conversation.inbox.channel.additional_attributes['agent_reply_time_window'].blank?
|
||||
|
||||
@conversation.inbox.channel.additional_attributes['agent_reply_time_window'].to_i.hours
|
||||
end
|
||||
|
||||
# Check medium of the inbox to determine the messaging window
|
||||
def twilio_messaging_window
|
||||
@conversation.inbox.channel.medium == 'whatsapp' ? MESSAGING_WINDOW_24_HOURS : nil
|
||||
end
|
||||
|
||||
def messenger_messaging_window
|
||||
meta_messaging_window('ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT')
|
||||
end
|
||||
|
||||
def instagram_messaging_window
|
||||
meta_messaging_window('ENABLE_INSTAGRAM_CHANNEL_HUMAN_AGENT')
|
||||
end
|
||||
|
||||
def meta_messaging_window(config_key)
|
||||
GlobalConfigService.load(config_key, nil) ? MESSAGING_WINDOW_7_DAYS : MESSAGING_WINDOW_24_HOURS
|
||||
end
|
||||
|
||||
def last_incoming_message
|
||||
@last_incoming_message ||= @conversation.messages&.incoming&.last
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user