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/server/methods/loadSurroundingMessages.js

72 lines
1.6 KiB

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Messages } from 'meteor/rocketchat:models';
import { settings } from 'meteor/rocketchat:settings';
import { composeMessageObjectWithUser } from 'meteor/rocketchat:utils';
Meteor.methods({
loadSurroundingMessages(message, limit = 50) {
check(message, Object);
check(limit, Number);
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'loadSurroundingMessages',
});
}
const fromId = Meteor.userId();
if (!message._id) {
return false;
}
message = Messages.findOneById(message._id);
if (!message || !message.rid) {
return false;
}
if (!Meteor.call('canAccessRoom', message.rid, fromId)) {
return false;
}
limit = limit - 1;
const options = {
sort: {
ts: -1,
},
limit: Math.ceil(limit / 2),
};
if (!settings.get('Message_ShowEditedStatus')) {
options.fields = {
editedAt: 0,
};
}
const messages = Messages.findVisibleByRoomIdBeforeTimestamp(message.rid, message.ts, options).fetch();
const moreBefore = messages.length === options.limit;
messages.push(message);
options.sort = {
ts: 1,
};
options.limit = Math.floor(limit / 2);
const afterMessages = Messages.findVisibleByRoomIdAfterTimestamp(message.rid, message.ts, options).fetch();
const moreAfter = afterMessages.length === options.limit;
return {
messages: messages.concat(afterMessages)
.map((message) => composeMessageObjectWithUser(message, fromId)),
moreBefore,
moreAfter,
};
},
});