2019-10-24 20:07:01 +00:00
|
|
|
import AuthAPI from '../api/auth';
|
2019-10-29 07:20:54 +00:00
|
|
|
import BaseActionCableConnector from '../../shared/helpers/BaseActionCableConnector';
|
2021-03-04 13:54:03 +00:00
|
|
|
import { newMessageNotification } from 'shared/helpers/AudioNotificationHelper';
|
2019-10-24 20:07:01 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
class ActionCableConnector extends BaseActionCableConnector {
|
2019-10-24 20:07:01 +00:00
|
|
|
constructor(app, pubsubToken) {
|
2021-05-28 13:51:16 +00:00
|
|
|
const { websocketURL = '' } = window.chatwootConfig || {};
|
|
|
|
super(app, pubsubToken, websocketURL);
|
2020-05-04 17:37:56 +00:00
|
|
|
this.CancelTyping = [];
|
2019-10-24 20:07:01 +00:00
|
|
|
this.events = {
|
|
|
|
'message.created': this.onMessageCreated,
|
2020-04-17 15:45:20 +00:00
|
|
|
'message.updated': this.onMessageUpdated,
|
2019-10-24 20:07:01 +00:00
|
|
|
'conversation.created': this.onConversationCreated,
|
2021-04-28 15:23:23 +00:00
|
|
|
'conversation.status_changed': this.onStatusChange,
|
2019-10-24 20:07:01 +00:00
|
|
|
'user:logout': this.onLogout,
|
|
|
|
'page:reload': this.onReload,
|
|
|
|
'assignee.changed': this.onAssigneeChanged,
|
2020-05-04 17:37:56 +00:00
|
|
|
'conversation.typing_on': this.onTypingOn,
|
|
|
|
'conversation.typing_off': this.onTypingOff,
|
2020-06-02 17:29:02 +00:00
|
|
|
'conversation.contact_changed': this.onConversationContactChange,
|
2020-07-04 06:12:47 +00:00
|
|
|
'presence.update': this.onPresenceUpdate,
|
2021-09-23 07:22:49 +00:00
|
|
|
'contact.deleted': this.onContactDelete,
|
2019-10-24 20:07:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:08:48 +00:00
|
|
|
isAValidEvent = data => {
|
|
|
|
return this.app.$store.getters.getCurrentAccountId === data.account_id;
|
|
|
|
};
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
onMessageUpdated = data => {
|
|
|
|
this.app.$store.dispatch('updateMessage', data);
|
|
|
|
};
|
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
onPresenceUpdate = data => {
|
|
|
|
this.app.$store.dispatch('contacts/updatePresence', data.contacts);
|
|
|
|
this.app.$store.dispatch('agents/updatePresence', data.users);
|
2021-10-14 19:00:48 +00:00
|
|
|
this.app.$store.dispatch('setCurrentUserAvailability', data.users);
|
2020-07-04 06:12:47 +00:00
|
|
|
};
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
onConversationContactChange = payload => {
|
|
|
|
const { meta = {}, id: conversationId } = payload;
|
|
|
|
const { sender } = meta || {};
|
|
|
|
if (conversationId) {
|
|
|
|
this.app.$store.dispatch('updateConversationContact', {
|
|
|
|
conversationId,
|
|
|
|
...sender,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-24 20:07:01 +00:00
|
|
|
onAssigneeChanged = payload => {
|
2021-03-22 14:54:51 +00:00
|
|
|
const { id } = payload;
|
|
|
|
if (id) {
|
|
|
|
this.app.$store.dispatch('updateConversation', payload);
|
|
|
|
}
|
2020-06-09 10:56:33 +00:00
|
|
|
this.fetchConversationStats();
|
2019-10-24 20:07:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
onConversationCreated = data => {
|
|
|
|
this.app.$store.dispatch('addConversation', data);
|
2020-06-09 10:56:33 +00:00
|
|
|
this.fetchConversationStats();
|
2019-10-24 20:07:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
onLogout = () => AuthAPI.logout();
|
|
|
|
|
|
|
|
onMessageCreated = data => {
|
2021-03-04 13:54:03 +00:00
|
|
|
newMessageNotification(data);
|
2019-10-24 20:07:01 +00:00
|
|
|
this.app.$store.dispatch('addMessage', data);
|
|
|
|
};
|
|
|
|
|
|
|
|
onReload = () => window.location.reload();
|
|
|
|
|
|
|
|
onStatusChange = data => {
|
2020-04-18 14:55:58 +00:00
|
|
|
this.app.$store.dispatch('updateConversation', data);
|
2020-06-09 10:56:33 +00:00
|
|
|
this.fetchConversationStats();
|
2019-10-24 20:07:01 +00:00
|
|
|
};
|
2020-05-04 17:37:56 +00:00
|
|
|
|
|
|
|
onTypingOn = ({ conversation, user }) => {
|
|
|
|
const conversationId = conversation.id;
|
|
|
|
|
|
|
|
this.clearTimer(conversationId);
|
|
|
|
this.app.$store.dispatch('conversationTypingStatus/create', {
|
|
|
|
conversationId,
|
|
|
|
user,
|
|
|
|
});
|
|
|
|
this.initTimer({ conversation, user });
|
|
|
|
};
|
|
|
|
|
|
|
|
onTypingOff = ({ conversation, user }) => {
|
|
|
|
const conversationId = conversation.id;
|
|
|
|
|
|
|
|
this.clearTimer(conversationId);
|
|
|
|
this.app.$store.dispatch('conversationTypingStatus/destroy', {
|
|
|
|
conversationId,
|
|
|
|
user,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
clearTimer = conversationId => {
|
|
|
|
const timerEvent = this.CancelTyping[conversationId];
|
|
|
|
|
|
|
|
if (timerEvent) {
|
|
|
|
clearTimeout(timerEvent);
|
|
|
|
this.CancelTyping[conversationId] = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
initTimer = ({ conversation, user }) => {
|
|
|
|
const conversationId = conversation.id;
|
|
|
|
// Turn off typing automatically after 30 seconds
|
|
|
|
this.CancelTyping[conversationId] = setTimeout(() => {
|
|
|
|
this.onTypingOff({ conversation, user });
|
|
|
|
}, 30000);
|
|
|
|
};
|
2020-06-09 10:56:33 +00:00
|
|
|
|
|
|
|
fetchConversationStats = () => {
|
|
|
|
bus.$emit('fetch_conversation_stats');
|
|
|
|
};
|
2021-09-23 07:22:49 +00:00
|
|
|
|
|
|
|
onContactDelete = data => {
|
|
|
|
this.app.$store.dispatch(
|
|
|
|
'contacts/deleteContactThroughConversations',
|
|
|
|
data.id
|
|
|
|
);
|
|
|
|
this.fetchConversationStats();
|
|
|
|
};
|
2019-10-24 20:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
init() {
|
|
|
|
if (AuthAPI.isLoggedIn()) {
|
|
|
|
const actionCable = new ActionCableConnector(
|
|
|
|
window.WOOT,
|
|
|
|
AuthAPI.getPubSubToken()
|
|
|
|
);
|
|
|
|
return actionCable;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
};
|