2021-03-10 04:52:20 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
2021-03-17 00:15:41 +08:00
|
|
|
import { check } from 'meteor/check';
|
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 ITENS_PER_PAGE = CHAT_CONFIG.itemsPerPage;
|
|
|
|
|
2023-03-14 02:28:06 +08:00
|
|
|
export default async function fetchMessagePerPage(chatId, page) {
|
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);
|
|
|
|
check(chatId, String);
|
|
|
|
check(page, Number);
|
2021-03-17 00:15:41 +08:00
|
|
|
|
2023-03-14 02:28:06 +08:00
|
|
|
const User = await Users.findOneAsync({ userId: requesterUserId, meetingId });
|
2021-03-10 04:52:20 +08:00
|
|
|
|
2023-03-14 02:28:06 +08:00
|
|
|
const messages = await GroupChatMsg.find(
|
2021-05-05 20:13:48 +08:00
|
|
|
{ chatId, meetingId, timestamp: { $lt: User.authTokenValidatedTime } },
|
|
|
|
{
|
|
|
|
sort: { timestamp: 1 },
|
|
|
|
skip: page > 0 ? ((page - 1) * ITENS_PER_PAGE) : 0,
|
|
|
|
limit: ITENS_PER_PAGE,
|
|
|
|
},
|
|
|
|
)
|
2023-03-14 02:28:06 +08:00
|
|
|
.fetchAsync();
|
2021-05-05 20:13:48 +08:00
|
|
|
return messages;
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Exception while invoking method fetchMessagePerPage ${err.stack}`);
|
|
|
|
}
|
2023-03-14 02:28:06 +08:00
|
|
|
//True returned because the function requires a return.
|
|
|
|
return true;
|
2021-03-10 04:52:20 +08:00
|
|
|
}
|