import { check } from 'meteor/check'; import Logger from '/imports/startup/server/logger'; import Meetings from '/imports/api/meetings'; import Users from '/imports/api/users'; import addChat from '/imports/api/chat/server/modifiers/addChat'; export default function handleValidateAuthToken({ payload }) { const meetingId = payload.meeting_id; const userId = payload.userid; const validStatus = payload.valid; check(meetingId, String); check(userId, String); check(validStatus, String); const selector = { meetingId, userId, }; const User = Users.findOne(selector); if (!User) { throw new Meteor.Error( 'user-not-found', `You need a valid user to be able validate the token`); } if (User.validated === validStatus) { return; } const modifier = { $set: { validated: validStatus, }, }; const cb = (err, numChanged) => { if (err) { return Logger.error(`Validating auth token: ${err}`); } if (numChanged) { if (validStatus) { addWelcomeChatMessage(meetingId, userId); } return Logger.info(`Validated auth token as '${validStatus}' user=${userId} meeting=${meetingId}`); } }; return Users.update(selector, modifier, cb); }; const addWelcomeChatMessage = (meetingId, userId) => { const APP_CONFIG = Meteor.settings.public.app; const CHAT_CONFIG = Meteor.settings.public.chat; const Meeting = Meetings.findOne({ meetingId }); let welcomeMessage = APP_CONFIG.defaultWelcomeMessage .concat(APP_CONFIG.defaultWelcomeMessageFooter) .replace(/%%CONFNAME%%/, Meeting.meetingName); const message = { chat_type: CHAT_CONFIG.type_system, message: welcomeMessage, from_color: '0x3399FF', to_userid: userId, from_userid: CHAT_CONFIG.type_system, from_username: '', from_time: (new Date()).getTime(), }; return addChat(meetingId, message); };