braid/src/channels/sites/mhsChannel.ts

39 lines
1.5 KiB
TypeScript

import ClientBase from '../../clients/clientBase';
import MHSClient from '../../clients/sites/mhsClient';
import ChannelBase from '../channelBase';
import * as Joi from 'joi';
var logger = require('../../logger');
class MHSChannel extends ChannelBase {
private schema = {
currentIndex: Joi.number().integer().required(),
totalQuestions: Joi.number().integer().required(),
timeElapsed: Joi.number().required(),
status: Joi.string().required(),
};
broadcastMessage(from: ClientBase|MHSClient|null, message: string) {
const result = Joi.validate(JSON.parse(message), this.schema);
if (result.error && from != null) {
from.ws.send(`Failed to send the message, the data provided does not conform to the schema outlined by the channel type (mhs) - ${result.error}`);
logger.errorLog.info(`Failed to send the message, the data provided does not conform to the schema outlined by the channel type (mhs)"`, {data: {clientId: from.id, message: message, error: result.error}});
} else {
for (let client of this.clients) {
if (client != from && client.data.user_type == 'teacher') {
client.ws.send(message);
logger.accessLog.info(`sent to ${client.id}`, { data: { message: message }});
} else {
logger.accessLog.info(`client either not a teacher or is the sender: ${client.id}`, { data: { message: message }});
}
}
}
return {'status': 'success', 'message': `message broadcast complete`};
}
};
module.exports = MHSChannel;
export default MHSChannel;