2019-08-14 09:48:44 +00:00
|
|
|
/* eslint-env browser */
|
|
|
|
/* eslint no-console: 0 */
|
|
|
|
import Pusher from 'pusher-js';
|
|
|
|
import AuthAPI from '../api/auth';
|
|
|
|
import CONSTANTS from '../constants';
|
|
|
|
|
|
|
|
class VuePusher {
|
|
|
|
constructor(apiKey, options) {
|
|
|
|
this.app = options.app;
|
|
|
|
this.pusher = new Pusher(apiKey, options);
|
|
|
|
this.channels = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(channelName) {
|
|
|
|
const channel = this.pusher.subscribe(channelName);
|
|
|
|
if (!this.channels.includes(channel)) {
|
|
|
|
this.channels.push(channelName);
|
|
|
|
}
|
|
|
|
this.bindEvent(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe(channelName) {
|
|
|
|
this.pusher.unsubscribe(channelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvent(channel) {
|
2019-09-02 14:56:28 +00:00
|
|
|
channel.bind('message.created', data => {
|
2019-08-14 09:48:44 +00:00
|
|
|
this.app.$store.dispatch('addMessage', data);
|
|
|
|
});
|
|
|
|
|
2019-09-02 14:56:28 +00:00
|
|
|
channel.bind('conversation.created', data => {
|
2019-08-14 09:48:44 +00:00
|
|
|
this.app.$store.dispatch('addConversation', data);
|
|
|
|
});
|
|
|
|
|
2019-09-02 14:56:28 +00:00
|
|
|
channel.bind('status_change:conversation', data => {
|
2019-08-14 09:48:44 +00:00
|
|
|
this.app.$store.dispatch('addConversation', data);
|
|
|
|
});
|
|
|
|
|
2019-09-02 14:56:28 +00:00
|
|
|
channel.bind('assignee.changed', payload => {
|
2019-09-05 07:00:36 +00:00
|
|
|
const { meta = {}, id } = payload;
|
|
|
|
const { assignee } = meta || {};
|
2019-08-14 09:48:44 +00:00
|
|
|
if (id) {
|
2019-09-05 07:00:36 +00:00
|
|
|
this.app.$store.dispatch('updateAssignee', { id, assignee });
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-05 07:00:36 +00:00
|
|
|
channel.bind('user:logout', () => AuthAPI.logout());
|
|
|
|
channel.bind('page:reload', () => window.location.reload());
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 07:29:56 +00:00
|
|
|
/* eslint no-param-reassign: ["error", { "props": false }] */
|
2019-08-14 09:48:44 +00:00
|
|
|
export default {
|
|
|
|
init() {
|
|
|
|
// Log only if env is testing or development.
|
2019-08-30 22:38:00 +00:00
|
|
|
Pusher.logToConsole = CONSTANTS.PUSHER.logToConsole || true;
|
2019-08-14 09:48:44 +00:00
|
|
|
// Init Pusher
|
2019-08-21 07:29:56 +00:00
|
|
|
const options = {
|
|
|
|
encrypted: true,
|
|
|
|
app: window.WOOT,
|
|
|
|
cluster: CONSTANTS.PUSHER.cluster,
|
|
|
|
};
|
2019-08-14 09:48:44 +00:00
|
|
|
const pusher = new VuePusher(CONSTANTS.PUSHER.token, options);
|
|
|
|
// Add to global Obj
|
|
|
|
if (AuthAPI.isLoggedIn()) {
|
2019-10-16 21:48:07 +00:00
|
|
|
pusher.subscribe(AuthAPI.getPubSubToken());
|
2019-08-14 09:48:44 +00:00
|
|
|
return pusher.pusher;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
};
|