6fdd4a2996
- 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.
73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
class Messages::MessageBuilder
|
|
include ::FileTypeHelper
|
|
attr_reader :message
|
|
|
|
def initialize(user, conversation, params)
|
|
@params = params
|
|
@private = params[:private] || false
|
|
@conversation = conversation
|
|
@user = user
|
|
@message_type = params[:message_type] || 'outgoing'
|
|
@items = params.to_unsafe_h&.dig(:content_attributes, :items)
|
|
@attachments = params[:attachments]
|
|
@in_reply_to = params.to_unsafe_h&.dig(:content_attributes, :in_reply_to)
|
|
end
|
|
|
|
def perform
|
|
@message = @conversation.messages.build(message_params)
|
|
process_attachments
|
|
@message.save!
|
|
@message
|
|
end
|
|
|
|
private
|
|
|
|
def process_attachments
|
|
return if @attachments.blank?
|
|
|
|
@attachments.each do |uploaded_attachment|
|
|
@message.attachments.build(
|
|
account_id: @message.account_id,
|
|
file_type: file_type(uploaded_attachment&.content_type),
|
|
file: uploaded_attachment
|
|
)
|
|
end
|
|
end
|
|
|
|
def message_type
|
|
if @conversation.inbox.channel_type != 'Channel::Api' && @message_type == 'incoming'
|
|
raise StandardError, 'Incoming messages are only allowed in Api inboxes'
|
|
end
|
|
|
|
@message_type
|
|
end
|
|
|
|
def sender
|
|
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])
|
|
end
|
|
|
|
def message_params
|
|
{
|
|
account_id: @conversation.account_id,
|
|
inbox_id: @conversation.inbox_id,
|
|
message_type: message_type,
|
|
content: @params[:content],
|
|
private: @private,
|
|
sender: sender,
|
|
content_type: @params[:content_type],
|
|
items: @items,
|
|
in_reply_to: @in_reply_to,
|
|
echo_id: @params[:echo_id]
|
|
}.merge(external_created_at)
|
|
end
|
|
end
|