2021-02-19 21:57:11 +08:00
|
|
|
import { Match, check } from 'meteor/check';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
|
|
|
import { BREAK_LINE } from '/imports/utils/lineEndings';
|
|
|
|
|
|
|
|
export function parseMessage(message) {
|
|
|
|
let parsedMessage = message || '';
|
|
|
|
|
|
|
|
// Replace \r and \n to <br/>
|
|
|
|
parsedMessage = parsedMessage.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, `$1${BREAK_LINE}$2`);
|
|
|
|
|
|
|
|
// Replace flash links to html valid ones
|
|
|
|
parsedMessage = parsedMessage.split('<a href=\'event:').join('<a target="_blank" href=\'');
|
|
|
|
parsedMessage = parsedMessage.split('<a href="event:').join('<a target="_blank" href="');
|
|
|
|
|
|
|
|
return parsedMessage;
|
|
|
|
}
|
|
|
|
|
2023-03-14 02:28:06 +08:00
|
|
|
export default async function addSystemMsg(meetingId, chatId, msg) {
|
2021-02-19 21:57:11 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(chatId, String);
|
|
|
|
check(msg, {
|
|
|
|
id: Match.Maybe(String),
|
|
|
|
timestamp: Number,
|
|
|
|
sender: Object,
|
|
|
|
message: String,
|
2022-02-21 19:14:34 +08:00
|
|
|
messageValues: Match.Maybe(Object),
|
|
|
|
extra: Match.Maybe(Object),
|
2021-02-19 21:57:11 +08:00
|
|
|
correlationId: Match.Maybe(String),
|
|
|
|
});
|
|
|
|
const msgDocument = {
|
|
|
|
...msg,
|
2021-04-26 22:56:45 +08:00
|
|
|
sender: msg.sender.id,
|
2021-02-19 21:57:11 +08:00
|
|
|
meetingId,
|
|
|
|
chatId,
|
|
|
|
message: parseMessage(msg.message),
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2023-03-14 02:28:06 +08:00
|
|
|
const insertedId = await GroupChatMsg.insertAsync(msgDocument);
|
2021-02-19 21:57:11 +08:00
|
|
|
|
|
|
|
if (insertedId) {
|
|
|
|
Logger.info(`Added system-msg msgId=${msg.id} chatId=${chatId} meetingId=${meetingId}`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Error on adding system-msg to collection: ${err}`);
|
|
|
|
}
|
|
|
|
}
|