From 818c769bb753521edd8af28747497b81f5e4126b Mon Sep 17 00:00:00 2001 From: Sony Mathew Date: Fri, 17 Apr 2020 21:15:20 +0530 Subject: [PATCH] Chore: Message to support multiple attachments (#730) * Changes for the message to have multiple attachments * changed the message association to attachments from has_one to has_many * changed all the references of this association in building and fetching to reflect this change * Added number of attachments validation to the message model * Modified the backend responses and endpoints to reflect multiple attachment support (#737) * Changing the frontend components for multiple attachments * changed the request structure to reflect the multiple attachment structures * changed the message bubbles to support multiple attachments * bugfix: agent side attachment was not showing because of a missing await * broken message was shown because of the store filtering * Added documentation for ImageMagick * spec fixes * refactored code to reflect more apt namings * Added updated message listener for the dashboard (#727) * Added the publishing for message updated event * Implemented the listener for dashboard Co-authored-by: Pranav Raj Sreepuram --- app/builders/messages/message_builder.rb | 2 +- .../messages/outgoing/normal_builder.rb | 19 ++++++---- .../api/v1/widget/messages_controller.rb | 17 +++++---- app/finders/message_finder.rb | 2 +- app/javascript/dashboard/api/inbox/message.js | 2 +- .../widgets/conversation/ConversationCard.vue | 14 ++++--- .../widgets/conversation/Message.vue | 38 ++++++++++++------- .../dashboard/helper/actionCable.js | 5 +++ .../store/modules/conversations/actions.js | 6 ++- .../store/modules/conversations/index.js | 19 ++++++---- app/javascript/widget/api/conversation.js | 2 +- app/javascript/widget/api/endPoints.js | 9 ++--- .../widget/components/AgentMessage.vue | 38 +++++++++---------- .../widget/components/ImageBubble.vue | 1 + .../widget/components/UserMessage.vue | 32 +++++++++------- app/javascript/widget/helpers/actionCable.js | 5 +++ .../widget/store/modules/conversation.js | 30 +++++++-------- .../specs/conversation/actions.spec.js | 21 +++++++--- .../specs/conversation/mutations.spec.js | 28 +++++++++----- app/listeners/action_cable_listener.rb | 9 +++++ app/models/message.rb | 10 ++++- app/services/facebook/send_reply_service.rb | 4 +- .../messages/create.json.jbuilder | 2 +- .../messages/index.json.jbuilder | 2 +- .../v1/widget/messages/create.json.jbuilder | 2 +- .../v1/widget/messages/index.json.jbuilder | 2 +- .../reply_with_summary.html.erb | 9 +++-- docs/development/environment-setup/mac-os.md | 3 ++ .../conversations/messages_controller_spec.rb | 6 +-- .../api/v1/widget/messages_controller_spec.rb | 6 +-- .../facebook/send_reply_service_spec.rb | 4 +- 31 files changed, 212 insertions(+), 137 deletions(-) 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 @@