Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

castle-chat-lib

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

castle-chat-lib - npm Package Compare versions

Comparing version 1.6.0 to 2.0.0

9

package.json
{
"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 @@ },

@@ -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);
}
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc