castle-chat-lib
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "castle-chat-lib", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -9,2 +9,5 @@ "main": "src/index.js", | ||
}, | ||
"files": [ | ||
"src" | ||
], | ||
"dependencies": { | ||
@@ -11,0 +14,0 @@ "jquery": "^3.3.1", |
@@ -21,2 +21,3 @@ /** File: candy.js | ||
import Event from './core/event'; | ||
let packageJson = require('../../package.json'); | ||
@@ -39,4 +40,4 @@ import './md5'; | ||
self.about = { | ||
name: 'Candy', | ||
version: '2.2.0', | ||
name: packageJson.name, | ||
version: packageJson.version, | ||
}; | ||
@@ -43,0 +44,0 @@ |
126
src/index.js
@@ -6,8 +6,56 @@ import Candy from './candy/candy'; | ||
export default function() { | ||
let self = {}; | ||
const PRESENCE_DEBOUNCE_DELAY = 100; | ||
const PRESENCE_DEBOUNCE_MAX_WAIT = 500; | ||
const MESSAGE_DEBOUNCE_DELAY = 50; | ||
const MESSAGE_DEBOUNCE_MAX_WAIT = 200; | ||
self.init = (url, userId, token, channels) => { | ||
self._chat = new Candy(); | ||
self._chat.init(url, { | ||
class Roster { | ||
constructor() { | ||
this._users = {}; | ||
} | ||
// returns true if there was a roster change | ||
handlePresenceEvent(args) { | ||
if (!args.user || !args.user.data || !args.user.data.jid) { | ||
return false; | ||
} | ||
let name = args.user.data.jid.split('/')[1]; | ||
if (args.action == 'join') { | ||
if (!this._users[name]) { | ||
this._users[name] = args.user.data; | ||
this._users[name].name = name; | ||
return true; | ||
} | ||
} else if (args.action == 'leave' || args.action == 'kick' || args.action == 'ban') { | ||
if (this._users[name]) { | ||
delete this._users[name]; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
getUsers() { | ||
return _.map(this._users, (user) => { | ||
return { | ||
name: user.name, | ||
}; | ||
}); | ||
} | ||
} | ||
export default class CastleChat { | ||
init(url, userId, token, channels) { | ||
this._chat = new Candy(); | ||
this._rooms = {}; | ||
this._messagesQueue = []; | ||
this._handleMessages = _.debounce(this._handleMessagesUndebounced, MESSAGE_DEBOUNCE_DELAY, { | ||
maxWait: MESSAGE_DEBOUNCE_MAX_WAIT, | ||
}); | ||
this._chat.init(url, { | ||
core: { | ||
@@ -19,3 +67,3 @@ autojoin: _.map(channels, (channel) => `${channel}@conference.castle.games`), | ||
self._chat.Core.connect( | ||
this._chat.Core.connect( | ||
`${userId}@castle.games`, | ||
@@ -25,15 +73,59 @@ token | ||
$(self._chat).on('candy:core.message', function(evt, args) { | ||
if (self._handler) { | ||
self._handler(args); | ||
$(this._chat).on('candy:core.message', (evt, args) => { | ||
let roomName = args.roomName; | ||
if (!roomName) { | ||
return; | ||
} | ||
this._addRoom(roomName); | ||
this._messagesQueue.push(args); | ||
this._handleMessages(); | ||
}); | ||
}; | ||
self.setOnMessageHandler = (handler) => { | ||
self._handler = handler; | ||
}; | ||
$(this._chat).on('candy:core.presence.room', (evt, args) => { | ||
let roomName = args.roomName; | ||
if (!roomName) { | ||
return; | ||
} | ||
self.sendMessage = (channel, message) => { | ||
self._chat.Core.Action.Jabber.Room.Message( | ||
this._addRoom(roomName); | ||
if (this._rooms[roomName].roster.handlePresenceEvent(args) && this._presenceHandler) { | ||
this._presenceHandler({ | ||
roomName, | ||
roster: this._rooms[roomName].roster.getUsers(), | ||
}); | ||
} | ||
}); | ||
} | ||
_addRoom(roomName) { | ||
if (!this._rooms[roomName]) { | ||
this._rooms[roomName] = { | ||
roster: new Roster(), | ||
}; | ||
} | ||
} | ||
_handleMessagesUndebounced() { | ||
let tempMessageQueue = this._messagesQueue; | ||
this._messagesQueue = []; | ||
if (this._messagesHandler) { | ||
this._messagesHandler(tempMessageQueue); | ||
} | ||
} | ||
setOnMessagesHandler(handler) { | ||
this._messagesHandler = handler; | ||
} | ||
setOnPresenceHandler(handler) { | ||
this._presenceHandler = _.debounce(handler, PRESENCE_DEBOUNCE_DELAY, { | ||
maxWait: PRESENCE_DEBOUNCE_MAX_WAIT, | ||
}); | ||
} | ||
sendMessage(channel, message) { | ||
this._chat.Core.Action.Jabber.Room.Message( | ||
`${channel}@conference.castle.games`, | ||
@@ -43,5 +135,3 @@ message, | ||
); | ||
}; | ||
return self; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3778
123050
12