Chatwoot/app/models/message.rb
Sony Mathew d4b3ba4baa [#139] Send conversation emails (#442)
* [#139] Delayed emails for conversations

* Added the setex and get methods to Redis wrapper
* Set the priorities for the sidekiq queues
* Was not able to use mailhog for testing email in local, switched back to letter opener and added comments on using the SMTP settings
* Added after create hood in messages to queue the sending of mail after 2 minutes using sidekiq worker and also set the redis key for the conversation to avoid the email sending for every message
* Added the sidekiq worker to send the email and delete the conversation redis key
* Added the mailer and mail template
* mailer sends the last 10 messages along with the new messages from the time it was queued

* Send email only in development or if smtp config is set

* Send email only in development or if smtp config is set
* Set the SMTP_PORT in production variable

* Adding redis to circle CI

* Specs for the conversation email changes

* Added specs for conversation email sidekiq worker
* Added specs for conversation mailer
* Added specs in message model for the after create hook for notify email

* Send emails only when there is a reply from agent

* set development to use mailhog

* Adding comments for using letter opener
2020-01-23 23:14:07 +05:45

111 lines
3.8 KiB
Ruby

# == Schema Information
#
# Table name: messages
#
# id :integer not null, primary key
# content :text
# content_attributes :json
# content_type :integer default("text")
# message_type :integer not null
# private :boolean default(FALSE)
# status :integer default("sent")
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# conversation_id :integer not null
# fb_id :string
# inbox_id :integer not null
# user_id :integer
#
# Indexes
#
# index_messages_on_conversation_id (conversation_id)
#
class Message < ApplicationRecord
include Events::Types
validates :account_id, presence: true
validates :inbox_id, presence: true
validates :conversation_id, presence: true
enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 }
enum content_type: { text: 0, input: 1, input_textarea: 2, input_email: 3 }
enum status: { sent: 0, delivered: 1, read: 2, failed: 3 }
store :content_attributes, accessors: [:submitted_email], coder: JSON, prefix: :input
# .succ is a hack to avoid https://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be
scope :unread_since, ->(datetime) { where('EXTRACT(EPOCH FROM created_at) > (?)', datetime.to_i.succ) }
scope :chat, -> { where.not(message_type: :activity).where.not(private: true) }
default_scope { order(created_at: :asc) }
belongs_to :account
belongs_to :inbox
belongs_to :conversation
belongs_to :user, required: false
has_one :attachment, dependent: :destroy, autosave: true
after_create :reopen_conversation,
:dispatch_event,
:send_reply,
:execute_message_template_hooks,
:notify_via_mail
def channel_token
@token ||= inbox.channel.try(:page_access_token)
end
def push_event_data
data = attributes.merge(
created_at: created_at.to_i,
message_type: message_type_before_type_cast,
conversation_id: conversation.display_id
)
data.merge!(attachment: attachment.push_event_data) if attachment
data.merge!(sender: user.push_event_data) if user
data
end
def reportable?
incoming? || outgoing?
end
private
def dispatch_event
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self)
if outgoing? && conversation.messages.outgoing.count == 1
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self)
end
end
def send_reply
::Facebook::SendReplyService.new(message: self).perform
end
def reopen_conversation
if incoming? && conversation.resolved?
conversation.toggle_status
Rails.configuration.dispatcher.dispatch(CONVERSATION_REOPENED, Time.zone.now, conversation: conversation)
end
end
def execute_message_template_hooks
::MessageTemplates::HookExecutionService.new(message: self).perform
end
def notify_via_mail
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % conversation.id
if Redis::Alfred.get(conversation_mail_key).nil? && conversation.contact.email? && outgoing?
# set a redis key for the conversation so that we don't need to send email for every
# new message that comes in and we dont enque the delayed sidekiq job for every message
Redis::Alfred.setex(conversation_mail_key, Time.zone.now)
# Since this is live chat, send the email after few minutes so the only one email with
# last few messages coupled together is sent rather than email for each message
ConversationEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
end
end
end