diff --git a/app/controllers/api/v1/conversations/messages_controller.rb b/app/controllers/api/v1/conversations/messages_controller.rb
index 22fdd88fb..793b3d054 100644
--- a/app/controllers/api/v1/conversations/messages_controller.rb
+++ b/app/controllers/api/v1/conversations/messages_controller.rb
@@ -1,8 +1,18 @@
class Api::V1::Conversations::MessagesController < Api::BaseController
- before_action :set_conversation, only: [:create]
+ before_action :set_conversation, only: [:index, :create]
+
+ def index
+ @messages = message_finder.perform
+ end
def create
mb = Messages::Outgoing::NormalBuilder.new(current_user, @conversation, params)
@message = mb.perform
end
+
+ private
+
+ def message_finder
+ @message_finder ||= MessageFinder.new(@conversation, params)
+ end
end
diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb
index 6c44114be..cf0d55779 100644
--- a/app/controllers/api/v1/conversations_controller.rb
+++ b/app/controllers/api/v1/conversations_controller.rb
@@ -7,9 +7,7 @@ class Api::V1::ConversationsController < Api::BaseController
@conversations_count = result[:count]
end
- def show
- @messages = messages_finder.perform
- end
+ def show; end
def toggle_status
@status = @conversation.toggle_status
@@ -34,8 +32,4 @@ class Api::V1::ConversationsController < Api::BaseController
def conversation_finder
@conversation_finder ||= ConversationFinder.new(current_user, params)
end
-
- def messages_finder
- @message_finder ||= MessageFinder.new(@conversation, params)
- end
end
diff --git a/app/controllers/twitter/callbacks_controller.rb b/app/controllers/twitter/callbacks_controller.rb
index 876720f26..a46c54821 100644
--- a/app/controllers/twitter/callbacks_controller.rb
+++ b/app/controllers/twitter/callbacks_controller.rb
@@ -1,5 +1,7 @@
class Twitter::CallbacksController < Twitter::BaseController
def show
+ return redirect_to app_new_twitter_inbox_url if permitted_params[:denied]
+
@response = twitter_client.access_token(
oauth_token: permitted_params[:oauth_token],
oauth_verifier: permitted_params[:oauth_verifier]
@@ -46,6 +48,6 @@ class Twitter::CallbacksController < Twitter::BaseController
end
def permitted_params
- params.permit(:oauth_token, :oauth_verifier)
+ params.permit(:oauth_token, :oauth_verifier, :denied)
end
end
diff --git a/app/javascript/dashboard/api/inbox/message.js b/app/javascript/dashboard/api/inbox/message.js
index 367e3e00c..00dda8a63 100644
--- a/app/javascript/dashboard/api/inbox/message.js
+++ b/app/javascript/dashboard/api/inbox/message.js
@@ -15,7 +15,7 @@ class MessageApi extends ApiClient {
}
getPreviousMessages({ conversationId, before }) {
- return axios.get(`${this.url}/${conversationId}`, {
+ return axios.get(`${this.url}/${conversationId}/messages`, {
params: { before },
});
}
diff --git a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
index 7e7bacfd2..fff476945 100644
--- a/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
+++ b/app/javascript/dashboard/components/widgets/conversation/ReplyBox.vue
@@ -33,14 +33,18 @@
@@ -49,16 +53,12 @@
class="button send-button"
:disabled="disableButton()"
:class="{
- disabled: message.length === 0 || message.length > 640,
+ disabled: message.length === 0 || message.length > maxLength,
warning: isPrivate,
}"
@click="sendMessage"
>
- {{
- isPrivate
- ? $t('CONVERSATION.REPLYBOX.CREATE')
- : $t('CONVERSATION.REPLYBOX.SEND')
- }}
+ {{ replyButtonLabel }}
this.initialize());
this.$watch('chatList.length', () => {
+ this.fetchConversation();
this.setActiveChat();
});
},
@@ -81,13 +82,28 @@ export default {
break;
default:
this.$store.dispatch('setActiveInbox', null);
+ this.$store.dispatch('clearSelectedState');
break;
}
},
- setActiveChat() {
+ fetchConversation() {
+ if (!this.conversationId) {
+ return;
+ }
+ const chat = this.findConversation();
+ if (!chat) {
+ this.$store.dispatch('getConversation', this.conversationId);
+ }
+ },
+ findConversation() {
const conversationId = parseInt(this.conversationId, 10);
const [chat] = this.chatList.filter(c => c.id === conversationId);
+ return chat;
+ },
+
+ setActiveChat() {
+ const chat = this.findConversation();
if (!chat) return;
this.$store.dispatch('setActiveChat', chat).then(() => {
bus.$emit('scrollToMessage');
diff --git a/app/javascript/dashboard/routes/dashboard/conversation/conversation.routes.js b/app/javascript/dashboard/routes/dashboard/conversation/conversation.routes.js
index b147fa6a2..f38cded3c 100644
--- a/app/javascript/dashboard/routes/dashboard/conversation/conversation.routes.js
+++ b/app/javascript/dashboard/routes/dashboard/conversation/conversation.routes.js
@@ -28,7 +28,7 @@ export default {
roles: ['administrator', 'agent'],
component: ConversationView,
props: route => {
- return { conversationId: route.params.conversation_id };
+ return { inboxId: 0, conversationId: route.params.conversation_id };
},
},
{
diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue
index 28eaa40de..c85436084 100644
--- a/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue
+++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/Index.vue
@@ -39,6 +39,9 @@
Website
+
+ Twitter
+
diff --git a/app/javascript/dashboard/store/modules/conversations/actions.js b/app/javascript/dashboard/store/modules/conversations/actions.js
index a3b718115..0b4072dc2 100644
--- a/app/javascript/dashboard/store/modules/conversations/actions.js
+++ b/app/javascript/dashboard/store/modules/conversations/actions.js
@@ -7,6 +7,15 @@ import FBChannel from '../../../api/channel/fbChannel';
// actions
const actions = {
+ getConversation: async ({ commit }, conversationId) => {
+ try {
+ const response = await ConversationApi.show(conversationId);
+ commit(types.default.ADD_CONVERSATION, response.data);
+ } catch (error) {
+ // Ignore error
+ }
+ },
+
fetchAllConversations: async ({ commit, dispatch }, params) => {
commit(types.default.SET_LIST_LOADING_STATUS);
try {
diff --git a/app/javascript/dashboard/store/modules/conversations/getters.js b/app/javascript/dashboard/store/modules/conversations/getters.js
index 92587d956..c3431333a 100644
--- a/app/javascript/dashboard/store/modules/conversations/getters.js
+++ b/app/javascript/dashboard/store/modules/conversations/getters.js
@@ -33,12 +33,13 @@ const getters = {
getChatListLoadingStatus: ({ listLoadingStatus }) => listLoadingStatus,
getAllMessagesLoaded(_state) {
const [chat] = getSelectedChatConversation(_state);
- return chat.allMessagesLoaded === undefined
+ return !chat || chat.allMessagesLoaded === undefined
? false
: chat.allMessagesLoaded;
},
getUnreadCount(_state) {
const [chat] = getSelectedChatConversation(_state);
+ if (!chat) return [];
return chat.messages.filter(
chatMessage =>
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&
diff --git a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js
new file mode 100644
index 000000000..306b87bbb
--- /dev/null
+++ b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js
@@ -0,0 +1,24 @@
+import axios from 'axios';
+import actions from '../../conversations/actions';
+import * as types from '../../../mutation-types';
+
+const commit = jest.fn();
+global.axios = axios;
+jest.mock('axios');
+
+describe('#actions', () => {
+ describe('#getConversation', () => {
+ it('sends correct actions if API is success', async () => {
+ axios.get.mockResolvedValue({ data: { id: 1, meta: {} } });
+ await actions.getConversation({ commit }, 1);
+ expect(commit.mock.calls).toEqual([
+ [types.default.ADD_CONVERSATION, { id: 1, meta: {} }],
+ ]);
+ });
+ it('sends correct actions if API is error', async () => {
+ axios.get.mockRejectedValue({ message: 'Incorrect header' });
+ await actions.getConversation({ commit });
+ expect(commit.mock.calls).toEqual([]);
+ });
+ });
+});
diff --git a/app/presenters/conversations/event_data_presenter.rb b/app/presenters/conversations/event_data_presenter.rb
index d8c58916a..bf1b4140d 100644
--- a/app/presenters/conversations/event_data_presenter.rb
+++ b/app/presenters/conversations/event_data_presenter.rb
@@ -6,6 +6,7 @@ class Conversations::EventDataPresenter < SimpleDelegator
def push_data
{
id: display_id,
+ additional_attributes: additional_attributes,
inbox_id: inbox_id,
messages: push_messages,
meta: push_meta,
diff --git a/app/services/twitter/direct_message_parser_service.rb b/app/services/twitter/direct_message_parser_service.rb
index d319dcf57..f68d60815 100644
--- a/app/services/twitter/direct_message_parser_service.rb
+++ b/app/services/twitter/direct_message_parser_service.rb
@@ -11,7 +11,9 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService
content: message_create_data['message_data']['text'],
account_id: @inbox.account_id,
inbox_id: @inbox.id,
- message_type: outgoing_message? ? :outgoing : :incoming
+ message_type: outgoing_message? ? :outgoing : :incoming,
+ contact_id: @contact.id,
+ source_id: direct_message_data['id']
)
end
diff --git a/app/services/twitter/send_reply_service.rb b/app/services/twitter/send_reply_service.rb
index 8c103c07f..88879c68b 100644
--- a/app/services/twitter/send_reply_service.rb
+++ b/app/services/twitter/send_reply_service.rb
@@ -39,10 +39,16 @@ class Twitter::SendReplyService
end
def send_tweet_reply
- twitter_client.send_tweet_reply(
+ response = twitter_client.send_tweet_reply(
reply_to_tweet_id: conversation.additional_attributes['tweet_id'],
tweet: screen_name + message.content
)
+ if response.status == '200'
+ tweet_data = response.body
+ message.update!(source_id: tweet_data['id_str'])
+ else
+ Rails.logger 'TWITTER_TWEET_REPLY_ERROR' + response.body
+ end
end
def send_reply
diff --git a/app/services/twitter/tweet_parser_service.rb b/app/services/twitter/tweet_parser_service.rb
index 79dc13015..84ea87ff4 100644
--- a/app/services/twitter/tweet_parser_service.rb
+++ b/app/services/twitter/tweet_parser_service.rb
@@ -3,16 +3,9 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
def perform
set_inbox
- find_or_create_contact(user)
- set_conversation
- @conversation.messages.create(
- account_id: @inbox.account_id,
- contact_id: @contact.id,
- content: tweet_text,
- inbox_id: @inbox.id,
- message_type: message_type,
- source_id: tweet_data['id'].to_s
- )
+ return if message_already_exist?
+
+ create_message
end
private
@@ -37,6 +30,10 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
tweet_data['user']
end
+ def tweet_id
+ tweet_data['id'].to_s
+ end
+
def parent_tweet_id
tweet_data['in_reply_to_status_id_str'].nil? ? tweet_data['id'].to_s : tweet_data['in_reply_to_status_id_str']
end
@@ -66,4 +63,21 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService
@conversation = ::Conversation.create!(conversation_params)
end
+
+ def message_already_exist?
+ @inbox.messages.find_by(source_id: tweet_id)
+ end
+
+ def create_message
+ find_or_create_contact(user)
+ set_conversation
+ @conversation.messages.create(
+ account_id: @inbox.account_id,
+ contact_id: @contact.id,
+ content: tweet_text,
+ inbox_id: @inbox.id,
+ message_type: message_type,
+ source_id: tweet_id
+ )
+ end
end
diff --git a/app/views/api/v1/contacts/conversations/index.json.jbuilder b/app/views/api/v1/contacts/conversations/index.json.jbuilder
index 3a0b2401a..4e8bcc8cd 100644
--- a/app/views/api/v1/contacts/conversations/index.json.jbuilder
+++ b/app/views/api/v1/contacts/conversations/index.json.jbuilder
@@ -1,26 +1,5 @@
json.payload do
json.array! @conversations do |conversation|
- json.meta do
- json.sender do
- json.id conversation.contact.id
- json.name conversation.contact.name
- json.thumbnail conversation.contact.avatar_url
- json.channel conversation.inbox.try(:channel_type)
- end
- json.assignee conversation.assignee
- end
-
- json.id conversation.display_id
- if conversation.unread_incoming_messages.count.zero?
- json.messages [conversation.messages.last.try(:push_event_data)]
- else
- json.messages conversation.unread_messages.map(&:push_event_data)
- end
- json.inbox_id conversation.inbox_id
- json.status conversation.status
- json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
- json.user_last_seen_at conversation.user_last_seen_at.to_i
- json.agent_last_seen_at conversation.agent_last_seen_at.to_i
- json.unread_count conversation.unread_incoming_messages.count
+ json.partial! 'api/v1/conversations/partials/conversation.json.jbuilder', conversation: conversation
end
end
diff --git a/app/views/api/v1/conversations/index.json.jbuilder b/app/views/api/v1/conversations/index.json.jbuilder
index d4efbc023..57c31ecc3 100644
--- a/app/views/api/v1/conversations/index.json.jbuilder
+++ b/app/views/api/v1/conversations/index.json.jbuilder
@@ -4,31 +4,9 @@ json.data do
json.unassigned_count @conversations_count[:unassigned_count]
json.all_count @conversations_count[:all_count]
end
-
json.payload do
json.array! @conversations do |conversation|
- json.meta do
- json.sender do
- json.id conversation.contact.id
- json.name conversation.contact.name
- json.thumbnail conversation.contact.avatar_url
- json.channel conversation.inbox.try(:channel_type)
- end
- json.assignee conversation.assignee
- end
-
- json.id conversation.display_id
- if conversation.unread_incoming_messages.count.zero?
- json.messages [conversation.messages.last.try(:push_event_data)]
- else
- json.messages conversation.unread_messages.map(&:push_event_data)
- end
- json.inbox_id conversation.inbox_id
- json.status conversation.status
- json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
- json.user_last_seen_at conversation.user_last_seen_at.to_i
- json.agent_last_seen_at conversation.agent_last_seen_at.to_i
- json.unread_count conversation.unread_incoming_messages.count
+ json.partial! 'api/v1/conversations/partials/conversation.json.jbuilder', conversation: conversation
end
end
end
diff --git a/app/views/api/v1/conversations/messages/index.json.jbuilder b/app/views/api/v1/conversations/messages/index.json.jbuilder
new file mode 100644
index 000000000..f9b88c36c
--- /dev/null
+++ b/app/views/api/v1/conversations/messages/index.json.jbuilder
@@ -0,0 +1,20 @@
+json.meta do
+ json.labels @conversation.label_list
+ json.additional_attributes @conversation.additional_attributes
+ json.contact_id @conversation.contact_id
+end
+
+json.payload do
+ json.array! @messages do |message|
+ json.id message.id
+ json.content message.content
+ json.inbox_id message.inbox_id
+ json.conversation_id message.conversation.display_id
+ json.message_type message.message_type_before_type_cast
+ json.created_at message.created_at.to_i
+ json.private message.private
+ json.source_id message.source_id
+ json.attachment message.attachment.push_event_data if message.attachment
+ json.sender message.user.push_event_data if message.user
+ end
+end
diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
new file mode 100644
index 000000000..c4e416d81
--- /dev/null
+++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
@@ -0,0 +1,24 @@
+json.meta do
+ json.sender do
+ json.id conversation.contact.id
+ json.name conversation.contact.name
+ json.thumbnail conversation.contact.avatar_url
+ json.channel conversation.inbox.try(:channel_type)
+ end
+ json.assignee conversation.assignee
+end
+
+json.id conversation.display_id
+if conversation.unread_incoming_messages.count.zero?
+ json.messages [conversation.messages.last.try(:push_event_data)]
+else
+ json.messages conversation.unread_messages.map(&:push_event_data)
+end
+
+json.inbox_id conversation.inbox_id
+json.status conversation.status
+json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
+json.user_last_seen_at conversation.user_last_seen_at.to_i
+json.agent_last_seen_at conversation.agent_last_seen_at.to_i
+json.unread_count conversation.unread_incoming_messages.count
+json.additional_attributes conversation.additional_attributes
diff --git a/app/views/api/v1/conversations/show.json.jbuilder b/app/views/api/v1/conversations/show.json.jbuilder
index f9b88c36c..2d39b121c 100644
--- a/app/views/api/v1/conversations/show.json.jbuilder
+++ b/app/views/api/v1/conversations/show.json.jbuilder
@@ -1,20 +1 @@
-json.meta do
- json.labels @conversation.label_list
- json.additional_attributes @conversation.additional_attributes
- json.contact_id @conversation.contact_id
-end
-
-json.payload do
- json.array! @messages do |message|
- json.id message.id
- json.content message.content
- json.inbox_id message.inbox_id
- json.conversation_id message.conversation.display_id
- json.message_type message.message_type_before_type_cast
- json.created_at message.created_at.to_i
- json.private message.private
- json.source_id message.source_id
- json.attachment message.attachment.push_event_data if message.attachment
- json.sender message.user.push_event_data if message.user
- end
-end
+json.partial! 'api/v1/conversations/partials/conversation.json.jbuilder', conversation: @conversation
diff --git a/config/routes.rb b/config/routes.rb
index 4a4e875a4..474c759d9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -73,8 +73,8 @@ Rails.application.routes.draw do
end
resources :conversations, only: [:index, :show] do
- scope module: :conversations do # for nested controller
- resources :messages, only: [:create]
+ scope module: :conversations do
+ resources :messages, only: [:index, :create]
resources :assignments, only: [:create]
resources :labels, only: [:create, :index]
end
diff --git a/spec/controllers/api/v1/conversations/messages_controller_spec.rb b/spec/controllers/api/v1/conversations/messages_controller_spec.rb
index 7febd1058..e53f65495 100644
--- a/spec/controllers/api/v1/conversations/messages_controller_spec.rb
+++ b/spec/controllers/api/v1/conversations/messages_controller_spec.rb
@@ -31,4 +31,29 @@ RSpec.describe 'Conversation Messages API', type: :request do
end
end
end
+
+ describe 'GET /api/v1/conversations/:id/messages' do
+ let(:conversation) { create(:conversation, account: account) }
+
+ context 'when it is an unauthenticated user' do
+ it 'returns unauthorized' do
+ get "/api/v1/conversations/#{conversation.display_id}/messages"
+
+ expect(response).to have_http_status(:unauthorized)
+ end
+ end
+
+ context 'when it is an authenticated user' do
+ let(:agent) { create(:user, account: account, role: :agent) }
+
+ it 'shows the conversation' do
+ get "/api/v1/conversations/#{conversation.display_id}/messages",
+ headers: agent.create_new_auth_token,
+ as: :json
+
+ expect(response).to have_http_status(:success)
+ expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id)
+ end
+ end
+ end
end
diff --git a/spec/controllers/api/v1/conversations_controller_spec.rb b/spec/controllers/api/v1/conversations_controller_spec.rb
index c30ea6752..b9c619b71 100644
--- a/spec/controllers/api/v1/conversations_controller_spec.rb
+++ b/spec/controllers/api/v1/conversations_controller_spec.rb
@@ -51,7 +51,7 @@ RSpec.describe 'Conversations API', type: :request do
as: :json
expect(response).to have_http_status(:success)
- expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id)
+ expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(conversation.display_id)
end
end
end
diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb
index 68bdefc53..437165c4d 100644
--- a/spec/models/conversation_spec.rb
+++ b/spec/models/conversation_spec.rb
@@ -226,6 +226,7 @@ RSpec.describe Conversation, type: :model do
let(:conversation) { create(:conversation) }
let(:expected_data) do
{
+ additional_attributes: nil,
meta: {
sender: conversation.contact.push_event_data,
assignee: conversation.assignee
diff --git a/spec/presenters/conversations/event_data_presenter_spec.rb b/spec/presenters/conversations/event_data_presenter_spec.rb
index d071e2892..82a9dc771 100644
--- a/spec/presenters/conversations/event_data_presenter_spec.rb
+++ b/spec/presenters/conversations/event_data_presenter_spec.rb
@@ -13,6 +13,7 @@ RSpec.describe Conversations::EventDataPresenter do
describe '#push_data' do
let(:expected_data) do
{
+ additional_attributes: nil,
meta: {
sender: conversation.contact.push_event_data,
assignee: conversation.assignee
diff --git a/spec/services/twitter/send_reply_service_spec.rb b/spec/services/twitter/send_reply_service_spec.rb
index ff1803710..140144e83 100644
--- a/spec/services/twitter/send_reply_service_spec.rb
+++ b/spec/services/twitter/send_reply_service_spec.rb
@@ -4,6 +4,7 @@ describe Twitter::SendReplyService do
subject(:send_reply_service) { described_class.new(message: message) }
let(:twitter_client) { instance_double(::Twitty::Facade) }
+ let(:twitter_response) { instance_double(::Twitty::Response) }
let(:account) { create(:account) }
let(:widget_inbox) { create(:inbox, account: account) }
let(:twitter_channel) { create(:channel_twitter_profile, account: account) }
@@ -32,7 +33,9 @@ describe Twitter::SendReplyService do
before do
allow(::Twitty::Facade).to receive(:new).and_return(twitter_client)
allow(twitter_client).to receive(:send_direct_message).and_return(true)
- allow(twitter_client).to receive(:send_tweet_reply).and_return(true)
+ allow(twitter_client).to receive(:send_tweet_reply).and_return(twitter_response)
+ allow(twitter_response).to receive(:status).and_return('200')
+ allow(twitter_response).to receive(:body).and_return(JSON.parse({ id_str: '12345' }.to_json))
end
describe '#perform' do
@@ -61,14 +64,15 @@ describe Twitter::SendReplyService do
context 'with reply' do
it 'if conversation is a direct message' do
create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: dm_conversation)
- create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: dm_conversation)
+ create(:message, message_type: :outgoing, inbox: twitter_inbox, account: account, conversation: dm_conversation)
expect(twitter_client).to have_received(:send_direct_message)
end
it 'if conversation is a tweet' do
create(:message, message_type: :incoming, inbox: twitter_inbox, account: account, conversation: tweet_conversation)
- create(:message, message_type: 'outgoing', inbox: twitter_inbox, account: account, conversation: tweet_conversation)
+ tweet = create(:message, message_type: :outgoing, inbox: twitter_inbox, account: account, conversation: tweet_conversation)
expect(twitter_client).to have_received(:send_tweet_reply)
+ expect(tweet.reload.source_id).to eq '12345'
end
end
end