Chatwoot/app/models/message.rb

174 lines
5.6 KiB
Ruby
Raw Normal View History

# == 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
# contact_id :bigint
# conversation_id :integer not null
# inbox_id :integer not null
# source_id :string
# user_id :integer
#
# Indexes
#
# index_messages_on_account_id (account_id)
# index_messages_on_contact_id (contact_id)
# index_messages_on_conversation_id (conversation_id)
# index_messages_on_inbox_id (inbox_id)
# index_messages_on_source_id (source_id)
# index_messages_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (contact_id => contacts.id)
#
class Message < ApplicationRecord
include Events::Types
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
validates :account_id, presence: true
validates :inbox_id, presence: true
validates :conversation_id, presence: true
validates_with ContentAttributeValidator
enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 }
enum content_type: {
text: 0,
input_text: 1,
input_textarea: 2,
input_email: 3,
input_select: 4,
cards: 5,
form: 6,
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
article: 7,
incoming_email: 8
}
enum status: { sent: 0, delivered: 1, read: 2, failed: 3 }
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email], coder: JSON
# .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(private: false) }
default_scope { order(created_at: :asc) }
belongs_to :account
belongs_to :inbox
belongs_to :conversation, touch: true
belongs_to :user, required: false
belongs_to :contact, required: false
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
after_create :reopen_conversation,
:notify_via_mail
after_create_commit :execute_after_create_commit_callbacks
after_update :dispatch_update_event
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!(attachments: attachments.map(&:push_event_data)) if attachments.present?
2019-10-20 08:47:26 +00:00
data.merge!(sender: user.push_event_data) if user
data
end
def reportable?
incoming? || outgoing?
end
def webhook_data
{
id: id,
content: content,
created_at: created_at,
message_type: message_type,
content_type: content_type,
content_attributes: content_attributes,
source_id: source_id,
sender: user.try(:webhook_data),
contact: contact.try(:webhook_data),
inbox: inbox.webhook_data,
conversation: conversation.webhook_data,
account: account.webhook_data
}
end
private
def execute_after_create_commit_callbacks
# rails issue with order of active record callbacks being executed
# https://github.com/rails/rails/issues/20911
dispatch_create_events
send_reply
execute_message_template_hooks
end
def dispatch_create_events
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self)
2019-10-20 08:47:26 +00:00
if outgoing? && conversation.messages.outgoing.count == 1
Rails.configuration.dispatcher.dispatch(FIRST_REPLY_CREATED, Time.zone.now, message: self)
end
end
def dispatch_update_event
Rails.configuration.dispatcher.dispatch(MESSAGE_UPDATED, Time.zone.now, message: self)
end
def send_reply
channel_name = conversation.inbox.channel.class.to_s
if channel_name == 'Channel::FacebookPage'
::Facebook::SendReplyService.new(message: self).perform
elsif channel_name == 'Channel::TwitterProfile'
::Twitter::SendReplyService.new(message: self).perform
elsif channel_name == 'Channel::TwilioSms'
::Twilio::OutgoingMessageService.new(message: self).perform
end
end
def reopen_conversation
conversation.open! if incoming? && conversation.resolved? && !conversation.muted?
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
ConversationReplyEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
end
end
def validate_attachments_limit(_attachment)
errors.add(attachments: 'exceeded maximum allowed') if attachments.size >= NUMBER_OF_PERMITTED_ATTACHMENTS
end
end