/
actionCable.js
79 lines (68 loc) 路 2.08 KB
/
actionCable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import BaseActionCableConnector from '../../shared/helpers/BaseActionCableConnector';
import { playNewMessageNotificationInWidget } from 'shared/helpers/AudioNotificationHelper';
class ActionCableConnector extends BaseActionCableConnector {
constructor(app, pubsubToken) {
super(app, pubsubToken);
this.events = {
'message.created': this.onMessageCreated,
'message.updated': this.onMessageUpdated,
'conversation.typing_on': this.onTypingOn,
'conversation.typing_off': this.onTypingOff,
'conversation.status_changed': this.onStatusChange,
'presence.update': this.onPresenceUpdate,
'contact.merged': this.onContactMerge,
};
}
onStatusChange = data => {
this.app.$store.dispatch('conversationAttributes/update', data);
};
onMessageCreated = data => {
this.app.$store
.dispatch('conversation/addOrUpdateMessage', data)
.then(() => {
window.bus.$emit('on-agent-message-received');
});
if (data.sender_type === 'User') {
playNewMessageNotificationInWidget();
}
};
onMessageUpdated = data => {
this.app.$store.dispatch('conversation/addOrUpdateMessage', data);
};
onPresenceUpdate = data => {
this.app.$store.dispatch('agent/updatePresence', data.users);
};
onContactMerge = data => {
const { pubsub_token: pubsubToken } = data;
ActionCableConnector.refreshConnector(pubsubToken);
};
onTypingOn = data => {
if (data.is_private) {
return;
}
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);
};
}
export default ActionCableConnector;