2021-03-10 04:52:20 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import GroupChat from '/imports/api/group-chat';
|
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
2021-05-05 20:13:48 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2021-03-10 04:52:20 +08:00
|
|
|
|
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const PUBLIC_CHAT_TYPE = CHAT_CONFIG.type_public;
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
export default async function chatMessageBeforeJoinCounter() {
|
2021-05-05 20:13:48 +08:00
|
|
|
try {
|
|
|
|
const { meetingId, requesterUserId } = extractCredentials(this.userId);
|
2021-03-17 00:15:41 +08:00
|
|
|
|
2021-05-05 20:13:48 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(requesterUserId, String);
|
2021-03-17 00:15:41 +08:00
|
|
|
|
2021-05-05 20:13:48 +08:00
|
|
|
const groupChats = GroupChat.find({
|
|
|
|
$or: [
|
|
|
|
{ meetingId, access: PUBLIC_CHAT_TYPE },
|
|
|
|
{ meetingId, users: { $all: [requesterUserId] } },
|
|
|
|
],
|
|
|
|
}).fetch();
|
2021-03-10 04:52:20 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const User = await Users.findOneAsync({ userId: requesterUserId, meetingId });
|
2021-03-10 04:52:20 +08:00
|
|
|
|
2021-05-05 20:13:48 +08:00
|
|
|
const chatIdWithCounter = groupChats.map((groupChat) => {
|
|
|
|
const msgCount = GroupChatMsg.find({
|
|
|
|
meetingId,
|
|
|
|
chatId: groupChat.chatId,
|
|
|
|
timestamp: { $lt: User.authTokenValidatedTime },
|
|
|
|
}).count();
|
|
|
|
return {
|
|
|
|
chatId: groupChat.chatId,
|
|
|
|
count: msgCount,
|
|
|
|
};
|
|
|
|
}).filter((chat) => chat.count);
|
|
|
|
return chatIdWithCounter;
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Exception while invoking method chatMessageBeforeJoinCounter ${err.stack}`);
|
|
|
|
}
|
2023-04-01 04:40:41 +08:00
|
|
|
//True returned because the function requires a return.
|
|
|
|
return true;
|
2021-03-10 04:52:20 +08:00
|
|
|
}
|