2021-01-17 17:14:03 +00:00
|
|
|
import {
|
2021-02-15 18:44:13 +00:00
|
|
|
createConversationAPI,
|
2021-01-17 17:14:03 +00:00
|
|
|
sendMessageAPI,
|
|
|
|
getMessagesAPI,
|
|
|
|
sendAttachmentAPI,
|
|
|
|
toggleTyping,
|
|
|
|
setUserLastSeenAt,
|
2022-03-15 16:37:30 +00:00
|
|
|
toggleStatus,
|
2021-01-17 17:14:03 +00:00
|
|
|
} from 'widget/api/conversation';
|
|
|
|
|
2021-07-15 08:57:37 +00:00
|
|
|
import { createTemporaryMessage, getNonDeletedMessages } from './helpers';
|
2021-01-17 17:14:03 +00:00
|
|
|
|
|
|
|
export const actions = {
|
2021-06-30 15:39:44 +00:00
|
|
|
createConversation: async ({ commit, dispatch }, params) => {
|
2021-02-15 18:44:13 +00:00
|
|
|
commit('setConversationUIFlag', { isCreating: true });
|
|
|
|
try {
|
|
|
|
const { data } = await createConversationAPI(params);
|
2021-11-22 18:02:17 +00:00
|
|
|
const { messages } = data;
|
2021-02-15 18:44:13 +00:00
|
|
|
const [message = {}] = messages;
|
|
|
|
commit('pushMessageToConversation', message);
|
2021-06-30 15:39:44 +00:00
|
|
|
dispatch('conversationAttributes/getAttributes', {}, { root: true });
|
2021-02-15 18:44:13 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
} finally {
|
|
|
|
commit('setConversationUIFlag', { isCreating: false });
|
|
|
|
}
|
|
|
|
},
|
2021-12-21 06:32:43 +00:00
|
|
|
sendMessage: async ({ dispatch }, params) => {
|
2021-01-17 17:14:03 +00:00
|
|
|
const { content } = params;
|
2021-12-21 06:32:43 +00:00
|
|
|
const message = createTemporaryMessage({ content });
|
|
|
|
|
|
|
|
dispatch('sendMessageWithData', message);
|
|
|
|
},
|
|
|
|
sendMessageWithData: async ({ commit }, message) => {
|
|
|
|
const { id, content, meta = {} } = message;
|
|
|
|
|
|
|
|
commit('pushMessageToConversation', message);
|
|
|
|
commit('updateMessageMeta', { id, meta: { ...meta, error: '' } });
|
|
|
|
try {
|
|
|
|
const { data } = await sendMessageAPI(content);
|
|
|
|
|
|
|
|
commit('deleteMessage', message.id);
|
|
|
|
commit('pushMessageToConversation', { ...data, status: 'sent' });
|
|
|
|
} catch (error) {
|
|
|
|
commit('pushMessageToConversation', { ...message, status: 'failed' });
|
|
|
|
commit('updateMessageMeta', {
|
|
|
|
id,
|
|
|
|
meta: { ...meta, error: '' },
|
|
|
|
});
|
|
|
|
}
|
2021-01-17 17:14:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sendAttachment: async ({ commit }, params) => {
|
|
|
|
const {
|
|
|
|
attachment: { thumbUrl, fileType },
|
2021-12-21 06:32:43 +00:00
|
|
|
meta = {},
|
2021-01-17 17:14:03 +00:00
|
|
|
} = params;
|
|
|
|
const attachment = {
|
|
|
|
thumb_url: thumbUrl,
|
|
|
|
data_url: thumbUrl,
|
|
|
|
file_type: fileType,
|
|
|
|
status: 'in_progress',
|
|
|
|
};
|
|
|
|
const tempMessage = createTemporaryMessage({
|
|
|
|
attachments: [attachment],
|
|
|
|
});
|
|
|
|
commit('pushMessageToConversation', tempMessage);
|
|
|
|
try {
|
|
|
|
const { data } = await sendAttachmentAPI(params);
|
|
|
|
commit('updateAttachmentMessageStatus', {
|
|
|
|
message: data,
|
|
|
|
tempId: tempMessage.id,
|
|
|
|
});
|
2021-12-21 06:32:43 +00:00
|
|
|
commit('pushMessageToConversation', { ...data, status: 'sent' });
|
2021-01-17 17:14:03 +00:00
|
|
|
} catch (error) {
|
2021-12-21 06:32:43 +00:00
|
|
|
commit('pushMessageToConversation', { ...tempMessage, status: 'failed' });
|
|
|
|
commit('updateMessageMeta', {
|
|
|
|
id: tempMessage.id,
|
|
|
|
meta: { ...meta, error: '' },
|
|
|
|
});
|
2021-01-17 17:14:03 +00:00
|
|
|
// Show error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fetchOldConversations: async ({ commit }, { before } = {}) => {
|
|
|
|
try {
|
|
|
|
commit('setConversationListLoading', true);
|
2022-03-31 12:05:39 +00:00
|
|
|
const {
|
|
|
|
data: { payload, meta },
|
|
|
|
} = await getMessagesAPI({ before });
|
|
|
|
const { contact_last_seen_at: lastSeen } = meta;
|
|
|
|
const formattedMessages = getNonDeletedMessages({ messages: payload });
|
|
|
|
commit('conversation/setMetaUserLastSeenAt', lastSeen, { root: true });
|
2021-07-15 08:57:37 +00:00
|
|
|
commit('setMessagesInConversation', formattedMessages);
|
2021-01-17 17:14:03 +00:00
|
|
|
commit('setConversationListLoading', false);
|
|
|
|
} catch (error) {
|
|
|
|
commit('setConversationListLoading', false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clearConversations: ({ commit }) => {
|
|
|
|
commit('clearConversations');
|
|
|
|
},
|
|
|
|
|
2021-07-15 08:57:37 +00:00
|
|
|
addOrUpdateMessage: async ({ commit }, data) => {
|
|
|
|
const { id, content_attributes } = data;
|
|
|
|
if (content_attributes && content_attributes.deleted) {
|
|
|
|
commit('deleteMessage', id);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-17 17:14:03 +00:00
|
|
|
commit('pushMessageToConversation', data);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleAgentTyping({ commit }, data) {
|
|
|
|
commit('toggleAgentTypingStatus', data);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleUserTyping: async (_, data) => {
|
|
|
|
try {
|
|
|
|
await toggleTyping(data);
|
|
|
|
} catch (error) {
|
|
|
|
// IgnoreError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setUserLastSeen: async ({ commit, getters: appGetters }) => {
|
|
|
|
if (!appGetters.getConversationSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastSeen = Date.now() / 1000;
|
|
|
|
try {
|
|
|
|
commit('setMetaUserLastSeenAt', lastSeen);
|
|
|
|
await setUserLastSeenAt({ lastSeen });
|
|
|
|
} catch (error) {
|
|
|
|
// IgnoreError
|
|
|
|
}
|
|
|
|
},
|
2022-03-15 16:37:30 +00:00
|
|
|
|
|
|
|
resolveConversation: async () => {
|
|
|
|
await toggleStatus();
|
|
|
|
},
|
2021-01-17 17:14:03 +00:00
|
|
|
};
|