castle-chat-lib
Advanced tools
Comparing version 1.6.0 to 2.0.0
{ | ||
"name": "castle-chat-lib", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
@@ -14,9 +14,4 @@ "main": "src/index.js", | ||
"castle-api-client": "^6.0.0", | ||
"jquery": "^3.3.1", | ||
"lodash": "^4.17.11", | ||
"strophe.js": "^1.3.1", | ||
"strophejs-plugin-caps": "^1.1.0", | ||
"strophejs-plugin-disco": "^0.0.2", | ||
"strophejs-plugin-muc": "^1.1.0", | ||
"strophejs-plugin-roster": "^1.1.0", | ||
"socket.io-client": "^1.7.3", | ||
"uuid": "^3.3.2" | ||
@@ -23,0 +18,0 @@ }, |
199
src/index.js
@@ -1,52 +0,6 @@ | ||
import Candy from './candy/candy'; | ||
import $ from 'jquery'; | ||
const uuidv4 = require('uuid/v4'); | ||
import _ from 'lodash'; | ||
import CastleApiClient from 'castle-api-client'; | ||
import io from 'socket.io-client'; | ||
export const API = CastleApiClient('https://api.castle.games'); | ||
const PRESENCE_DEBOUNCE_DELAY = 100; | ||
const PRESENCE_DEBOUNCE_MAX_WAIT = 500; | ||
const MESSAGE_DEBOUNCE_DELAY = 50; | ||
const MESSAGE_DEBOUNCE_MAX_WAIT = 200; | ||
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 const ConnectionStatus = Object.freeze({ | ||
@@ -60,15 +14,16 @@ DISCONNECTED: 0, | ||
export class CastleChat { | ||
init(url, userId, token, channels) { | ||
this._url = url; | ||
this._userId = userId; | ||
init(chatUrl, apiUrl, token, channels) { | ||
this._chatUrl = chatUrl; | ||
this._api = CastleApiClient(apiUrl); | ||
this._token = token; | ||
this._channels = channels; | ||
this._connectionStatus = ConnectionStatus.DISCONNECTED; | ||
if (window) { | ||
window.addEventListener('online', () => { | ||
this._handleConnectionStatusChange(Strophe.Status.CONNECTING); | ||
this._handleConnectionStatusChange(ConnectionStatus.CONNECTING); | ||
this.connect(); | ||
}); | ||
window.addEventListener('offline', () => { | ||
this._handleConnectionStatusChange(Strophe.Status.DISCONNECTED); | ||
this._handleConnectionStatusChange(ConnectionStatus.DISCONNECTED); | ||
}); | ||
@@ -79,98 +34,41 @@ } | ||
async _debugSetTokenInStorageAsync() { | ||
await API.client.setTokenAsync(this._token); | ||
await this._api.client.setTokenAsync(this._token); | ||
} | ||
connect() { | ||
if (this._chat) { | ||
$(this._chat).off(); | ||
this._chat.Core.disconnect(); | ||
} | ||
this._chat = new Candy(); | ||
this._rooms = {}; | ||
this._messagesQueue = []; | ||
this._connectionStatus = ConnectionStatus.DISCONNECTED; | ||
this._handleMessages = _.debounce(this._handleMessagesUndebounced, MESSAGE_DEBOUNCE_DELAY, { | ||
maxWait: MESSAGE_DEBOUNCE_MAX_WAIT, | ||
}); | ||
this._chat.init(this._url, { | ||
core: { | ||
autojoin: _.map(this._channels, (channel) => `${channel}@conference.castle.games`), | ||
resource: 'desktop-' + uuidv4(), | ||
this._socket = io(this._chatUrl, { | ||
query: { | ||
token: this._token, | ||
channels: JSON.stringify(this._channels), | ||
}, | ||
}); | ||
$(this._chat).on('candy:core.message', (evt, args) => { | ||
let roomName = args.roomName; | ||
if (!roomName) { | ||
return; | ||
} | ||
this._addRoom(roomName); | ||
this._messagesQueue.push(args); | ||
this._handleMessages(); | ||
this._socket.on('connect', () => { | ||
this._handleConnectionStatusChange( | ||
this._socket.connected ? ConnectionStatus.CONNECTED : ConnectionStatus.DISCONNECTED | ||
); | ||
}); | ||
$(this._chat).on('candy:core.presence.room', (evt, args) => { | ||
let roomName = args.roomName; | ||
if (!roomName) { | ||
return; | ||
} | ||
this._socket.on('message', (msg) => { | ||
// convert to old format | ||
this._addRoom(roomName); | ||
try { | ||
let parsedMessage = JSON.parse(msg); | ||
let newMessage = { | ||
...parsedMessage, | ||
roomName: parsedMessage.channel_id, | ||
message: { | ||
name: parsedMessage.from_user_id, | ||
body: JSON.stringify(parsedMessage.body), | ||
}, | ||
timestamp: new Date().toString(), | ||
}; | ||
if (this._rooms[roomName].roster.handlePresenceEvent(args) && this._presenceHandler) { | ||
this._presenceHandler({ | ||
roomName, | ||
roster: this._rooms[roomName].roster.getUsers(), | ||
}); | ||
} | ||
if (this._messagesHandler) { | ||
this._messagesHandler([newMessage]); | ||
} | ||
} catch (e) {} | ||
}); | ||
$(this._chat).on('candy:core.chat.connection', (evt, args) => { | ||
this._handleConnectionStatusChange(args.status); | ||
}); | ||
this._chat.Core.connect(`${this._userId}@castle.games`, this._token); | ||
} | ||
_addRoom(roomName) { | ||
if (!this._rooms[roomName]) { | ||
this._rooms[roomName] = { | ||
roster: new Roster(), | ||
}; | ||
} | ||
} | ||
_handleConnectionStatusChange(status) { | ||
let lastConnectionStatus = this._connectionStatus; | ||
if (status == Strophe.Status.CONNECTED) { | ||
this._connectionStatus = ConnectionStatus.CONNECTED; | ||
} | ||
if (status == Strophe.Status.CONNECTING) { | ||
this._connectionStatus = ConnectionStatus.CONNECTING; | ||
} | ||
if (status == Strophe.Status.DISCONNECTED || status == Strophe.Status.CONNTIMEOUT) { | ||
this._connectionStatus = ConnectionStatus.DISCONNECTED; | ||
} | ||
if (lastConnectionStatus != this._connectionStatus && this._connectionStatusHandler) { | ||
this._connectionStatusHandler(this._connectionStatus); | ||
} | ||
} | ||
_handleMessagesUndebounced() { | ||
let tempMessageQueue = this._messagesQueue; | ||
this._messagesQueue = []; | ||
if (this._messagesHandler) { | ||
this._messagesHandler(tempMessageQueue); | ||
} | ||
} | ||
setOnMessagesHandler(handler) { | ||
@@ -181,5 +79,3 @@ this._messagesHandler = handler; | ||
setOnPresenceHandler(handler) { | ||
this._presenceHandler = _.debounce(handler, PRESENCE_DEBOUNCE_DELAY, { | ||
maxWait: PRESENCE_DEBOUNCE_MAX_WAIT, | ||
}); | ||
this._presenceHandler = handler; | ||
} | ||
@@ -193,6 +89,6 @@ | ||
try { | ||
let result = await API.graphqlAsync( | ||
let result = await this._api.graphqlAsync( | ||
` | ||
mutation($roomName: String!, $message: String! $postId: String) { | ||
sendChatMessage(roomName: $roomName, message: $message, postId: $postId) | ||
mutation($roomName: String!, $message: String!, $useNewChat: Boolean) { | ||
sendChatMessage(roomName: $roomName, message: $message, useNewChat: $useNewChat) | ||
} | ||
@@ -203,3 +99,3 @@ `, | ||
message, | ||
postId, | ||
useNewChat: true, | ||
} | ||
@@ -217,16 +113,11 @@ ); | ||
async internalSendMessageDirectAsync(channel, message) { | ||
try { | ||
this._chat.Core.Action.Jabber.Room.Message( | ||
`${channel}@conference.castle.games`, | ||
message, | ||
'groupchat', | ||
null, | ||
uuidv4() | ||
); | ||
} catch (e) { | ||
console.log('send message error'); | ||
console.log(JSON.stringify(e)); | ||
_handleConnectionStatusChange(status) { | ||
let lastConnectionStatus = this._connectionStatus; | ||
this._connectionStatus = status; | ||
if (lastConnectionStatus != this._connectionStatus && this._connectionStatusHandler) { | ||
this._connectionStatusHandler(this._connectionStatus); | ||
} | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
4
3444
2
100
1
+ Addedsocket.io-client@^1.7.3
+ Addedafter@0.8.2(transitive)
+ Addedarraybuffer.slice@0.0.6(transitive)
+ Addedbacko2@1.0.2(transitive)
+ Addedbase64-arraybuffer@0.1.5(transitive)
+ Addedbetter-assert@1.0.2(transitive)
+ Addedblob@0.0.4(transitive)
+ Addedcallsite@1.0.0(transitive)
+ Addedcomponent-bind@1.0.0(transitive)
+ Addedcomponent-emitter@1.1.21.2.1(transitive)
+ Addedcomponent-inherit@0.0.3(transitive)
+ Addeddebug@2.2.02.3.3(transitive)
+ Addedengine.io-client@1.8.6(transitive)
+ Addedengine.io-parser@1.3.2(transitive)
+ Addedhas-binary@0.1.7(transitive)
+ Addedhas-cors@1.1.0(transitive)
+ Addedindexof@0.0.1(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedjson3@3.3.2(transitive)
+ Addedms@0.7.10.7.2(transitive)
+ Addedobject-component@0.0.3(transitive)
+ Addedoptions@0.0.6(transitive)
+ Addedparsejson@0.0.3(transitive)
+ Addedparseqs@0.0.5(transitive)
+ Addedparseuri@0.0.5(transitive)
+ Addedsocket.io-client@1.7.4(transitive)
+ Addedsocket.io-parser@2.3.1(transitive)
+ Addedto-array@0.1.4(transitive)
+ Addedultron@1.0.2(transitive)
+ Addedws@1.1.5(transitive)
+ Addedwtf-8@1.0.0(transitive)
+ Addedxmlhttprequest-ssl@1.6.3(transitive)
+ Addedyeast@0.1.2(transitive)
- Removedjquery@^3.3.1
- Removedstrophe.js@^1.3.1
- Removedstrophejs-plugin-caps@^1.1.0
- Removedstrophejs-plugin-disco@^0.0.2
- Removedstrophejs-plugin-muc@^1.1.0
- Removedstrophejs-plugin-roster@^1.1.0
- Removed@xmldom/xmldom@0.8.7(transitive)
- Removedabab@2.0.6(transitive)
- Removedjquery@3.7.1(transitive)
- Removedstrophe.js@1.6.2(transitive)
- Removedstrophejs-plugin-caps@1.1.3(transitive)
- Removedstrophejs-plugin-disco@0.0.2(transitive)
- Removedstrophejs-plugin-muc@1.1.0(transitive)
- Removedstrophejs-plugin-roster@1.1.0(transitive)
- Removedws@8.18.0(transitive)