2017-07-12 00:57:54 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { check } from 'meteor/check';
|
2017-07-12 04:44:07 +08:00
|
|
|
import RedisPubSub from '/imports/startup/server/redis2x';
|
2017-07-12 00:57:54 +08:00
|
|
|
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;
|
2017-07-12 04:44:07 +08:00
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
2017-07-12 00:57:54 +08:00
|
|
|
|
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
2017-07-12 20:42:16 +08:00
|
|
|
const TO_PUBLIC_CHAT = CHAT_CONFIG.public_username;
|
2017-07-12 00:57:54 +08:00
|
|
|
|
|
|
|
const { meetingId, requesterUserId, requesterToken } = credentials;
|
|
|
|
|
|
|
|
check(meetingId, String);
|
|
|
|
check(requesterUserId, String);
|
|
|
|
check(requesterToken, String);
|
|
|
|
check(message, Object);
|
|
|
|
|
|
|
|
let eventName = 'SendPrivateMessagePubMsg';
|
2017-07-12 04:44:07 +08:00
|
|
|
|
2017-07-12 00:57:54 +08:00
|
|
|
const parsedMessage = message;
|
|
|
|
|
|
|
|
parsedMessage.message = parseMessage(message.message);
|
|
|
|
|
2017-07-12 20:42:16 +08:00
|
|
|
if (message.toUsername === TO_PUBLIC_CHAT) {
|
2017-07-12 00:57:54 +08:00
|
|
|
eventName = 'SendPublicMessagePubMsg';
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:44:07 +08:00
|
|
|
const header = {
|
2017-07-12 00:57:54 +08:00
|
|
|
meetingId,
|
2017-07-12 04:44:07 +08:00
|
|
|
name: eventName,
|
|
|
|
userId: requesterUserId,
|
2017-07-12 00:57:54 +08:00
|
|
|
};
|
|
|
|
|
2017-07-12 04:44:07 +08:00
|
|
|
return RedisPubSub.publish(CHANNEL, eventName, meetingId, { message: parsedMessage }, header);
|
2017-07-12 00:57:54 +08:00
|
|
|
}
|