diff --git a/app/builders/messages/instagram/message_builder.rb b/app/builders/messages/instagram/message_builder.rb index e2c594376..5b2243906 100644 --- a/app/builders/messages/instagram/message_builder.rb +++ b/app/builders/messages/instagram/message_builder.rb @@ -76,6 +76,10 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder message[:reply_to][:story] if message[:reply_to].present? && message[:reply_to][:story].present? end + def message_reply_attributes + message[:reply_to][:mid] if message[:reply_to].present? && message[:reply_to][:mid].present? + end + def build_message return if @outgoing_echo && already_sent_from_chatwoot? return if message_content.blank? && all_unsupported_files? @@ -118,7 +122,10 @@ class Messages::Instagram::MessageBuilder < Messages::Messenger::MessageBuilder message_type: message_type, source_id: message_identifier, content: message_content, - sender: @outgoing_echo ? nil : contact + sender: @outgoing_echo ? nil : contact, + content_attributes: { + in_reply_to_external_id: message_reply_attributes + } } end diff --git a/app/javascript/dashboard/components/widgets/conversation/Message.vue b/app/javascript/dashboard/components/widgets/conversation/Message.vue index e0027850b..d7c9742ae 100644 --- a/app/javascript/dashboard/components/widgets/conversation/Message.vue +++ b/app/javascript/dashboard/components/widgets/conversation/Message.vue @@ -24,7 +24,7 @@ /> ({}), }, inReplyTo: { type: Object, @@ -281,7 +281,7 @@ export default { copy: this.hasText, delete: this.hasText || this.hasAttachments, cannedResponse: this.isOutgoing && this.hasText, - replyTo: !this.data.private && this.inboxSupportsReplyTo, + replyTo: !this.data.private && this.inboxSupportsReplyTo.outgoing, }; }, contentAttributes() { diff --git a/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue b/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue index a1dc9b814..38bb24f8f 100644 --- a/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue +++ b/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue @@ -285,13 +285,18 @@ export default { return this.currentChat.unread_count || 0; }, inboxSupportsReplyTo() { - return ( - this.inboxHasFeature(INBOX_FEATURES.REPLY_TO_OUTGOING) && - this.isFeatureEnabledonAccount( + if ( + !this.isFeatureEnabledonAccount( this.accountId, FEATURE_FLAGS.MESSAGE_REPLY_TO ) - ); + ) { + return {}; + } + return { + incoming: this.inboxHasFeature(INBOX_FEATURES.REPLY_TO), + outgoing: this.inboxHasFeature(INBOX_FEATURES.REPLY_TO_OUTGOING), + }; }, }, diff --git a/spec/builders/messages/instagram/message_builder_spec.rb b/spec/builders/messages/instagram/message_builder_spec.rb index b7dfddc8e..618b8bdd6 100644 --- a/spec/builders/messages/instagram/message_builder_spec.rb +++ b/spec/builders/messages/instagram/message_builder_spec.rb @@ -14,6 +14,7 @@ describe Messages::Instagram::MessageBuilder do let!(:dm_params) { build(:instagram_message_create_event).with_indifferent_access } let!(:story_mention_params) { build(:instagram_story_mention_event).with_indifferent_access } let!(:instagram_story_reply_event) { build(:instagram_story_reply_event).with_indifferent_access } + let!(:instagram_message_reply_event) { build(:instagram_message_reply_event).with_indifferent_access } let(:fb_object) { double } let(:contact) { create(:contact, id: 'Sender-id-1', name: 'Jane Dae') } let(:contact_inbox) { create(:contact_inbox, contact_id: contact.id, inbox_id: instagram_inbox.id, source_id: 'Sender-id-1') } @@ -101,6 +102,34 @@ describe Messages::Instagram::MessageBuilder do expect(message.content_attributes[:story_url]).to eq('https://chatwoot-assets.local/sample.png') end + it 'creates message with for reply with mid' do + allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) + allow(fb_object).to receive(:get_object).and_return( + { + name: 'Jane', + id: 'Sender-id-1', + account_id: instagram_inbox.account_id, + profile_pic: 'https://chatwoot-assets.local/sample.png' + }.with_indifferent_access + ) + # create first message to ensure reply to is valid + first_message = dm_params[:entry][0]['messaging'][0] + contact_inbox + described_class.new(first_message, instagram_inbox).perform + + # create the second message with the reply to mid set + messaging = instagram_message_reply_event[:entry][0]['messaging'][0] + contact_inbox + + described_class.new(messaging, instagram_inbox).perform + first_message = instagram_channel.inbox.messages.first + message = instagram_channel.inbox.messages.last + + expect(message.content).to eq('This is message with replyto mid') + expect(message.content_attributes[:in_reply_to_external_id]).to eq(first_message.source_id) + expect(message.content_attributes[:in_reply_to]).to eq(first_message.id) + end + it 'raises exception on deleted story' do allow(Koala::Facebook::API).to receive(:new).and_return(fb_object) allow(fb_object).to receive(:get_object).and_raise(Koala::Facebook::ClientError.new( diff --git a/spec/factories/instagram/instagram_message_create_event.rb b/spec/factories/instagram/instagram_message_create_event.rb index 4cc8b7f67..7729c1d7a 100644 --- a/spec/factories/instagram/instagram_message_create_event.rb +++ b/spec/factories/instagram/instagram_message_create_event.rb @@ -86,6 +86,36 @@ FactoryBot.define do initialize_with { attributes } end + factory :instagram_message_reply_event, class: Hash do + entry do + [ + { + 'id': 'instagram-message-id-123', + 'time': '2021-09-08T06:35:04+0000', + 'messaging': [ + { + 'sender': { + 'id': 'Sender-id-1' + }, + 'recipient': { + 'id': 'chatwoot-app-user-id-1' + }, + 'timestamp': '2021-09-08T06:34:04+0000', + 'message': { + 'mid': 'message-id-2', + 'text': 'This is message with replyto mid', + 'reply_to': { + 'mid': 'message-id-1' + } + } + } + ] + } + ] + end + initialize_with { attributes } + end + factory :instagram_test_text_event, class: Hash do entry do [