diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index a01df50c4..0eeba6b95 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -41,7 +41,7 @@ class Messages::MessageBuilder def build_message @message = conversation.messages.create!(message_params) (response.attachments || []).each do |attachment| - attachment_obj = @message.build_attachment(attachment_params(attachment).except(:remote_file_url)) + attachment_obj = @message.attachments.new(attachment_params(attachment).except(:remote_file_url)) attachment_obj.save! attach_file(attachment_obj, attachment_params(attachment)[:remote_file_url]) if attachment_params(attachment)[:remote_file_url] end diff --git a/app/builders/messages/outgoing/normal_builder.rb b/app/builders/messages/outgoing/normal_builder.rb index c420b2a41..3b44fcd0e 100644 --- a/app/builders/messages/outgoing/normal_builder.rb +++ b/app/builders/messages/outgoing/normal_builder.rb @@ -10,19 +10,22 @@ class Messages::Outgoing::NormalBuilder @fb_id = params[:fb_id] @content_type = params[:content_type] @items = params.to_unsafe_h&.dig(:content_attributes, :items) - @attachment = params[:attachment] + @attachments = params[:attachments] end def perform @message = @conversation.messages.build(message_params) - if @attachment - @message.attachment = Attachment.new( - account_id: message.account_id, - file_type: file_type(@attachment[:file]&.content_type) - ) - @message.attachment.file.attach(@attachment[:file]) - end @message.save + if @attachments.present? + @attachments.each do |uploaded_attachment| + attachment = @message.attachments.new( + account_id: @message.account_id, + file_type: file_type(uploaded_attachment&.content_type) + ) + attachment.file.attach(uploaded_attachment) + end + @message.save + end @message end diff --git a/app/controllers/api/v1/widget/messages_controller.rb b/app/controllers/api/v1/widget/messages_controller.rb index 7e0c446ea..a65e01f9c 100644 --- a/app/controllers/api/v1/widget/messages_controller.rb +++ b/app/controllers/api/v1/widget/messages_controller.rb @@ -10,8 +10,8 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController def create @message = conversation.messages.new(message_params) + @message.save build_attachment - @message.save! end def update @@ -28,13 +28,16 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController private def build_attachment - return if params[:message][:attachment].blank? + return if params[:message][:attachments].blank? - @message.attachment = Attachment.new( - account_id: @message.account_id, - file_type: helpers.file_type(params[:message][:attachment][:file]&.content_type) - ) - @message.attachment.file.attach(params[:message][:attachment][:file]) + params[:message][:attachments].each do |uploaded_attachment| + attachment = @message.attachments.new( + account_id: @message.account_id, + file_type: helpers.file_type(uploaded_attachment&.content_type) + ) + attachment.file.attach(uploaded_attachment) + end + @message.save! end def set_conversation diff --git a/app/finders/message_finder.rb b/app/finders/message_finder.rb index f21093b48..00ef8ab3b 100644 --- a/app/finders/message_finder.rb +++ b/app/finders/message_finder.rb @@ -11,7 +11,7 @@ class MessageFinder private def conversation_messages - @conversation.messages.includes(:attachment, user: { avatar_attachment: :blob }) + @conversation.messages.includes(:attachments, user: { avatar_attachment: :blob }) end def messages diff --git a/app/javascript/dashboard/api/inbox/message.js b/app/javascript/dashboard/api/inbox/message.js index 4ceac84d0..c9681f685 100644 --- a/app/javascript/dashboard/api/inbox/message.js +++ b/app/javascript/dashboard/api/inbox/message.js @@ -22,7 +22,7 @@ class MessageApi extends ApiClient { sendAttachment([conversationId, { file }]) { const formData = new FormData(); - formData.append('attachment[file]', file); + formData.append('attachments[]', file, file.name); return axios({ method: 'post', url: `${this.url}/${conversationId}/messages`, diff --git a/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue b/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue index 7292344f8..4afe61c31 100644 --- a/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue +++ b/app/javascript/dashboard/components/widgets/conversation/ConversationCard.vue @@ -105,15 +105,17 @@ export default { router.push({ path: frontendURL(path) }); }, extractMessageText(chatItem) { - if (chatItem.content) { - return chatItem.content; + const { content, attachments } = chatItem; + + if (content) { + return content; } - let fileType = ''; - if (chatItem.attachment) { - fileType = chatItem.attachment.file_type; - } else { + if (!attachments) { return ' '; } + + const [attachment] = attachments; + const { file_type: fileType } = attachment; const key = `CHAT_LIST.ATTACHMENTS.${fileType}`; return ` diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index 7095b2d86..4ef1ddb4c 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -1,22 +1,26 @@