lintageddon 4
This commit is contained in:
parent
6ec98af769
commit
e004a64e9b
@ -12,12 +12,12 @@ class ChannelManager {
|
||||
|
||||
constructor() {
|
||||
// create default channel...
|
||||
let channel = new PublicChannel('default');
|
||||
const channel = new PublicChannel('default');
|
||||
this.channels.push(channel);
|
||||
}
|
||||
|
||||
createChannel(data: any) {
|
||||
let channelExists: PublicChannel|PrivateChannel|CustomChannel|null = this.channelExists(data.channel);
|
||||
const channelExists: PublicChannel|PrivateChannel|CustomChannel|null = this.channelExists(data.channel);
|
||||
let channel: PublicChannel|PrivateChannel|CustomChannel;
|
||||
|
||||
if (channelExists) {
|
||||
@ -33,7 +33,7 @@ class ChannelManager {
|
||||
}
|
||||
|
||||
channelExists(channel_id: string) {
|
||||
for (let channel of this.channels) {
|
||||
for (const channel of this.channels) {
|
||||
if (channel.id === channel_id) {
|
||||
return channel;
|
||||
}
|
||||
@ -61,7 +61,7 @@ class ChannelManager {
|
||||
|
||||
if (data.channel_type === 'public') {
|
||||
return new PublicChannel(data.channel);
|
||||
} else if (data.channel_type == 'private') {
|
||||
} else if (data.channel_type === 'private') {
|
||||
return new PrivateChannel(data.channel);
|
||||
} else {
|
||||
return new CustomChannel(data.channel, data.custom);
|
||||
@ -76,7 +76,7 @@ class ChannelManager {
|
||||
|
||||
removeClientFromChannel(client_id: number, channel_id: string) {
|
||||
for (const channel of this.channels) {
|
||||
if (channel.id == channel_id) {
|
||||
if (channel.id === channel_id) {
|
||||
if (channel.removeClient(client_id)) {
|
||||
logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`);
|
||||
return true;
|
||||
|
@ -17,7 +17,7 @@ class ChannelBase {
|
||||
for (const to of this.clients) {
|
||||
if (this.messageTransactionPossible(from, to)) {
|
||||
to.ws.send(JSON.stringify(message));
|
||||
logger.accessLog.info(`sent to ${to.id}`, { data: { message: message }});
|
||||
logger.accessLog.info(`sent to ${to.id}`, { data: {message}});
|
||||
} else {
|
||||
logger.accessLog.info(`client is unable to send: ${to.id}`, { data: {message}});
|
||||
}
|
||||
@ -52,7 +52,7 @@ class ChannelBase {
|
||||
}
|
||||
|
||||
removeClient(id: number) {
|
||||
for (let client of this.clients) {
|
||||
for (const client of this.clients) {
|
||||
if (client.id === id) {
|
||||
const index = this.clients.indexOf(client)
|
||||
this.clients.splice(index, 1);
|
||||
|
@ -11,6 +11,6 @@ class PrivateChannel extends ChannelBase {
|
||||
from.roles.includes('broadcaster')
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default PrivateChannel;
|
||||
|
@ -9,6 +9,6 @@ class PublicChannel extends ChannelBase {
|
||||
to !== from
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default PublicChannel;
|
||||
|
@ -29,7 +29,7 @@ class ClientManager {
|
||||
clientsOfType(client_type: string) {
|
||||
let result: PublicClient[]|PrivateClient[]|CustomClient[] = [];
|
||||
|
||||
for (let client of this.clients) {
|
||||
for (const client of this.clients) {
|
||||
if (client.type() === client_type) {
|
||||
result.push(client);
|
||||
}
|
||||
@ -55,8 +55,8 @@ class ClientManager {
|
||||
removeClient(id: number) {
|
||||
let index: number = 0;
|
||||
|
||||
for (let client of this.clients) {
|
||||
if (client.id == id) {
|
||||
for (const client of this.clients) {
|
||||
if (client.id === id) {
|
||||
client.ws.close();
|
||||
this.clients.splice(index, 1);
|
||||
logger.accessLog.info(`client disconnected: ${client.id}`);
|
||||
@ -72,11 +72,11 @@ class ClientManager {
|
||||
logger.accessLog.info(`attempting to create client of type ${data.client_type}, client id: ${data.user_id}...`);
|
||||
|
||||
if (data.client_type === 'public') {
|
||||
return new PublicClient(data, ws, channelManager, this)
|
||||
return new PublicClient(data, ws, channelManager, this);
|
||||
} else if (data.channel_type === 'private') {
|
||||
return new PrivateClient(data, ws, channelManager, this)
|
||||
return new PrivateClient(data, ws, channelManager, this);
|
||||
} else {
|
||||
return new CustomClient(data, ws, channelManager, this)
|
||||
return new CustomClient(data, ws, channelManager, this);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.errorLog.info(e);
|
||||
|
@ -15,7 +15,7 @@ class ClientBase {
|
||||
channel: PublicChannel|PrivateChannel|CustomChannel|null;
|
||||
clientManager: ClientManager;
|
||||
channelManager: ChannelManager;
|
||||
roles: Array<string>;
|
||||
roles: String[];
|
||||
messageListener: (data: any) => void;
|
||||
|
||||
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
||||
@ -25,12 +25,12 @@ class ClientBase {
|
||||
this.channel = null;
|
||||
this.clientManager = clientManager;
|
||||
this.channelManager = channelManager;
|
||||
this.roles = ['receiver']
|
||||
this.messageListener = (data: any) => {
|
||||
this.roles = ['receiver'];
|
||||
this.messageListener = (message: any) => {
|
||||
if (this.channel) {
|
||||
logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {data});
|
||||
data = messageManager.prepareMessage(data, this.channel, this);
|
||||
this.messageTransaction(data);
|
||||
logger.accessLog.info(`starting message transaction on channel ${this.channel.id}: `, {message});
|
||||
message = messageManager.prepareMessage(message, this.channel, this);
|
||||
this.messageTransaction(message);
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -64,25 +64,25 @@ class ClientBase {
|
||||
});
|
||||
}
|
||||
|
||||
messageTransaction(data: any) {
|
||||
if (this.channel && !data.error) {
|
||||
switch (data.message_type) {
|
||||
messageTransaction(message: any) {
|
||||
if (this.channel && !message.error) {
|
||||
switch (message.message_type) {
|
||||
case 'broadcast':
|
||||
this.channel.broadcastMessage(this, data);
|
||||
this.channel.broadcastMessage(this, message);
|
||||
break;
|
||||
case 'direct':
|
||||
let to = this.clientManager.getClient(data.message.to)
|
||||
to.directMessage(data);
|
||||
const to = this.clientManager.getClient(message.message.to);
|
||||
to.directMessage(message);
|
||||
break;
|
||||
case 'changeChannel':
|
||||
this.ws.removeListener('message', this.messageListener);
|
||||
this.channelManager.changeChannel(this, data);
|
||||
this.channelManager.changeChannel(this, message);
|
||||
break;
|
||||
}
|
||||
|
||||
logger.accessLog.info(`message transaction complete on channel ${this.channel.id}: `, {message: data});
|
||||
logger.accessLog.info(`message transaction complete on channel ${this.channel.id}: `, {message});
|
||||
} else {
|
||||
logger.errorLog.info(`Validation failed or client is not part of a channel, please review schema`, {data: {message: data, error: data.error}});
|
||||
logger.errorLog.info(`Validation failed or client is not part of a channel, please review schema`, {data: {message, error: message.error}});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ import * as jwt from 'jsonwebtoken';
|
||||
const app = require('../config/app');
|
||||
|
||||
module.exports = {
|
||||
confirmToken : (req: any, res: any) => {
|
||||
const token = req.body.token
|
||||
confirmToken: (req: any, res: any) => {
|
||||
const token = req.body.token;
|
||||
res.json({
|
||||
response: JSON.stringify(jwt.verify(token, app.secret, app.signOptions))
|
||||
});
|
||||
|
@ -28,6 +28,6 @@ const accessLog = createLogger({
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
errorLog: errorLog,
|
||||
accessLog: accessLog
|
||||
errorLog,
|
||||
accessLog
|
||||
};
|
||||
|
@ -3,8 +3,8 @@ import Validations from './services/validations';
|
||||
|
||||
module.exports = {
|
||||
prepareMessage: (message: string) => {
|
||||
let validations = new Validations(message)
|
||||
let parsed = JSON.parse(message);
|
||||
const validations = new Validations(message);
|
||||
const parsed = JSON.parse(message);
|
||||
const result = Joi.validate(parsed, validations.MessageConditions);
|
||||
|
||||
if (result.error) {
|
||||
|
@ -10,7 +10,7 @@ const corsOptions = {
|
||||
callback(new Error(`Not allowed by CORS. Origin: ${origin}`));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
|
@ -58,8 +58,8 @@ function connectionManager() {
|
||||
|
||||
function validateJWT(request: any) {
|
||||
try {
|
||||
let query = url.parse(request.url, true).query;
|
||||
let token = query.token || (app.environment === 'development' ? app.devToken : '');
|
||||
const query = url.parse(request.url, true).query;
|
||||
const token = query.token || (app.environment === 'development' ? app.devToken : '');
|
||||
return JSON.stringify(jwt.verify(token, app.secret, app.signOptions));
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
Loading…
x
Reference in New Issue
Block a user