60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
|
import { Meteor } from 'meteor/meteor';
|
||
|
import { check } from 'meteor/check';
|
||
|
import RedisPubSub from '/imports/startup/server/redis';
|
||
|
import RegexWebUrl from '/imports/utils/regex-weburl';
|
||
|
|
||
|
const HTML_SAFE_MAP = {
|
||
|
'<': '<',
|
||
|
'>': '>',
|
||
|
'"': '"',
|
||
|
"'": ''',
|
||
|
};
|
||
|
|
||
|
const parseMessage = (message) => {
|
||
|
let parsedMessage = message || '';
|
||
|
parsedMessage = parsedMessage.trim();
|
||
|
|
||
|
// Replace <br/> with \n\r
|
||
|
parsedMessage = parsedMessage.replace(/<br\s*[\\/]?>/gi, '\n\r');
|
||
|
|
||
|
// Sanitize. See: http://shebang.brandonmintern.com/foolproof-html-escaping-in-javascript/
|
||
|
parsedMessage = parsedMessage.replace(/[<>'"]/g, c => HTML_SAFE_MAP[c]);
|
||
|
|
||
|
// Replace flash links to flash valid ones
|
||
|
parsedMessage = parsedMessage.replace(RegexWebUrl, "<a href='event:$&'><u>$&</u></a>");
|
||
|
|
||
|
return parsedMessage;
|
||
|
};
|
||
|
|
||
|
export default function sendChat(credentials, message) {
|
||
|
const REDIS_CONFIG = Meteor.settings.redis;
|
||
|
const CHANNEL = REDIS_CONFIG.channels.toBBBApps.chat;
|
||
|
|
||
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
||
|
const PUBLIC_CHAT_TYPE = CHAT_CONFIG.type_public;
|
||
|
|
||
|
const { meetingId, requesterUserId, requesterToken } = credentials;
|
||
|
|
||
|
check(meetingId, String);
|
||
|
check(requesterUserId, String);
|
||
|
check(requesterToken, String);
|
||
|
check(message, Object);
|
||
|
|
||
|
let eventName = 'SendPrivateMessagePubMsg';
|
||
|
const parsedMessage = message;
|
||
|
|
||
|
parsedMessage.message = parseMessage(message.message);
|
||
|
|
||
|
if (message.chat_type === PUBLIC_CHAT_TYPE) {
|
||
|
eventName = 'SendPublicMessagePubMsg';
|
||
|
}
|
||
|
|
||
|
const payload = {
|
||
|
parsedMessage,
|
||
|
meetingId,
|
||
|
requesterId: message.fromUserid,
|
||
|
};
|
||
|
|
||
|
return RedisPubSub.publish(CHANNEL, eventName, payload);
|
||
|
}
|