The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/lib/server/functions/loadMessageHistory.js

71 lines
1.6 KiB

import { settings } from '../../../settings/server';
import { Messages, Rooms } from '../../../models/server';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
import { getHiddenSystemMessages } from '../lib/getHiddenSystemMessages';
export const loadMessageHistory = function loadMessageHistory({ userId, rid, end, limit = 20, ls, showThreadMessages = true }) {
const room = Rooms.findOneById(rid, { fields: { sysMes: 1 } });
const hiddenMessageTypes = getHiddenSystemMessages(room);
const options = {
sort: {
ts: -1,
},
limit,
};
if (!settings.get('Message_ShowEditedStatus')) {
options.fields = {
editedAt: 0,
};
}
const records = end != null
? Messages.findVisibleByRoomIdBeforeTimestampNotContainingTypes(
rid,
end,
hiddenMessageTypes,
options,
showThreadMessages,
).fetch()
: Messages.findVisibleByRoomIdNotContainingTypes(
rid,
hiddenMessageTypes,
options,
showThreadMessages,
).fetch();
const messages = normalizeMessagesForUser(records, userId);
let unreadNotLoaded = 0;
let firstUnread;
if (ls != null) {
const firstMessage = messages[messages.length - 1];
if ((firstMessage != null ? firstMessage.ts : undefined) > ls) {
delete options.limit;
const unreadMessages = Messages.findVisibleByRoomIdBetweenTimestampsNotContainingTypes(
rid,
ls,
firstMessage.ts,
hiddenMessageTypes,
{
limit: 1,
sort: {
ts: 1,
},
},
showThreadMessages,
);
firstUnread = unreadMessages.fetch()[0];
unreadNotLoaded = unreadMessages.count();
}
}
return {
messages,
firstUnread,
unreadNotLoaded,
};
};