zulip/static/js/unread.js

329 lines
9.7 KiB
JavaScript
Raw Normal View History

2016-08-17 01:19:14 +02:00
// See http://zulip.readthedocs.io/en/latest/pointer.html for notes on
// how this system is designed.
var unread = (function () {
var exports = {};
var unread_mentioned = new Dict();
exports.suppress_unread_counts = true;
exports.messages_read_in_narrow = false;
exports.unread_pm_counter = (function () {
var self = {};
var unread_privates = new Dict(); // indexed by user_ids_string like 5,7,9
self.clear = function () {
unread_privates = new Dict();
};
self.add = function (message) {
var user_ids_string = people.pm_reply_user_string(message);
if (user_ids_string) {
unread_privates.setdefault(user_ids_string, new Dict());
unread_privates.get(user_ids_string).set(message.id, true);
}
};
self.del = function (message) {
var user_ids_string = people.pm_reply_user_string(message);
if (user_ids_string) {
var dict = unread_privates.get(user_ids_string);
if (dict) {
dict.del(message.id);
}
}
};
self.get_counts = function () {
var pm_dict = new Dict(); // Hash by user_ids_string -> count
var total_count = 0;
unread_privates.each(function (obj, user_ids_string) {
var count = obj.num_items();
pm_dict.set(user_ids_string, count);
total_count += count;
});
return {
total_count: total_count,
pm_dict: pm_dict,
};
};
self.num_unread = function (user_ids_string) {
if (!user_ids_string) {
return 0;
}
if (!unread_privates.has(user_ids_string)) {
return 0;
}
return unread_privates.get(user_ids_string).num_items();
};
return self;
}());
exports.unread_topic_counter = (function () {
var self = {};
function str_dict() {
2017-05-13 19:26:54 +02:00
// Use this when keys are topics
return new Dict({fold_case: true});
}
function num_dict() {
// Use this for message ids.
return new Dict();
}
2017-05-13 19:26:54 +02:00
var unread_topics = num_dict(); // dict of stream -> topic -> msg id
self.clear = function () {
2017-05-13 19:26:54 +02:00
unread_topics = num_dict();
};
self.update = function (stream_id, topic, new_topic, msg_id) {
2017-05-13 19:26:54 +02:00
if (unread_topics.has(stream_id) &&
unread_topics.get(stream_id).has(topic) &&
unread_topics.get(stream_id).get(topic).get(msg_id)) {
// Move the unread topic count to the new topic
unread_topics.get(stream_id).get(topic).del(msg_id);
if (unread_topics.get(stream_id).get(topic).num_items() === 0) {
unread_topics.get(stream_id).del(topic);
}
unread_topics.get(stream_id).setdefault(new_topic, num_dict());
unread_topics.get(stream_id).get(new_topic).set(msg_id, true);
}
};
self.add = function (stream_id, topic, msg_id) {
2017-05-13 19:26:54 +02:00
unread_topics.setdefault(stream_id, str_dict());
unread_topics.get(stream_id).setdefault(topic, num_dict());
unread_topics.get(stream_id).get(topic).set(msg_id, true);
};
self.del = function (stream_id, topic, msg_id) {
2017-05-13 19:26:54 +02:00
var stream_dict = unread_topics.get(stream_id);
if (stream_dict) {
var topic_dict = stream_dict.get(topic);
if (topic_dict) {
topic_dict.del(msg_id);
}
}
};
self.get_counts = function () {
var res = {};
res.stream_unread_messages = 0;
2017-05-13 19:26:54 +02:00
res.stream_count = num_dict(); // hash by stream_id -> count
res.topic_count = num_dict(); // hash of hashes (stream_id, then topic -> count)
2017-05-13 19:26:54 +02:00
unread_topics.each(function (_, stream_id) {
// We track unread counts for streams that may be currently
// unsubscribed. Since users may re-subscribe, we don't
// completely throw away the data. But we do ignore it here,
// so that callers have a view of the **current** world.
2017-05-13 19:26:54 +02:00
var sub = stream_data.get_sub_by_id(stream_id);
if (!sub || !stream_data.is_subscribed(sub.name)) {
return true;
}
2017-05-13 19:26:54 +02:00
if (unread_topics.has(stream_id)) {
res.topic_count.set(stream_id, str_dict());
var stream_count = 0;
unread_topics.get(stream_id).each(function (msgs, topic) {
var topic_count = msgs.num_items();
res.topic_count.get(stream_id).set(topic, topic_count);
if (!muting.is_topic_muted(sub.name, topic)) {
stream_count += topic_count;
}
});
2017-05-13 19:26:54 +02:00
res.stream_count.set(stream_id, stream_count);
if (stream_data.in_home_view(stream_id)) {
res.stream_unread_messages += stream_count;
}
}
});
return res;
};
2017-05-13 19:26:54 +02:00
self.get_stream_count = function (stream_id) {
var stream_count = 0;
2017-05-13 19:26:54 +02:00
if (!unread_topics.has(stream_id)) {
return 0;
}
unread_topics.get(stream_id).each(function (msgs, topic) {
2017-05-13 19:26:54 +02:00
var sub = stream_data.get_sub_by_id(stream_id);
if (sub && !muting.is_topic_muted(sub.name, topic)) {
stream_count += msgs.num_items();
}
});
return stream_count;
};
self.get = function (stream_id, topic) {
var num_unread = 0;
2017-05-13 19:26:54 +02:00
if (unread_topics.has(stream_id) &&
unread_topics.get(stream_id).has(topic)) {
num_unread = unread_topics.get(stream_id).get(topic).num_items();
}
return num_unread;
};
2017-05-13 19:26:54 +02:00
self.topic_has_any_unread = function (stream_id, topic) {
var stream_dct = unread_topics.get(stream_id);
2017-04-21 18:00:19 +02:00
if (!stream_dct) {
return false;
}
var topic_dct = stream_dct.get(topic);
if (!topic_dct) {
return false;
}
return !topic_dct.is_empty();
};
return self;
}());
exports.message_unread = function (message) {
if (message === undefined) {
return false;
}
return message.flags === undefined ||
message.flags.indexOf('read') === -1;
};
exports.update_unread_topics = function (msg, event) {
if (event.subject !== undefined) {
exports.unread_topic_counter.update(
2017-05-13 19:26:54 +02:00
msg.stream_id,
msg.subject,
event.subject,
msg.id
);
}
};
exports.process_loaded_messages = function (messages) {
_.each(messages, function (message) {
var unread = exports.message_unread(message);
if (!unread) {
return;
}
if (message.type === 'private') {
exports.unread_pm_counter.add(message);
}
if (message.type === 'stream') {
exports.unread_topic_counter.add(
2017-05-13 19:26:54 +02:00
message.stream_id,
message.subject,
message.id
);
}
if (message.mentioned) {
unread_mentioned.set(message.id, true);
}
});
};
exports.process_read_message = function (message) {
if (message.type === 'private') {
exports.unread_pm_counter.del(message);
}
if (message.type === 'stream') {
if (message.stream_id === undefined) {
blueslip.error('No stream_id found for message ' + message.id);
return;
}
exports.unread_topic_counter.del(
2017-05-13 19:26:54 +02:00
message.stream_id,
message.subject,
message.id
);
}
unread_mentioned.del(message.id);
};
exports.declare_bankruptcy = function () {
exports.unread_pm_counter.clear();
exports.unread_topic_counter.clear();
unread_mentioned = new Dict();
};
Clean up code for unread counts and notifications. The core simplification here is that zephyr.js no longer has: * the global home_unread_messages * the function unread_in_current_view() [which used the global] The logic that used to be in zephyr is now in its proper home of unread.js, which has these changes: * the structure returned from unread.get_counts() includes a new member called unread_in_current_view * there's a helper function unread.num_unread_current_messages() Deprecating zephyr.unread_in_current_view() affected two callers: * notifications.update_title_count() * notifications_bar.update() The above functions used to call back to zephyr to get counts, but there was no nice way to enforce that they were getting counts at the right time in the code flow, because they depended on functions like process_visible_unread_messages() to orchestrate updating internal unread counts before pushing out counts to the DOM. Now both of those function take a parameter with the unread count, and we then had to change all of their callers appropriately. This went hand in hand with another goal, which is that we want all the unread-counts logic to funnel though basically one place, which is zephyr.update_unread_counts(). So now that function always calls notifications_bar.update() [NEW] as well as calling into the modules unread.js, stream_list.js, and notifications.js [OLD]. Adding the call to notifications_bar.update() in update_unread_counts() made it so that some other places in the code no longer needed to call notifications_bar.update(), so you'll see some lines of code removed. There are also cases where notifications.update_title_count() was called redundantly, since the callers were already reaching update_unread_counts() via other calls. Finally, in ui.resizehandler, you'll see a simple case where the call to notifications_bar.update() is preceded by an explicit call to unread.get_counts(). (imported from commit ce84b9c8076c1f9bb20a61209913f0cb0dae098c)
2013-06-05 21:04:06 +02:00
exports.num_unread_current_messages = function () {
var num_unread = 0;
_.each(current_msg_list.all_messages(), function (msg) {
Clean up code for unread counts and notifications. The core simplification here is that zephyr.js no longer has: * the global home_unread_messages * the function unread_in_current_view() [which used the global] The logic that used to be in zephyr is now in its proper home of unread.js, which has these changes: * the structure returned from unread.get_counts() includes a new member called unread_in_current_view * there's a helper function unread.num_unread_current_messages() Deprecating zephyr.unread_in_current_view() affected two callers: * notifications.update_title_count() * notifications_bar.update() The above functions used to call back to zephyr to get counts, but there was no nice way to enforce that they were getting counts at the right time in the code flow, because they depended on functions like process_visible_unread_messages() to orchestrate updating internal unread counts before pushing out counts to the DOM. Now both of those function take a parameter with the unread count, and we then had to change all of their callers appropriately. This went hand in hand with another goal, which is that we want all the unread-counts logic to funnel though basically one place, which is zephyr.update_unread_counts(). So now that function always calls notifications_bar.update() [NEW] as well as calling into the modules unread.js, stream_list.js, and notifications.js [OLD]. Adding the call to notifications_bar.update() in update_unread_counts() made it so that some other places in the code no longer needed to call notifications_bar.update(), so you'll see some lines of code removed. There are also cases where notifications.update_title_count() was called redundantly, since the callers were already reaching update_unread_counts() via other calls. Finally, in ui.resizehandler, you'll see a simple case where the call to notifications_bar.update() is preceded by an explicit call to unread.get_counts(). (imported from commit ce84b9c8076c1f9bb20a61209913f0cb0dae098c)
2013-06-05 21:04:06 +02:00
if ((msg.id > current_msg_list.selected_id()) && exports.message_unread(msg)) {
num_unread += 1;
}
});
return num_unread;
};
exports.get_counts = function () {
var res = {};
// Return a data structure with various counts. This function should be
// pretty cheap, even if you don't care about all the counts, and you
// should strive to keep it free of side effects on globals or DOM.
res.private_message_count = 0;
res.mentioned_message_count = unread_mentioned.num_items();
// This sets stream_count, topic_count, and home_unread_messages
var topic_res = exports.unread_topic_counter.get_counts();
res.home_unread_messages = topic_res.stream_unread_messages;
res.stream_count = topic_res.stream_count;
res.topic_count = topic_res.topic_count;
var pm_res = exports.unread_pm_counter.get_counts();
res.pm_count = pm_res.pm_dict;
res.private_message_count = pm_res.total_count;
res.home_unread_messages += pm_res.total_count;
if (narrow_state.active()) {
Clean up code for unread counts and notifications. The core simplification here is that zephyr.js no longer has: * the global home_unread_messages * the function unread_in_current_view() [which used the global] The logic that used to be in zephyr is now in its proper home of unread.js, which has these changes: * the structure returned from unread.get_counts() includes a new member called unread_in_current_view * there's a helper function unread.num_unread_current_messages() Deprecating zephyr.unread_in_current_view() affected two callers: * notifications.update_title_count() * notifications_bar.update() The above functions used to call back to zephyr to get counts, but there was no nice way to enforce that they were getting counts at the right time in the code flow, because they depended on functions like process_visible_unread_messages() to orchestrate updating internal unread counts before pushing out counts to the DOM. Now both of those function take a parameter with the unread count, and we then had to change all of their callers appropriately. This went hand in hand with another goal, which is that we want all the unread-counts logic to funnel though basically one place, which is zephyr.update_unread_counts(). So now that function always calls notifications_bar.update() [NEW] as well as calling into the modules unread.js, stream_list.js, and notifications.js [OLD]. Adding the call to notifications_bar.update() in update_unread_counts() made it so that some other places in the code no longer needed to call notifications_bar.update(), so you'll see some lines of code removed. There are also cases where notifications.update_title_count() was called redundantly, since the callers were already reaching update_unread_counts() via other calls. Finally, in ui.resizehandler, you'll see a simple case where the call to notifications_bar.update() is preceded by an explicit call to unread.get_counts(). (imported from commit ce84b9c8076c1f9bb20a61209913f0cb0dae098c)
2013-06-05 21:04:06 +02:00
res.unread_in_current_view = exports.num_unread_current_messages();
} else {
Clean up code for unread counts and notifications. The core simplification here is that zephyr.js no longer has: * the global home_unread_messages * the function unread_in_current_view() [which used the global] The logic that used to be in zephyr is now in its proper home of unread.js, which has these changes: * the structure returned from unread.get_counts() includes a new member called unread_in_current_view * there's a helper function unread.num_unread_current_messages() Deprecating zephyr.unread_in_current_view() affected two callers: * notifications.update_title_count() * notifications_bar.update() The above functions used to call back to zephyr to get counts, but there was no nice way to enforce that they were getting counts at the right time in the code flow, because they depended on functions like process_visible_unread_messages() to orchestrate updating internal unread counts before pushing out counts to the DOM. Now both of those function take a parameter with the unread count, and we then had to change all of their callers appropriately. This went hand in hand with another goal, which is that we want all the unread-counts logic to funnel though basically one place, which is zephyr.update_unread_counts(). So now that function always calls notifications_bar.update() [NEW] as well as calling into the modules unread.js, stream_list.js, and notifications.js [OLD]. Adding the call to notifications_bar.update() in update_unread_counts() made it so that some other places in the code no longer needed to call notifications_bar.update(), so you'll see some lines of code removed. There are also cases where notifications.update_title_count() was called redundantly, since the callers were already reaching update_unread_counts() via other calls. Finally, in ui.resizehandler, you'll see a simple case where the call to notifications_bar.update() is preceded by an explicit call to unread.get_counts(). (imported from commit ce84b9c8076c1f9bb20a61209913f0cb0dae098c)
2013-06-05 21:04:06 +02:00
res.unread_in_current_view = res.home_unread_messages;
}
return res;
};
2017-05-13 19:26:54 +02:00
exports.num_unread_for_stream = function (stream_id) {
return exports.unread_topic_counter.get_stream_count(stream_id);
};
exports.num_unread_for_topic = function (stream_id, subject) {
2017-05-13 19:26:54 +02:00
return exports.unread_topic_counter.get(stream_id, subject);
};
2017-05-13 19:26:54 +02:00
exports.topic_has_any_unread = function (stream_id, topic) {
return exports.unread_topic_counter.topic_has_any_unread(stream_id, topic);
2017-04-21 18:00:19 +02:00
};
exports.num_unread_for_person = function (user_ids_string) {
return exports.unread_pm_counter.num_unread(user_ids_string);
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = unread;
}