2019-10-29 07:20:54 +00:00
|
|
|
import BaseActionCableConnector from '../../shared/helpers/BaseActionCableConnector';
|
|
|
|
|
|
|
|
class ActionCableConnector extends BaseActionCableConnector {
|
|
|
|
constructor(app, pubsubToken) {
|
|
|
|
super(app, pubsubToken);
|
|
|
|
this.events = {
|
|
|
|
'message.created': this.onMessageCreated,
|
2020-04-17 15:45:20 +00:00
|
|
|
'message.updated': this.onMessageUpdated,
|
2020-05-04 17:37:56 +00:00
|
|
|
'conversation.typing_on': this.onTypingOn,
|
|
|
|
'conversation.typing_off': this.onTypingOff,
|
2021-04-28 15:23:23 +00:00
|
|
|
'conversation.status_changed': this.onStatusChange,
|
2020-07-04 06:12:47 +00:00
|
|
|
'presence.update': this.onPresenceUpdate,
|
2021-08-31 10:00:18 +00:00
|
|
|
'contact.merged': this.onContactMerge,
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:00:18 +00:00
|
|
|
static refreshConnector = pubsubToken => {
|
|
|
|
if (!pubsubToken || window.chatwootPubsubToken === pubsubToken) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.chatwootPubsubToken = pubsubToken;
|
|
|
|
window.actionCable.disconnect();
|
|
|
|
window.actionCable = new ActionCableConnector(
|
|
|
|
window.WOOT_WIDGET,
|
|
|
|
window.chatwootPubsubToken
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-05-09 16:32:43 +00:00
|
|
|
onStatusChange = data => {
|
|
|
|
this.app.$store.dispatch('conversationAttributes/update', data);
|
|
|
|
};
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
onMessageCreated = data => {
|
2021-09-29 08:58:32 +00:00
|
|
|
console.log('onMessageUpdated', data);
|
|
|
|
this.app.$store.dispatch('messageV2/addOrUpdate', data).then(() => {
|
|
|
|
window.bus.$emit('on-agent-message-recieved');
|
|
|
|
});
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
2020-04-17 15:45:20 +00:00
|
|
|
|
|
|
|
onMessageUpdated = data => {
|
2021-09-29 08:58:32 +00:00
|
|
|
console.log('onMessageUpdated', data);
|
|
|
|
this.app.$store.dispatch('messageV2/addOrUpdate', data).then(() => {
|
|
|
|
window.bus.$emit('on-agent-message-recieved');
|
|
|
|
});
|
2020-04-17 15:45:20 +00:00
|
|
|
};
|
2020-05-04 17:37:56 +00:00
|
|
|
|
2020-07-04 06:12:47 +00:00
|
|
|
onPresenceUpdate = data => {
|
|
|
|
this.app.$store.dispatch('agent/updatePresence', data.users);
|
|
|
|
};
|
|
|
|
|
2021-08-31 10:00:18 +00:00
|
|
|
onContactMerge = data => {
|
|
|
|
const { pubsub_token: pubsubToken } = data;
|
|
|
|
ActionCableConnector.refreshConnector(pubsubToken);
|
|
|
|
};
|
|
|
|
|
2020-05-04 17:37:56 +00:00
|
|
|
onTypingOn = () => {
|
|
|
|
this.clearTimer();
|
|
|
|
this.app.$store.dispatch('conversation/toggleAgentTyping', {
|
|
|
|
status: 'on',
|
|
|
|
});
|
|
|
|
this.initTimer();
|
|
|
|
};
|
|
|
|
|
|
|
|
onTypingOff = () => {
|
|
|
|
this.clearTimer();
|
|
|
|
this.app.$store.dispatch('conversation/toggleAgentTyping', {
|
|
|
|
status: 'off',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
clearTimer = () => {
|
|
|
|
if (this.CancelTyping) {
|
|
|
|
clearTimeout(this.CancelTyping);
|
|
|
|
this.CancelTyping = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
initTimer = () => {
|
|
|
|
// Turn off typing automatically after 30 seconds
|
|
|
|
this.CancelTyping = setTimeout(() => {
|
|
|
|
this.onTypingOff();
|
|
|
|
}, 30000);
|
|
|
|
};
|
2019-10-29 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:00:18 +00:00
|
|
|
export const refreshActionCableConnector =
|
|
|
|
ActionCableConnector.refreshConnector;
|
2020-04-03 07:34:58 +00:00
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
export default ActionCableConnector;
|