forked from nishant8BITS/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadSurroundingMessages.js
More file actions
80 lines (61 loc) · 1.63 KB
/
loadSurroundingMessages.js
File metadata and controls
80 lines (61 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import _ from 'underscore';
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 = RocketChat.models.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 (!RocketChat.settings.get('Message_ShowEditedStatus')) {
options.fields = {
editedAt: 0
};
}
const recordsBefore = RocketChat.models.Messages.findVisibleByRoomIdBeforeTimestamp(message.rid, message.ts, options).fetch();
const messages = recordsBefore.map((message) => {
message.starred = _.findWhere(message.starred, {
_id: fromId
});
return message;
});
const moreBefore = messages.length === options.limit;
messages.push(message);
options.sort = {
ts: 1
};
options.limit = Math.floor(limit / 2);
const recordsAfter = RocketChat.models.Messages.findVisibleByRoomIdAfterTimestamp(message.rid, message.ts, options).fetch();
const afterMessages = recordsAfter.map((message) => {
message.starred = _.findWhere(message.starred, {
_id: fromId
});
return message;
});
const moreAfter = afterMessages.length === options.limit;
return {
messages: messages.concat(afterMessages),
moreBefore,
moreAfter
};
}
});