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,
|
2020-05-09 16:32:43 +00:00
|
|
|
'conversation.resolved': this.onStatusChange,
|
|
|
|
'conversation.opened': this.onStatusChange,
|
2020-07-04 06:12:47 +00:00
|
|
|
'presence.update': this.onPresenceUpdate,
|
2019-10-29 07:20:54 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 => {
|
2020-07-07 18:34:44 +00:00
|
|
|
this.app.$store.dispatch('conversation/addMessage', 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 => {
|
|
|
|
this.app.$store.dispatch('conversation/updateMessage', data);
|
|
|
|
};
|
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);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
export const refreshActionCableConnector = pubsubToken => {
|
2020-06-08 13:43:33 +00:00
|
|
|
if (!pubsubToken || window.chatwootPubsubToken === pubsubToken) {
|
2020-04-10 11:12:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-03 07:34:58 +00:00
|
|
|
window.chatwootPubsubToken = pubsubToken;
|
|
|
|
window.actionCable.disconnect();
|
|
|
|
window.actionCable = new ActionCableConnector(
|
|
|
|
window.WOOT_WIDGET,
|
|
|
|
window.chatwootPubsubToken
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
export default ActionCableConnector;
|