2019-10-29 07:20:54 +00:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
import Vue from 'vue';
|
2020-03-30 06:45:06 +00:00
|
|
|
import {
|
|
|
|
sendMessageAPI,
|
|
|
|
getConversationAPI,
|
|
|
|
sendAttachmentAPI,
|
|
|
|
} from 'widget/api/conversation';
|
2019-11-29 12:12:35 +00:00
|
|
|
import { MESSAGE_TYPE } from 'widget/helpers/constants';
|
2020-02-29 05:57:02 +00:00
|
|
|
import { playNotificationAudio } from 'shared/helpers/AudioNotificationHelper';
|
2019-11-29 12:12:35 +00:00
|
|
|
import getUuid from '../../helpers/uuid';
|
2019-12-14 18:36:01 +00:00
|
|
|
import DateHelper from '../../../shared/helpers/DateHelper';
|
|
|
|
|
|
|
|
const groupBy = require('lodash.groupby');
|
2019-11-29 12:12:35 +00:00
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
export const createTemporaryMessage = ({ attachments, content }) => {
|
2019-12-17 04:17:44 +00:00
|
|
|
const timestamp = new Date().getTime() / 1000;
|
2019-11-29 12:12:35 +00:00
|
|
|
return {
|
|
|
|
id: getUuid(),
|
|
|
|
content,
|
2020-04-17 15:45:20 +00:00
|
|
|
attachments,
|
2019-11-29 12:12:35 +00:00
|
|
|
status: 'in_progress',
|
|
|
|
created_at: timestamp,
|
|
|
|
message_type: MESSAGE_TYPE.INCOMING,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const findUndeliveredMessage = (messageInbox, { content }) =>
|
|
|
|
Object.values(messageInbox).filter(
|
|
|
|
message => message.content === content && message.status === 'in_progress'
|
|
|
|
);
|
2019-10-29 07:20:54 +00:00
|
|
|
|
|
|
|
export const DEFAULT_CONVERSATION = 'default';
|
2019-12-11 15:27:06 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
const state = {
|
|
|
|
conversations: {},
|
2019-12-11 15:27:06 +00:00
|
|
|
uiFlags: {
|
|
|
|
allMessagesLoaded: false,
|
|
|
|
isFetchingList: false,
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const getters = {
|
2019-12-14 18:36:01 +00:00
|
|
|
getAllMessagesLoaded: _state => _state.uiFlags.allMessagesLoaded,
|
2019-10-29 07:20:54 +00:00
|
|
|
getConversation: _state => _state.conversations,
|
2019-11-26 17:05:26 +00:00
|
|
|
getConversationSize: _state => Object.keys(_state.conversations).length,
|
2019-12-11 15:27:06 +00:00
|
|
|
getEarliestMessage: _state => {
|
|
|
|
const conversation = Object.values(_state.conversations);
|
|
|
|
if (conversation.length) {
|
|
|
|
return conversation[0];
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
2019-12-14 18:36:01 +00:00
|
|
|
getGroupedConversation: _state => {
|
2019-12-15 18:23:04 +00:00
|
|
|
const conversationGroupedByDate = groupBy(
|
|
|
|
Object.values(_state.conversations),
|
|
|
|
message => new DateHelper(message.created_at).format()
|
2019-12-14 18:36:01 +00:00
|
|
|
);
|
2019-12-15 18:23:04 +00:00
|
|
|
return Object.keys(conversationGroupedByDate).map(date => {
|
|
|
|
const messages = conversationGroupedByDate[date].map((message, index) => {
|
|
|
|
let showAvatar = false;
|
|
|
|
if (index === conversationGroupedByDate[date].length - 1) {
|
|
|
|
showAvatar = true;
|
|
|
|
} else {
|
|
|
|
const nextMessage = conversationGroupedByDate[date][index + 1];
|
|
|
|
const currentSender = message.sender ? message.sender.name : '';
|
|
|
|
const nextSender = nextMessage.sender ? nextMessage.sender.name : '';
|
2020-02-02 17:04:16 +00:00
|
|
|
showAvatar =
|
|
|
|
currentSender !== nextSender ||
|
|
|
|
message.message_type !== nextMessage.message_type;
|
2019-12-15 18:23:04 +00:00
|
|
|
}
|
2020-02-02 17:04:16 +00:00
|
|
|
return { showAvatar, ...message };
|
2019-12-15 18:23:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
date,
|
|
|
|
messages,
|
|
|
|
};
|
|
|
|
});
|
2019-12-14 18:36:01 +00:00
|
|
|
},
|
|
|
|
getIsFetchingList: _state => _state.uiFlags.isFetchingList,
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const actions = {
|
2019-11-29 12:12:35 +00:00
|
|
|
sendMessage: async ({ commit }, params) => {
|
2019-10-29 07:20:54 +00:00
|
|
|
const { content } = params;
|
2020-03-30 06:45:06 +00:00
|
|
|
commit('pushMessageToConversation', createTemporaryMessage({ content }));
|
2019-10-29 07:20:54 +00:00
|
|
|
await sendMessageAPI(content);
|
|
|
|
},
|
|
|
|
|
2020-03-30 06:45:06 +00:00
|
|
|
sendAttachment: async ({ commit }, params) => {
|
2020-04-02 06:58:38 +00:00
|
|
|
const {
|
|
|
|
attachment: { thumbUrl, fileType },
|
|
|
|
} = params;
|
|
|
|
const attachment = {
|
2020-03-30 06:45:06 +00:00
|
|
|
thumb_url: thumbUrl,
|
|
|
|
data_url: thumbUrl,
|
2020-04-02 06:58:38 +00:00
|
|
|
file_type: fileType,
|
2020-03-30 06:45:06 +00:00
|
|
|
status: 'in_progress',
|
|
|
|
};
|
2020-04-17 15:45:20 +00:00
|
|
|
const tempMessage = createTemporaryMessage({ attachments: [attachment] });
|
2020-03-30 06:45:06 +00:00
|
|
|
commit('pushMessageToConversation', tempMessage);
|
|
|
|
try {
|
|
|
|
const { data } = await sendAttachmentAPI(params);
|
2020-04-17 15:45:20 +00:00
|
|
|
commit('updateAttachmentMessageStatus', {
|
|
|
|
message: data,
|
|
|
|
tempId: tempMessage.id,
|
|
|
|
});
|
2020-03-30 06:45:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Show error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
fetchOldConversations: async ({ commit }, { before } = {}) => {
|
2019-10-29 07:20:54 +00:00
|
|
|
try {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('setConversationListLoading', true);
|
|
|
|
const { data } = await getConversationAPI({ before });
|
|
|
|
commit('setMessagesInConversation', data);
|
|
|
|
commit('setConversationListLoading', false);
|
2019-10-29 07:20:54 +00:00
|
|
|
} catch (error) {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('setConversationListLoading', false);
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addMessage({ commit }, data) {
|
2020-02-29 05:57:02 +00:00
|
|
|
if (data.message_type === MESSAGE_TYPE.OUTGOING) {
|
|
|
|
playNotificationAudio();
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('pushMessageToConversation', data);
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
2020-04-17 15:45:20 +00:00
|
|
|
|
|
|
|
updateMessage({ commit }, data) {
|
|
|
|
commit('pushMessageToConversation', data);
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
export const mutations = {
|
|
|
|
pushMessageToConversation($state, message) {
|
2019-11-29 12:12:35 +00:00
|
|
|
const { id, status, message_type: type } = message;
|
2019-10-29 07:20:54 +00:00
|
|
|
const messagesInbox = $state.conversations;
|
2019-11-29 12:12:35 +00:00
|
|
|
const isMessageIncoming = type === MESSAGE_TYPE.INCOMING;
|
|
|
|
const isTemporaryMessage = status === 'in_progress';
|
|
|
|
|
|
|
|
if (!isMessageIncoming || isTemporaryMessage) {
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [messageInConversation] = findUndeliveredMessage(
|
|
|
|
messagesInbox,
|
|
|
|
message
|
|
|
|
);
|
|
|
|
if (!messageInConversation) {
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
} else {
|
|
|
|
Vue.delete(messagesInbox, messageInConversation.id);
|
|
|
|
Vue.set(messagesInbox, id, message);
|
|
|
|
}
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
updateAttachmentMessageStatus($state, { message, tempId }) {
|
|
|
|
const { id } = message;
|
2020-03-30 06:45:06 +00:00
|
|
|
const messagesInbox = $state.conversations;
|
|
|
|
|
|
|
|
const messageInConversation = messagesInbox[tempId];
|
|
|
|
|
|
|
|
if (messageInConversation) {
|
|
|
|
Vue.delete(messagesInbox, tempId);
|
2020-04-17 15:45:20 +00:00
|
|
|
Vue.set(messagesInbox, id, { ...message });
|
2020-03-30 06:45:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
setConversationListLoading($state, status) {
|
|
|
|
$state.uiFlags.isFetchingList = status;
|
|
|
|
},
|
|
|
|
|
|
|
|
setMessagesInConversation($state, payload) {
|
2019-10-29 07:20:54 +00:00
|
|
|
if (!payload.length) {
|
2019-12-11 15:27:06 +00:00
|
|
|
$state.uiFlags.allMessagesLoaded = true;
|
2019-10-29 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-11 15:27:06 +00:00
|
|
|
payload.map(message => Vue.set($state.conversations, message.id, message));
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
2020-01-09 07:36:40 +00:00
|
|
|
|
|
|
|
updateMessage($state, { id, content_attributes }) {
|
|
|
|
$state.conversations[id] = {
|
|
|
|
...$state.conversations[id],
|
2020-04-10 11:12:37 +00:00
|
|
|
content_attributes: {
|
|
|
|
...($state.conversations[id].content_attributes || {}),
|
|
|
|
...content_attributes,
|
|
|
|
},
|
2020-01-09 07:36:40 +00:00
|
|
|
};
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|