mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 00:46:03 +00:00
recent-topics: Add module.
Add methods to extract recent topics from received messages. Process new messages as they are received. Use new messages received from the server to extract recent_topics. Node tests added.
This commit is contained in:
48
static/js/recent_topics.js
Normal file
48
static/js/recent_topics.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const topics = new Map(); // Key is stream-id:topic.
|
||||
|
||||
exports.process_messages = function (messages) {
|
||||
for (const msg of messages) {
|
||||
exports.process_message(msg);
|
||||
}
|
||||
};
|
||||
|
||||
exports.process_message = function (msg) {
|
||||
if (msg.type !== 'stream') {
|
||||
return false;
|
||||
}
|
||||
// Initialize topic data
|
||||
const key = msg.stream_id + ':' + msg.topic;
|
||||
if (!topics.has(key)) {
|
||||
topics.set(key, {
|
||||
last_msg_id: -1,
|
||||
starred: new Set(),
|
||||
participated: false,
|
||||
muted: false,
|
||||
});
|
||||
}
|
||||
// Update topic data
|
||||
const is_ours = people.is_my_user_id(msg.sender_id);
|
||||
const topic_data = topics.get(key);
|
||||
if (topic_data.last_msg_id < msg.id) {
|
||||
topic_data.last_msg_id = msg.id;
|
||||
}
|
||||
if (msg.starred) {
|
||||
topic_data.starred.add(msg.id);
|
||||
}
|
||||
topic_data.participated = is_ours || topic_data.participated;
|
||||
topic_data.muted = topic_data.muted || muting.is_topic_muted(msg.stream_id, msg.topic);
|
||||
return true;
|
||||
};
|
||||
|
||||
function get_sorted_topics() {
|
||||
// Sort all recent topics by last message time.
|
||||
return new Map(Array.from(topics.entries()).sort(function (a, b) {
|
||||
return b[1].last_msg_id - a[1].last_msg_id;
|
||||
}));
|
||||
}
|
||||
|
||||
exports.get = function () {
|
||||
return get_sorted_topics();
|
||||
};
|
||||
|
||||
window.recent_topics = exports;
|
||||
Reference in New Issue
Block a user