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,
|
2020-05-09 16:32:43 +00:00
|
|
|
getMessagesAPI,
|
2020-03-30 06:45:06 +00:00
|
|
|
sendAttachmentAPI,
|
2020-05-04 17:37:56 +00:00
|
|
|
toggleTyping,
|
2020-07-07 18:34:44 +00:00
|
|
|
setUserLastSeenAt,
|
2020-03-30 06:45:06 +00:00
|
|
|
} 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';
|
2020-09-14 09:44:26 +00:00
|
|
|
import { formatUnixDate } from 'shared/helpers/DateHelper';
|
2020-05-17 17:19:45 +00:00
|
|
|
import { isASubmittedFormMessage } from 'shared/helpers/MessageTypeHelper';
|
2019-12-14 18:36:01 +00:00
|
|
|
|
2020-05-17 17:19:45 +00:00
|
|
|
import getUuid from '../../helpers/uuid';
|
2019-12-14 18:36:01 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-17 17:19:45 +00:00
|
|
|
const getSenderName = message => (message.sender ? message.sender.name : '');
|
|
|
|
|
|
|
|
const shouldShowAvatar = (message, nextMessage) => {
|
|
|
|
const currentSender = getSenderName(message);
|
|
|
|
const nextSender = getSenderName(nextMessage);
|
|
|
|
|
|
|
|
return (
|
|
|
|
currentSender !== nextSender ||
|
|
|
|
message.message_type !== nextMessage.message_type ||
|
|
|
|
isASubmittedFormMessage(nextMessage)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const groupConversationBySender = conversationsForADate =>
|
|
|
|
conversationsForADate.map((message, index) => {
|
|
|
|
let showAvatar = false;
|
|
|
|
const isLastMessage = index === conversationsForADate.length - 1;
|
|
|
|
if (isASubmittedFormMessage(message)) {
|
|
|
|
showAvatar = false;
|
|
|
|
} else if (isLastMessage) {
|
|
|
|
showAvatar = true;
|
|
|
|
} else {
|
|
|
|
const nextMessage = conversationsForADate[index + 1];
|
|
|
|
showAvatar = shouldShowAvatar(message, nextMessage);
|
|
|
|
}
|
|
|
|
return { showAvatar, ...message };
|
|
|
|
});
|
|
|
|
|
2019-11-29 12:12:35 +00:00
|
|
|
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
|
|
|
|
2020-07-07 18:34:44 +00:00
|
|
|
export const onNewMessageCreated = data => {
|
|
|
|
const { message_type: messageType } = data;
|
|
|
|
const isIncomingMessage = messageType === MESSAGE_TYPE.OUTGOING;
|
|
|
|
|
|
|
|
if (isIncomingMessage) {
|
|
|
|
playNotificationAudio();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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: {},
|
2020-07-07 18:34:44 +00:00
|
|
|
meta: {
|
|
|
|
userLastSeenAt: undefined,
|
|
|
|
},
|
2019-12-11 15:27:06 +00:00
|
|
|
uiFlags: {
|
|
|
|
allMessagesLoaded: false,
|
|
|
|
isFetchingList: false,
|
2020-05-04 17:37:56 +00:00
|
|
|
isAgentTyping: false,
|
2019-12-11 15:27:06 +00:00
|
|
|
},
|
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,
|
2020-05-04 17:37:56 +00:00
|
|
|
getIsAgentTyping: _state => _state.uiFlags.isAgentTyping,
|
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),
|
2020-09-14 09:44:26 +00:00
|
|
|
message => formatUnixDate(message.created_at)
|
2019-12-14 18:36:01 +00:00
|
|
|
);
|
2020-05-17 17:19:45 +00:00
|
|
|
return Object.keys(conversationGroupedByDate).map(date => ({
|
|
|
|
date,
|
|
|
|
messages: groupConversationBySender(conversationGroupedByDate[date]),
|
|
|
|
}));
|
2019-12-14 18:36:01 +00:00
|
|
|
},
|
|
|
|
getIsFetchingList: _state => _state.uiFlags.isFetchingList,
|
2020-07-07 18:34:44 +00:00
|
|
|
getUnreadMessageCount: _state => {
|
|
|
|
const { userLastSeenAt } = _state.meta;
|
|
|
|
const count = Object.values(_state.conversations).filter(chat => {
|
|
|
|
const { created_at: createdAt, message_type: messageType } = chat;
|
|
|
|
const isOutGoing = messageType === MESSAGE_TYPE.OUTGOING;
|
|
|
|
const hasNotSeen = userLastSeenAt
|
|
|
|
? createdAt * 1000 > userLastSeenAt * 1000
|
|
|
|
: true;
|
|
|
|
return hasNotSeen && isOutGoing;
|
|
|
|
}).length;
|
|
|
|
return count;
|
|
|
|
},
|
|
|
|
getUnreadTextMessages: (_state, _getters) => {
|
|
|
|
const unreadCount = _getters.getUnreadMessageCount;
|
|
|
|
const allMessages = [...Object.values(_state.conversations)];
|
|
|
|
const unreadAgentMessages = allMessages.filter(message => {
|
|
|
|
const { message_type: messageType } = message;
|
|
|
|
return messageType === MESSAGE_TYPE.OUTGOING;
|
|
|
|
});
|
|
|
|
const maxUnreadCount = Math.min(unreadCount, 3);
|
|
|
|
const allUnreadMessages = unreadAgentMessages.splice(-maxUnreadCount);
|
|
|
|
return allUnreadMessages;
|
|
|
|
},
|
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-07-07 18:34:44 +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);
|
2020-05-09 16:32:43 +00:00
|
|
|
const { data } = await getMessagesAPI({ before });
|
2019-12-11 15:27:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-07 18:34:44 +00:00
|
|
|
addMessage: async ({ commit }, data) => {
|
2019-12-11 15:27:06 +00:00
|
|
|
commit('pushMessageToConversation', data);
|
2020-07-07 18:34:44 +00:00
|
|
|
onNewMessageCreated(data);
|
2019-10-29 07:20:54 +00:00
|
|
|
},
|
2020-04-17 15:45:20 +00:00
|
|
|
|
|
|
|
updateMessage({ commit }, data) {
|
|
|
|
commit('pushMessageToConversation', data);
|
|
|
|
},
|
2020-05-04 17:37:56 +00:00
|
|
|
|
|
|
|
toggleAgentTyping({ commit }, data) {
|
|
|
|
commit('toggleAgentTypingStatus', data);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleUserTyping: async (_, data) => {
|
|
|
|
try {
|
|
|
|
await toggleTyping(data);
|
|
|
|
} catch (error) {
|
2020-07-07 18:34:44 +00:00
|
|
|
// 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
|
2020-05-04 17:37:56 +00:00
|
|
|
}
|
|
|
|
},
|
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
|
|
|
};
|
|
|
|
},
|
2020-05-04 17:37:56 +00:00
|
|
|
|
|
|
|
toggleAgentTypingStatus($state, { status }) {
|
|
|
|
const isTyping = status === 'on';
|
|
|
|
$state.uiFlags.isAgentTyping = isTyping;
|
|
|
|
},
|
2020-07-07 18:34:44 +00:00
|
|
|
|
|
|
|
setMetaUserLastSeenAt($state, lastSeen) {
|
|
|
|
$state.meta.userLastSeenAt = lastSeen;
|
|
|
|
},
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
getters,
|
|
|
|
actions,
|
|
|
|
mutations,
|
|
|
|
};
|