feat: Notification on new messages in conversation (#1204)

fixes: #895
fixes: #1118
fixes: #1075

Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
Sojan Jose
2020-09-10 19:19:15 +05:30
committed by GitHub
parent 3b92c744d6
commit 31c07771e8
36 changed files with 259 additions and 94 deletions

View File

@@ -19,6 +19,18 @@ class AgentNotifications::ConversationNotificationsMailer < ApplicationMailer
send_mail_with_liquid(to: @agent.email, subject: subject) and return
end
def assigned_conversation_new_message(conversation, agent)
return unless smtp_config_set_or_development?
# Don't spam with email notifications if agent is online
return if ::OnlineStatusTracker.get_presence(conversation.account.id, 'User', agent.id)
@agent = agent
@conversation = conversation
subject = "#{@agent.available_name}, New message in your assigned conversation [ID - #{@conversation.display_id}]."
@action_url = app_account_conversation_url(account_id: @conversation.account_id, id: @conversation.display_id)
send_mail_with_liquid(to: @agent.email, subject: subject) and return
end
private
def liquid_droppables

View File

@@ -50,7 +50,13 @@ class ApplicationMailer < ActionMailer::Base
}
end
def locale_from_account(account)
I18n.available_locales.map(&:to_s).include?(account.locale) ? account.locale : nil
end
def ensure_current_account(account)
Current.account = account if account.present?
locale ||= locale_from_account(account) if account.present?
I18n.locale = locale || I18n.default_locale
end
end

View File

@@ -6,6 +6,7 @@ class ConversationReplyMailer < ApplicationMailer
return unless smtp_config_set_or_development?
init_conversation_attributes(conversation)
return if conversation_already_viewed?
recap_messages = @conversation.messages.chat.where('created_at < ?', message_queued_time).last(10)
new_messages = @conversation.messages.chat.where('created_at >= ?', message_queued_time)
@@ -26,6 +27,7 @@ class ConversationReplyMailer < ApplicationMailer
return unless smtp_config_set_or_development?
init_conversation_attributes(conversation)
return if conversation_already_viewed?
@messages = @conversation.messages.chat.outgoing.where('created_at >= ?', message_queued_time)
return false if @messages.count.zero?
@@ -63,6 +65,18 @@ class ConversationReplyMailer < ApplicationMailer
@agent = @conversation.assignee
end
def conversation_already_viewed?
# whether contact already saw the message on widget
return unless @conversation.contact_last_seen_at
return unless last_outgoing_message&.created_at
@conversation.contact_last_seen_at > last_outgoing_message&.created_at
end
def last_outgoing_message
@conversation.messages.chat.where.not(message_type: :incoming)&.last
end
def assignee_name
@assignee_name ||= @agent&.available_name || 'Notifications'
end