2020-03-21 14:42:10 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-05-15 14:27:36 +02:00
|
|
|
exports.update_topic_is_muted = function (stream_id, topic, is_muted) {
|
|
|
|
const key = stream_id + ":" + topic;
|
|
|
|
if (!topics.has(key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const topic_data = topics.get(stream_id + ":" + topic);
|
|
|
|
topic_data.muted = is_muted;
|
|
|
|
};
|
|
|
|
|
2020-03-21 14:42:10 +01:00
|
|
|
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;
|