2020-01-09 07:36:40 +00:00
|
|
|
class Messages::MessageBuilder
|
2020-07-21 06:45:24 +00:00
|
|
|
include ::FileTypeHelper
|
|
|
|
attr_reader :message
|
2020-01-09 07:36:40 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def initialize(user, conversation, params)
|
2020-09-13 10:46:52 +00:00
|
|
|
@params = params
|
2020-07-21 06:45:24 +00:00
|
|
|
@private = params[:private] || false
|
|
|
|
@conversation = conversation
|
|
|
|
@user = user
|
|
|
|
@message_type = params[:message_type] || 'outgoing'
|
|
|
|
@attachments = params[:attachments]
|
2022-01-10 07:11:59 +00:00
|
|
|
return unless params.instance_of?(ActionController::Parameters)
|
|
|
|
|
2020-08-11 04:27:42 +00:00
|
|
|
@in_reply_to = params.to_unsafe_h&.dig(:content_attributes, :in_reply_to)
|
2022-01-10 07:11:59 +00:00
|
|
|
@items = params.to_unsafe_h&.dig(:content_attributes, :items)
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def perform
|
2020-07-21 06:45:24 +00:00
|
|
|
@message = @conversation.messages.build(message_params)
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
process_attachments
|
2021-10-11 07:30:48 +00:00
|
|
|
process_emails
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
@message.save!
|
2020-07-21 06:45:24 +00:00
|
|
|
@message
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
private
|
2019-08-31 06:22:09 +00:00
|
|
|
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
def process_attachments
|
|
|
|
return if @attachments.blank?
|
|
|
|
|
|
|
|
@attachments.each do |uploaded_attachment|
|
2022-02-15 10:46:54 +00:00
|
|
|
attachment = @message.attachments.build(
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
account_id: @message.account_id,
|
|
|
|
file: uploaded_attachment
|
|
|
|
)
|
2022-02-15 10:46:54 +00:00
|
|
|
|
|
|
|
attachment.file_type = file_type(uploaded_attachment&.content_type) if uploaded_attachment.is_a?(ActionDispatch::Http::UploadedFile)
|
chore: Security Improvements to the API (#2893)
- Devise auth tokens are reset on password update
- Avatar attachment file type is limited to jpeg,gif and png
- Avatar attachment file size is limited to 15 mb
- Widget Message attachments are limited to types ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/tiff', 'application/pdf', 'audio/mpeg', 'video/mp4', 'audio/ogg', 'text/csv']
- Widget Message attachments are limited to 40Mb size limit.
2021-09-01 09:38:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-11 07:30:48 +00:00
|
|
|
def process_emails
|
|
|
|
return unless @conversation.inbox&.inbox_type == 'Email'
|
|
|
|
|
|
|
|
cc_emails = @params[:cc_emails].split(',') if @params[:cc_emails]
|
|
|
|
bcc_emails = @params[:bcc_emails].split(',') if @params[:bcc_emails]
|
|
|
|
|
|
|
|
@message.content_attributes[:cc_emails] = cc_emails
|
|
|
|
@message.content_attributes[:bcc_emails] = bcc_emails
|
|
|
|
end
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def message_type
|
2020-08-10 06:45:29 +00:00
|
|
|
if @conversation.inbox.channel_type != 'Channel::Api' && @message_type == 'incoming'
|
2020-07-21 06:45:24 +00:00
|
|
|
raise StandardError, 'Incoming messages are only allowed in Api inboxes'
|
2019-10-20 19:10:18 +00:00
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
@message_type
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def sender
|
2021-06-07 19:41:06 +00:00
|
|
|
message_type == 'outgoing' ? (message_sender || @user) : @conversation.contact
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_created_at
|
|
|
|
@params[:external_created_at].present? ? { external_created_at: @params[:external_created_at] } : {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_sender
|
|
|
|
return if @params[:sender_type] != 'AgentBot'
|
|
|
|
|
|
|
|
AgentBot.where(account_id: [nil, @conversation.account.id]).find_by(id: @params[:sender_id])
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-10-20 19:10:18 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def message_params
|
|
|
|
{
|
2020-07-21 06:45:24 +00:00
|
|
|
account_id: @conversation.account_id,
|
|
|
|
inbox_id: @conversation.inbox_id,
|
|
|
|
message_type: message_type,
|
2020-09-13 10:46:52 +00:00
|
|
|
content: @params[:content],
|
2020-07-21 06:45:24 +00:00
|
|
|
private: @private,
|
|
|
|
sender: sender,
|
2020-09-13 10:46:52 +00:00
|
|
|
content_type: @params[:content_type],
|
2020-08-11 04:27:42 +00:00
|
|
|
items: @items,
|
2020-09-13 10:46:52 +00:00
|
|
|
in_reply_to: @in_reply_to,
|
|
|
|
echo_id: @params[:echo_id]
|
2021-06-07 19:41:06 +00:00
|
|
|
}.merge(external_created_at)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|