forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpm_conversations.js
68 lines (53 loc) · 1.85 KB
/
pm_conversations.js
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
var pm_conversations = (function () {
var exports = {};
var partners = new Dict();
exports.set_partner = function (user_id) {
partners.set(user_id, true);
};
exports.is_partner = function (user_id) {
return partners.get(user_id) || false;
};
exports.recent = (function () {
var self = {};
var recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string
var recent_private_messages = [];
self.insert = function (user_ids_string, timestamp) {
var conversation = recent_timestamps.get(user_ids_string);
if (conversation === undefined) {
// This is a new user, so create a new object.
conversation = {
user_ids_string: user_ids_string,
timestamp: timestamp,
};
recent_timestamps.set(user_ids_string, conversation);
// Optimistically insert the new message at the front, since that
// is usually where it belongs, but we'll re-sort.
recent_private_messages.unshift(conversation);
} else {
if (conversation.timestamp >= timestamp) {
return; // don't backdate our conversation
}
// update our timestamp
conversation.timestamp = timestamp;
}
recent_private_messages.sort(function (a, b) {
return b.timestamp - a.timestamp;
});
};
self.get = function () {
// returns array of structs with user_ids_string and
// timestamp
return recent_private_messages;
};
self.get_strings = function () {
// returns array of structs with user_ids_string and
// timestamp
return _.pluck(recent_private_messages, 'user_ids_string');
};
return self;
}());
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = pm_conversations;
}