2020-11-30 22:27:26 +08:00
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
2023-05-04 04:14:05 +08:00
|
|
|
import GroupChat from '/imports/api/group-chat';
|
2020-11-30 22:27:26 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import flat from 'flat';
|
2023-08-02 22:13:46 +08:00
|
|
|
import { parseMessage } from './addGroupChatMsg';
|
2020-11-30 22:27:26 +08:00
|
|
|
|
|
|
|
export default async function addBulkGroupChatMsgs(msgs) {
|
2020-12-09 21:33:01 +08:00
|
|
|
if (!msgs.length) return;
|
|
|
|
|
2020-11-30 22:27:26 +08:00
|
|
|
const mappedMsgs = msgs
|
2021-03-25 03:57:09 +08:00
|
|
|
.map(({ chatId, meetingId, msg }) => {
|
|
|
|
const {
|
|
|
|
sender,
|
|
|
|
...restMsg
|
|
|
|
} = msg;
|
|
|
|
|
|
|
|
return {
|
|
|
|
_id: new Mongo.ObjectID()._str,
|
|
|
|
...restMsg,
|
|
|
|
meetingId,
|
|
|
|
chatId,
|
|
|
|
message: parseMessage(msg.message),
|
|
|
|
sender: sender.id,
|
2022-01-27 04:02:38 +08:00
|
|
|
senderName: sender.name,
|
2023-04-01 04:40:41 +08:00
|
|
|
senderRole: sender.role,
|
2021-03-25 03:57:09 +08:00
|
|
|
};
|
|
|
|
})
|
2023-05-04 04:14:05 +08:00
|
|
|
.map((el) => flat(el, { safe: true }))
|
|
|
|
.map((msg)=>{
|
|
|
|
const groupChat = GroupChat.findOne({ meetingId: msg.meetingId, chatId: msg.chatId });
|
|
|
|
return {
|
|
|
|
...msg,
|
|
|
|
participants: [...groupChat.users],
|
|
|
|
};
|
|
|
|
});
|
2020-11-30 22:27:26 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { insertedCount } = await GroupChatMsg.rawCollection().insertMany(mappedMsgs);
|
|
|
|
msgs.length = 0;
|
|
|
|
|
|
|
|
if (insertedCount) {
|
|
|
|
Logger.info(`Inserted ${insertedCount} messages`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Error on bulk insert. ${err}`);
|
|
|
|
}
|
|
|
|
}
|