2019-08-14 09:48:44 +00:00
|
|
|
class Messages::Outgoing::NormalBuilder
|
2020-04-02 06:58:38 +00:00
|
|
|
include ::FileTypeHelper
|
2019-08-14 09:48:44 +00:00
|
|
|
attr_reader :message
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
def initialize(user, conversation, params)
|
2020-04-10 11:12:37 +00:00
|
|
|
@content = params[:content]
|
2020-03-05 20:13:12 +00:00
|
|
|
@private = params[:private] || false
|
2019-08-14 09:48:44 +00:00
|
|
|
@conversation = conversation
|
|
|
|
@user = user
|
|
|
|
@fb_id = params[:fb_id]
|
2020-04-10 11:12:37 +00:00
|
|
|
@content_type = params[:content_type]
|
|
|
|
@items = params.to_unsafe_h&.dig(:content_attributes, :items)
|
2020-03-22 10:24:36 +00:00
|
|
|
@attachment = params[:attachment]
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2020-03-22 10:24:36 +00:00
|
|
|
@message = @conversation.messages.build(message_params)
|
|
|
|
if @attachment
|
2020-04-02 06:58:38 +00:00
|
|
|
@message.attachment = Attachment.new(
|
|
|
|
account_id: message.account_id,
|
|
|
|
file_type: file_type(@attachment[:file]&.content_type)
|
|
|
|
)
|
2020-03-22 10:24:36 +00:00
|
|
|
@message.attachment.file.attach(@attachment[:file])
|
|
|
|
end
|
|
|
|
@message.save
|
|
|
|
@message
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def message_params
|
|
|
|
{
|
|
|
|
account_id: @conversation.account_id,
|
|
|
|
inbox_id: @conversation.inbox_id,
|
|
|
|
message_type: :outgoing,
|
|
|
|
content: @content,
|
|
|
|
private: @private,
|
2020-03-10 18:32:15 +00:00
|
|
|
user_id: @user&.id,
|
2020-04-10 11:12:37 +00:00
|
|
|
source_id: @fb_id,
|
|
|
|
content_type: @content_type,
|
|
|
|
items: @items
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|