2018-03-10 02:42:14 +08:00
|
|
|
import { Match, check } from 'meteor/check';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
2019-08-03 02:18:33 +08:00
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
2018-03-10 02:42:14 +08:00
|
|
|
import { BREAK_LINE } from '/imports/utils/lineEndings';
|
2022-01-28 02:45:04 +08:00
|
|
|
import changeHasMessages from '/imports/api/users-persistent-data/server/modifiers/changeHasMessages';
|
2018-03-10 02:42:14 +08:00
|
|
|
|
2020-11-30 22:27:26 +08:00
|
|
|
export function parseMessage(message) {
|
2018-03-10 02:42:14 +08:00
|
|
|
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;
|
2020-11-30 22:27:26 +08:00
|
|
|
}
|
2018-03-10 02:42:14 +08:00
|
|
|
|
|
|
|
export default function addGroupChatMsg(meetingId, chatId, msg) {
|
|
|
|
check(meetingId, String);
|
|
|
|
check(chatId, String);
|
|
|
|
check(msg, {
|
2018-08-03 22:03:16 +08:00
|
|
|
id: Match.Maybe(String),
|
2018-03-10 02:42:14 +08:00
|
|
|
timestamp: Number,
|
|
|
|
sender: Object,
|
2022-02-22 23:00:36 +08:00
|
|
|
chatEmphasizedText: Boolean,
|
2018-03-10 02:42:14 +08:00
|
|
|
message: String,
|
|
|
|
correlationId: Match.Maybe(String),
|
|
|
|
});
|
2021-03-25 03:57:09 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
sender,
|
|
|
|
...restMsg
|
|
|
|
} = msg;
|
|
|
|
|
2018-03-10 02:42:14 +08:00
|
|
|
const msgDocument = {
|
2021-03-25 03:57:09 +08:00
|
|
|
...restMsg,
|
|
|
|
sender: sender.id,
|
2022-01-27 04:02:38 +08:00
|
|
|
senderName: sender.name,
|
2022-02-14 21:10:32 +08:00
|
|
|
senderRole: sender.role,
|
2018-03-10 02:42:14 +08:00
|
|
|
meetingId,
|
|
|
|
chatId,
|
|
|
|
message: parseMessage(msg.message),
|
|
|
|
};
|
|
|
|
|
2020-11-21 04:32:21 +08:00
|
|
|
try {
|
2020-12-18 06:02:38 +08:00
|
|
|
const insertedId = GroupChatMsg.insert(msgDocument);
|
2018-03-10 02:42:14 +08:00
|
|
|
|
|
|
|
if (insertedId) {
|
2022-11-22 00:07:30 +08:00
|
|
|
changeHasMessages(true, sender.id, meetingId, chatId);
|
2020-11-21 04:32:21 +08:00
|
|
|
Logger.info(`Added group-chat-msg msgId=${msg.id} chatId=${chatId} meetingId=${meetingId}`);
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|
2020-11-21 04:32:21 +08:00
|
|
|
} catch (err) {
|
2020-11-30 22:27:26 +08:00
|
|
|
Logger.error(`Error on adding group-chat-msg to collection: ${err}`);
|
2020-11-21 04:32:21 +08:00
|
|
|
}
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|