2013-05-17 21:32:26 +02:00
|
|
|
var unread = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-05-30 22:15:59 +02:00
|
|
|
var unread_counts = {
|
2013-08-07 23:56:51 +02:00
|
|
|
'stream': new Dict(),
|
|
|
|
'private': new Dict()
|
2013-05-30 22:15:59 +02:00
|
|
|
};
|
|
|
|
var unread_mentioned = {};
|
2013-08-07 23:56:51 +02:00
|
|
|
var unread_subjects = new Dict();
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2013-05-18 00:10:12 +02:00
|
|
|
function unread_hashkey(message) {
|
|
|
|
var hashkey;
|
|
|
|
if (message.type === 'stream') {
|
2013-05-22 18:06:40 +02:00
|
|
|
hashkey = subs.canonicalized_name(message.stream);
|
2013-05-18 00:10:12 +02:00
|
|
|
} else {
|
2013-06-05 20:29:31 +02:00
|
|
|
hashkey = message.reply_to;
|
2013-05-18 00:10:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! unread_counts[message.type].has(hashkey)) {
|
|
|
|
unread_counts[message.type].set(hashkey, {});
|
2013-05-18 00:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'stream') {
|
2013-05-22 18:06:40 +02:00
|
|
|
var canon_subject = subs.canonicalized_name(message.subject);
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! unread_subjects.has(hashkey)) {
|
|
|
|
unread_subjects.set(hashkey, new Dict());
|
2013-05-18 00:10:12 +02:00
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! unread_subjects.get(hashkey).has(canon_subject)) {
|
|
|
|
unread_subjects.get(hashkey).set(canon_subject, {});
|
2013-05-18 00:10:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashkey;
|
|
|
|
}
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
exports.message_unread = function (message) {
|
|
|
|
if (message === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return message.flags === undefined ||
|
|
|
|
message.flags.indexOf('read') === -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_unread_subjects = function (msg, event) {
|
2013-05-22 18:06:40 +02:00
|
|
|
var canon_stream = subs.canonicalized_name(msg.stream);
|
|
|
|
var canon_subject = subs.canonicalized_name(msg.subject);
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
if (event.subject !== undefined &&
|
2013-08-07 23:56:51 +02:00
|
|
|
unread_subjects.has(canon_stream) &&
|
|
|
|
unread_subjects.get(canon_stream).has(canon_subject) &&
|
|
|
|
unread_subjects.get(canon_stream).get(canon_subject)[msg.id]) {
|
2013-05-22 18:06:40 +02:00
|
|
|
var new_canon_subject = subs.canonicalized_name(event.subject);
|
2013-05-17 21:32:26 +02:00
|
|
|
// Move the unread subject count to the new subject
|
2013-08-07 23:56:51 +02:00
|
|
|
delete unread_subjects.get(canon_stream).get(canon_subject)[msg.id];
|
|
|
|
if (unread_subjects.get(canon_stream).get(canon_subject).length === 0) {
|
|
|
|
unread_subjects.get(canon_stream).del(canon_subject);
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
if (! unread_subjects.get(canon_stream).has(new_canon_subject)) {
|
|
|
|
unread_subjects.get(canon_stream).set(new_canon_subject, {});
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
unread_subjects.get(canon_stream).get(new_canon_subject)[msg.id] = true;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.process_loaded_messages = function (messages) {
|
2013-07-27 00:41:19 +02:00
|
|
|
_.each(messages, function (message) {
|
2013-05-17 21:32:26 +02:00
|
|
|
var unread = exports.message_unread(message);
|
|
|
|
if (!unread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var hashkey = unread_hashkey(message);
|
2013-08-07 23:56:51 +02:00
|
|
|
unread_counts[message.type].get(hashkey)[message.id] = true;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
|
|
|
if (message.type === 'stream') {
|
2013-05-22 18:06:40 +02:00
|
|
|
var canon_subject = subs.canonicalized_name(message.subject);
|
2013-08-07 23:56:51 +02:00
|
|
|
unread_subjects.get(hashkey).get(canon_subject)[message.id] = true;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
|
|
|
|
if (message.mentioned) {
|
|
|
|
unread_mentioned[message.id] = true;
|
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.process_read_message = function (message) {
|
|
|
|
var hashkey = unread_hashkey(message);
|
2013-08-07 23:56:51 +02:00
|
|
|
delete unread_counts[message.type].get(hashkey)[message.id];
|
2013-05-17 21:32:26 +02:00
|
|
|
if (message.type === 'stream') {
|
2013-05-22 18:06:40 +02:00
|
|
|
var canon_stream = subs.canonicalized_name(message.stream);
|
|
|
|
var canon_subject = subs.canonicalized_name(message.subject);
|
2013-08-07 23:56:51 +02:00
|
|
|
delete unread_subjects.get(canon_stream).get(canon_subject)[message.id];
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
delete unread_mentioned[message.id];
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.declare_bankruptcy = function () {
|
2013-08-07 23:56:51 +02:00
|
|
|
unread_counts = {'stream': new Dict(), 'private': new Dict()};
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2013-07-27 00:41:19 +02:00
|
|
|
_.each(current_msg_list.all(), 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;
|
|
|
|
};
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
exports.get_counts = function () {
|
|
|
|
var res = {};
|
2013-05-22 18:06:40 +02:00
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
// 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.home_unread_messages = 0;
|
2013-05-30 22:15:59 +02:00
|
|
|
res.mentioned_message_count = Object.keys(unread_mentioned).length;
|
2013-08-07 23:56:51 +02:00
|
|
|
res.stream_count = new Dict(); // hash by stream -> count
|
|
|
|
res.subject_count = new Dict(); // hash of hashes (stream, then subject -> count)
|
|
|
|
res.pm_count = new Dict(); // Hash by email -> count
|
2013-05-17 21:32:26 +02:00
|
|
|
|
|
|
|
function only_in_home_view(msgids) {
|
2013-07-27 00:41:19 +02:00
|
|
|
return _.filter(msgids, function (msgid) {
|
2013-05-17 21:32:26 +02:00
|
|
|
return home_msg_list.get(msgid) !== undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-15 16:16:12 +02:00
|
|
|
unread_counts.stream.each(function (msgs, stream) {
|
2013-06-12 21:13:19 +02:00
|
|
|
if (! subs.is_subscribed(stream)) {
|
2013-05-17 21:32:26 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var count = Object.keys(msgs).length;
|
2013-08-07 23:56:51 +02:00
|
|
|
res.stream_count.set(stream, count);
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2013-06-12 21:13:19 +02:00
|
|
|
if (subs.in_home_view(stream)) {
|
2013-05-17 21:32:26 +02:00
|
|
|
res.home_unread_messages += only_in_home_view(Object.keys(msgs)).length;
|
|
|
|
}
|
|
|
|
|
2013-08-07 23:56:51 +02:00
|
|
|
if (unread_subjects.has(stream)) {
|
|
|
|
res.subject_count.set(stream, new Dict());
|
2013-08-15 16:16:12 +02:00
|
|
|
unread_subjects.get(stream).each(function (msgs, subject) {
|
2013-08-07 23:56:51 +02:00
|
|
|
res.subject_count.get(stream).set(subject, Object.keys(msgs).length);
|
2013-05-17 21:32:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
var pm_count = 0;
|
2013-08-15 16:16:12 +02:00
|
|
|
unread_counts["private"].each(function (obj, index) {
|
2013-06-05 20:29:31 +02:00
|
|
|
var count = Object.keys(obj).length;
|
2013-08-07 23:56:51 +02:00
|
|
|
res.pm_count.set(index, count);
|
2013-06-05 20:29:31 +02:00
|
|
|
pm_count += count;
|
2013-05-17 21:32:26 +02:00
|
|
|
});
|
|
|
|
res.private_message_count = pm_count;
|
|
|
|
res.home_unread_messages += pm_count;
|
|
|
|
|
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 (narrow.active()) {
|
|
|
|
res.unread_in_current_view = exports.num_unread_current_messages();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.unread_in_current_view = res.home_unread_messages;
|
|
|
|
}
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.num_unread_for_subject = function (stream, subject) {
|
|
|
|
var num_unread = 0;
|
2013-08-07 23:56:51 +02:00
|
|
|
if (unread_subjects.has(stream) &&
|
|
|
|
unread_subjects.get(stream).has(subject)) {
|
|
|
|
num_unread = Object.keys(unread_subjects.get(stream).get(subject)).length;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
|
|
|
return num_unread;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
2013-07-28 18:40:50 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = unread;
|
|
|
|
}
|