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';
|
|
|
|
|
|
|
|
const ding = require('../assets/audio/ding.mp3');
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
// eslint-disable-next-line
|
2019-08-14 09:48:44 +00:00
|
|
|
bindEvent(channel) {
|
2019-08-30 22:38:00 +00:00
|
|
|
channel.bind('message.created', function messageCreate(data) {
|
2019-08-14 09:48:44 +00:00
|
|
|
// Play sound if incoming
|
|
|
|
if (!data.message_type) {
|
|
|
|
new Audio(ding).play();
|
|
|
|
}
|
|
|
|
this.app.$store.dispatch('addMessage', data);
|
|
|
|
});
|
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
channel.bind('conversation.created', function conversationCreated(data) {
|
2019-08-14 09:48:44 +00:00
|
|
|
this.app.$store.dispatch('addConversation', data);
|
|
|
|
});
|
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
channel.bind('status_change:conversation', function statusChange(data) {
|
2019-08-14 09:48:44 +00:00
|
|
|
this.app.$store.dispatch('addConversation', data);
|
|
|
|
});
|
|
|
|
|
2019-08-30 22:38:00 +00:00
|
|
|
channel.bind('assignee.changed', function assigneeChanged(payload) {
|
2019-08-14 09:48:44 +00:00
|
|
|
if (!payload.meta) return;
|
|
|
|
const { assignee } = payload.meta;
|
|
|
|
const { id } = payload;
|
|
|
|
if (id) {
|
|
|
|
this.app.$store.dispatch('updateAssignee', {
|
|
|
|
id,
|
|
|
|
assignee,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
channel.bind('user:logout', () => {
|
|
|
|
AuthAPI.logout();
|
|
|
|
});
|
|
|
|
|
|
|
|
channel.bind('page:reload', () => {
|
2019-08-21 07:29:56 +00:00
|
|
|
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()) {
|
|
|
|
pusher.subscribe(AuthAPI.getChannel());
|
|
|
|
return pusher.pusher;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
};
|