2017-11-16 19:51:44 +01:00
|
|
|
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html for notes on
|
2016-08-17 01:19:14 +02:00
|
|
|
// how this system is designed.
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
var unread = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2014-01-31 18:06:38 +01:00
|
|
|
exports.suppress_unread_counts = true;
|
2016-04-03 16:45:07 +02:00
|
|
|
exports.messages_read_in_narrow = false;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2017-08-03 03:28:11 +02:00
|
|
|
function make_id_set() {
|
|
|
|
/* This is just a basic set class where
|
|
|
|
elements should be numeric ids.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var self = {};
|
|
|
|
var ids = new Dict();
|
|
|
|
|
|
|
|
self.clear = function () {
|
|
|
|
ids.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.add = function (id) {
|
|
|
|
ids.set(id, true);
|
|
|
|
};
|
|
|
|
|
2017-08-15 19:54:38 +02:00
|
|
|
self.has = function (id) {
|
|
|
|
return ids.has(id);
|
|
|
|
};
|
|
|
|
|
2017-08-01 14:50:40 +02:00
|
|
|
self.add_many = function (id_list) {
|
|
|
|
_.each(id_list, function (id) {
|
|
|
|
ids.set(id, true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-03 03:28:11 +02:00
|
|
|
self.del = function (id) {
|
|
|
|
ids.del(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.count = function () {
|
|
|
|
return ids.num_items();
|
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
self.members = function () {
|
|
|
|
return ids.keys();
|
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
self.max = function () {
|
|
|
|
return _.max(ids.keys());
|
|
|
|
};
|
|
|
|
|
2017-08-03 03:28:11 +02:00
|
|
|
self.is_empty = function () {
|
|
|
|
return ids.is_empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-15 19:54:38 +02:00
|
|
|
var unread_messages = make_id_set();
|
|
|
|
|
2017-08-03 04:32:21 +02:00
|
|
|
function make_bucketer(options) {
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
var key_to_bucket = new Dict({fold_case: options.fold_case});
|
|
|
|
var reverse_lookup = new Dict();
|
|
|
|
|
|
|
|
self.clear = function () {
|
|
|
|
key_to_bucket.clear();
|
|
|
|
reverse_lookup.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.add = function (opts) {
|
|
|
|
var bucket_key = opts.bucket_key;
|
|
|
|
var item_id = opts.item_id;
|
|
|
|
var add_callback = opts.add_callback;
|
|
|
|
|
|
|
|
var bucket = key_to_bucket.get(bucket_key);
|
|
|
|
if (!bucket) {
|
|
|
|
bucket = options.make_bucket();
|
|
|
|
key_to_bucket.set(bucket_key, bucket);
|
|
|
|
}
|
|
|
|
if (add_callback) {
|
|
|
|
add_callback(bucket, item_id);
|
|
|
|
} else {
|
|
|
|
bucket.add(item_id);
|
|
|
|
}
|
|
|
|
reverse_lookup.set(item_id, bucket);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.del = function (item_id) {
|
|
|
|
var bucket = reverse_lookup.get(item_id);
|
|
|
|
if (bucket) {
|
|
|
|
bucket.del(item_id);
|
|
|
|
reverse_lookup.del(item_id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_bucket = function (bucket_key) {
|
|
|
|
return key_to_bucket.get(bucket_key);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.each = function (callback) {
|
|
|
|
key_to_bucket.each(callback);
|
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
self.keys = function () {
|
|
|
|
return key_to_bucket.keys();
|
|
|
|
};
|
|
|
|
|
2017-08-03 04:32:21 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter = (function () {
|
|
|
|
var self = {};
|
2017-08-03 04:32:21 +02:00
|
|
|
|
|
|
|
var bucketer = make_bucketer({
|
|
|
|
fold_case: false,
|
|
|
|
make_bucket: make_id_set,
|
|
|
|
});
|
2017-07-31 14:59:18 +02:00
|
|
|
|
|
|
|
self.clear = function () {
|
2017-08-03 04:32:21 +02:00
|
|
|
bucketer.clear();
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
|
|
|
|
2017-08-01 14:50:40 +02:00
|
|
|
self.set_pms = function (pms) {
|
|
|
|
_.each(pms, function (obj) {
|
|
|
|
var user_ids_string = obj.sender_id.toString();
|
|
|
|
self.set_message_ids(user_ids_string, obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_huddles = function (huddles) {
|
|
|
|
_.each(huddles, function (obj) {
|
|
|
|
var user_ids_string = people.pm_lookup_key(obj.user_ids_string);
|
|
|
|
self.set_message_ids(user_ids_string, obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_message_ids = function (user_ids_string, unread_message_ids) {
|
|
|
|
_.each(unread_message_ids, function (msg_id) {
|
|
|
|
bucketer.add({
|
|
|
|
bucket_key: user_ids_string,
|
|
|
|
item_id: msg_id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-31 14:59:18 +02:00
|
|
|
self.add = function (message) {
|
|
|
|
var user_ids_string = people.pm_reply_user_string(message);
|
|
|
|
if (user_ids_string) {
|
2017-08-03 04:32:21 +02:00
|
|
|
bucketer.add({
|
|
|
|
bucket_key: user_ids_string,
|
|
|
|
item_id: message.id,
|
|
|
|
});
|
2017-07-31 14:59:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-02 21:01:12 +02:00
|
|
|
self.del = function (message_id) {
|
2017-08-03 04:32:21 +02:00
|
|
|
bucketer.del(message_id);
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.get_counts = function () {
|
|
|
|
var pm_dict = new Dict(); // Hash by user_ids_string -> count
|
|
|
|
var total_count = 0;
|
2017-08-03 04:32:21 +02:00
|
|
|
bucketer.each(function (id_set, user_ids_string) {
|
2017-08-03 03:28:11 +02:00
|
|
|
var count = id_set.count();
|
2017-07-31 14:59:18 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-03 04:32:21 +02:00
|
|
|
var bucket = bucketer.get_bucket(user_ids_string);
|
|
|
|
|
|
|
|
if (!bucket) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-03 04:32:21 +02:00
|
|
|
return bucket.count();
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 19:50:25 +02:00
|
|
|
self.get_msg_ids = function () {
|
|
|
|
var lists = [];
|
|
|
|
|
|
|
|
bucketer.each(function (id_set) {
|
|
|
|
var members = id_set.members();
|
|
|
|
lists.push(members);
|
|
|
|
});
|
|
|
|
|
|
|
|
var ids = [].concat.apply([], lists);
|
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_msg_ids_for_person = function (user_ids_string) {
|
2018-05-02 13:02:32 +02:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var bucket = bucketer.get_bucket(user_ids_string);
|
|
|
|
|
|
|
|
if (!bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids = bucket.members();
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2017-07-31 14:59:18 +02:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
function make_per_stream_bucketer() {
|
|
|
|
return make_bucketer({
|
|
|
|
fold_case: true, // bucket keys are topics
|
|
|
|
make_bucket: make_id_set,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-30 03:20:29 +01:00
|
|
|
exports.unread_topic_counter = (function () {
|
|
|
|
var self = {};
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
var bucketer = make_bucketer({
|
|
|
|
fold_case: false, // bucket keys are stream_ids
|
|
|
|
make_bucket: make_per_stream_bucketer,
|
|
|
|
});
|
2016-11-30 03:20:29 +01:00
|
|
|
|
|
|
|
self.clear = function () {
|
2017-08-03 05:01:47 +02:00
|
|
|
bucketer.clear();
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
self.set_streams = function (objs) {
|
|
|
|
_.each(objs, function (obj) {
|
|
|
|
var stream_id = obj.stream_id;
|
|
|
|
var topic = obj.topic;
|
|
|
|
var unread_message_ids = obj.unread_message_ids;
|
|
|
|
|
|
|
|
_.each(unread_message_ids, function (msg_id) {
|
|
|
|
self.add(stream_id, topic, msg_id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-31 13:56:04 +02:00
|
|
|
self.add = function (stream_id, topic, msg_id) {
|
2017-08-03 05:01:47 +02:00
|
|
|
bucketer.add({
|
|
|
|
bucket_key: stream_id,
|
|
|
|
item_id: msg_id,
|
|
|
|
add_callback: function (per_stream_bucketer) {
|
|
|
|
per_stream_bucketer.add({
|
|
|
|
bucket_key: topic,
|
|
|
|
item_id: msg_id,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2017-08-02 21:23:05 +02:00
|
|
|
self.del = function (msg_id) {
|
2017-08-03 05:01:47 +02:00
|
|
|
bucketer.del(msg_id);
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
function str_dict() {
|
|
|
|
// Use this when keys are topics
|
|
|
|
return new Dict({fold_case: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
function num_dict() {
|
|
|
|
// Use this for stream ids.
|
|
|
|
return new Dict();
|
|
|
|
}
|
|
|
|
|
2016-12-15 01:53:33 +01:00
|
|
|
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
|
2017-07-31 14:04:20 +02:00
|
|
|
res.topic_count = num_dict(); // hash of hashes (stream_id, then topic -> count)
|
2017-08-03 05:01:47 +02:00
|
|
|
bucketer.each(function (per_stream_bucketer, stream_id) {
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2016-12-15 01:59:08 +01:00
|
|
|
// 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)) {
|
2017-08-03 05:01:47 +02:00
|
|
|
return;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
res.topic_count.set(stream_id, str_dict());
|
|
|
|
var stream_count = 0;
|
|
|
|
per_stream_bucketer.each(function (msgs, topic) {
|
|
|
|
var topic_count = msgs.count();
|
|
|
|
res.topic_count.get(stream_id).set(topic, topic_count);
|
|
|
|
if (!muting.is_topic_muted(sub.name, topic)) {
|
|
|
|
stream_count += topic_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
});
|
|
|
|
res.stream_count.set(stream_id, stream_count);
|
|
|
|
if (stream_data.in_home_view(stream_id)) {
|
|
|
|
res.stream_unread_messages += stream_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2016-12-15 01:53:33 +01:00
|
|
|
|
|
|
|
return res;
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
self.get_missing_topics = function (opts) {
|
|
|
|
var stream_id = opts.stream_id;
|
|
|
|
var topic_dict = opts.topic_dict;
|
|
|
|
|
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var topic_names = per_stream_bucketer.keys();
|
|
|
|
|
|
|
|
topic_names = _.reject(topic_names, function (topic_name) {
|
|
|
|
return topic_dict.has(topic_name);
|
|
|
|
});
|
|
|
|
|
|
|
|
var result = _.map(topic_names, function (topic_name) {
|
|
|
|
var msgs = per_stream_bucketer.get_bucket(topic_name);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pretty_name: topic_name,
|
|
|
|
message_id: msgs.max(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
self.get_stream_count = function (stream_id) {
|
2017-01-15 16:44:33 +01:00
|
|
|
var stream_count = 0;
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
2017-01-15 16:44:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:49:06 +02:00
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
per_stream_bucketer.each(function (msgs, topic) {
|
2017-07-31 13:56:04 +02:00
|
|
|
if (sub && !muting.is_topic_muted(sub.name, topic)) {
|
2017-08-03 03:28:11 +02:00
|
|
|
stream_count += msgs.count();
|
2017-01-15 16:44:33 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return stream_count;
|
|
|
|
};
|
|
|
|
|
2017-07-31 13:56:04 +02:00
|
|
|
self.get = function (stream_id, topic) {
|
2017-08-03 05:01:47 +02:00
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return 0;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
|
|
|
|
var topic_bucket = per_stream_bucketer.get_bucket(topic);
|
|
|
|
if (!topic_bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return topic_bucket.count();
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2018-05-01 21:22:49 +02:00
|
|
|
self.get_msg_ids_for_stream = function (stream_id) {
|
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var topic_lists = [];
|
2018-05-13 11:49:06 +02:00
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
2018-05-01 21:22:49 +02:00
|
|
|
per_stream_bucketer.each(function (msgs, topic) {
|
|
|
|
if (sub && !muting.is_topic_muted(sub.name, topic)) {
|
|
|
|
topic_lists.push(msgs.members());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var ids = [].concat.apply([], topic_lists);
|
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
self.get_msg_ids_for_topic = function (stream_id, topic) {
|
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var topic_bucket = per_stream_bucketer.get_bucket(topic);
|
|
|
|
if (!topic_bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids = topic_bucket.members();
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
self.topic_has_any_unread = function (stream_id, topic) {
|
2017-08-03 05:01:47 +02:00
|
|
|
var per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2017-04-21 18:00:19 +02:00
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
var id_set = per_stream_bucketer.get_bucket(topic);
|
|
|
|
if (!id_set) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:01:47 +02:00
|
|
|
return !id_set.is_empty();
|
2017-04-21 18:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-11-30 03:20:29 +01:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2017-08-03 03:28:11 +02:00
|
|
|
exports.unread_mentions_counter = make_id_set();
|
2017-08-01 14:04:48 +02:00
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
exports.message_unread = function (message) {
|
|
|
|
if (message === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-16 21:42:41 +01:00
|
|
|
return message.unread;
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
exports.get_unread_message_ids = function (message_ids) {
|
|
|
|
return _.filter(message_ids, unread_messages.has);
|
|
|
|
};
|
|
|
|
|
2018-03-28 21:00:48 +02:00
|
|
|
exports.get_unread_messages = function (messages) {
|
|
|
|
return _.filter(messages, function (message) {
|
2017-12-16 16:53:27 +01:00
|
|
|
return unread_messages.has(message.id);
|
|
|
|
});
|
2017-08-03 22:01:21 +02:00
|
|
|
};
|
|
|
|
|
2016-08-27 03:29:32 +02:00
|
|
|
exports.update_unread_topics = function (msg, event) {
|
2017-08-10 20:27:23 +02:00
|
|
|
if (event.subject === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unread_messages.has(msg.id)) {
|
|
|
|
return;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2017-08-10 20:27:23 +02:00
|
|
|
|
|
|
|
exports.unread_topic_counter.del(
|
|
|
|
msg.id
|
|
|
|
);
|
|
|
|
|
|
|
|
exports.unread_topic_counter.add(
|
|
|
|
msg.stream_id,
|
|
|
|
event.subject,
|
|
|
|
msg.id
|
|
|
|
);
|
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) {
|
2017-12-16 21:42:41 +01:00
|
|
|
if (!message.unread) {
|
2013-05-17 21:32:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-15 19:54:38 +02:00
|
|
|
unread_messages.add(message.id);
|
2017-08-03 22:01:21 +02:00
|
|
|
|
2013-09-27 23:20:52 +02:00
|
|
|
if (message.type === 'private') {
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter.add(message);
|
2013-09-27 23:20:52 +02:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
|
|
|
|
if (message.type === 'stream') {
|
2016-11-30 03:20:29 +01:00
|
|
|
exports.unread_topic_counter.add(
|
2017-05-13 19:26:54 +02:00
|
|
|
message.stream_id,
|
2016-11-30 03:20:29 +01:00
|
|
|
message.subject,
|
|
|
|
message.id
|
|
|
|
);
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
|
|
|
|
if (message.mentioned) {
|
2017-08-01 14:04:48 +02:00
|
|
|
exports.unread_mentions_counter.add(message.id);
|
2013-05-30 22:15:59 +02:00
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
exports.mark_as_read = function (message_id) {
|
|
|
|
// We don't need to check anything about the message, since all
|
|
|
|
// the following methods are cheap and work fine even if message_id
|
|
|
|
// was never set to unread.
|
|
|
|
exports.unread_pm_counter.del(message_id);
|
|
|
|
exports.unread_topic_counter.del(message_id);
|
|
|
|
exports.unread_mentions_counter.del(message_id);
|
2017-08-03 22:01:21 +02:00
|
|
|
unread_messages.del(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
|
|
|
|
var message = message_store.get(message_id);
|
|
|
|
if (message) {
|
|
|
|
message.unread = false;
|
|
|
|
}
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.declare_bankruptcy = function () {
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter.clear();
|
2016-11-30 03:20:29 +01:00
|
|
|
exports.unread_topic_counter.clear();
|
2017-08-01 14:04:48 +02:00
|
|
|
exports.unread_mentions_counter.clear();
|
2017-08-03 22:01:21 +02:00
|
|
|
unread_messages.clear();
|
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;
|
2017-08-01 14:04:48 +02:00
|
|
|
res.mentioned_message_count = exports.unread_mentions_counter.count();
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2017-07-31 14:04:20 +02:00
|
|
|
// This sets stream_count, topic_count, and home_unread_messages
|
2017-07-31 15:06:19 +02:00
|
|
|
var topic_res = exports.unread_topic_counter.get_counts();
|
2016-12-15 01:53:33 +01:00
|
|
|
res.home_unread_messages = topic_res.stream_unread_messages;
|
|
|
|
res.stream_count = topic_res.stream_count;
|
2017-07-31 14:04:20 +02:00
|
|
|
res.topic_count = topic_res.topic_count;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2017-07-31 14:59:18 +02:00
|
|
|
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;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
|
|
|
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);
|
2017-01-15 16:44:33 +01:00
|
|
|
};
|
|
|
|
|
2017-07-31 14:11:18 +02:00
|
|
|
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);
|
2013-05-17 21:32:26 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2016-11-18 17:02:06 +01:00
|
|
|
exports.num_unread_for_person = function (user_ids_string) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return exports.unread_pm_counter.num_unread(user_ids_string);
|
2013-08-22 19:06:04 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 21:22:49 +02:00
|
|
|
exports.get_msg_ids_for_stream = function (stream_id) {
|
|
|
|
return exports.unread_topic_counter.get_msg_ids_for_stream(stream_id);
|
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
exports.get_msg_ids_for_topic = function (stream_id, subject) {
|
|
|
|
return exports.unread_topic_counter.get_msg_ids_for_topic(stream_id, subject);
|
|
|
|
};
|
|
|
|
|
2018-05-02 13:02:32 +02:00
|
|
|
exports.get_msg_ids_for_person = function (user_ids_string) {
|
2018-05-02 19:50:25 +02:00
|
|
|
return exports.unread_pm_counter.get_msg_ids_for_person(user_ids_string);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_msg_ids_for_private = function () {
|
|
|
|
return exports.unread_pm_counter.get_msg_ids();
|
2018-05-02 13:02:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 19:54:36 +02:00
|
|
|
exports.get_msg_ids_for_mentions = function () {
|
|
|
|
var ids = exports.unread_mentions_counter.members();
|
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-05-10 13:11:53 +02:00
|
|
|
exports.get_all_msg_ids = function () {
|
|
|
|
var ids = unread_messages.members();
|
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
exports.get_missing_topics = function (opts) {
|
|
|
|
return exports.unread_topic_counter.get_missing_topics(opts);
|
|
|
|
};
|
|
|
|
|
2018-05-03 22:57:07 +02:00
|
|
|
exports.get_msg_ids_for_starred = function () {
|
|
|
|
// This is here for API consistency sake--we never
|
|
|
|
// have unread starred messages. (Some day we may ironically
|
|
|
|
// want to make starring the same as mark-as-unread, but
|
|
|
|
// for now starring === reading.)
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
2017-08-01 14:50:40 +02:00
|
|
|
exports.load_server_counts = function () {
|
|
|
|
var unread_msgs = page_params.unread_msgs;
|
|
|
|
|
|
|
|
exports.unread_pm_counter.set_huddles(unread_msgs.huddles);
|
|
|
|
exports.unread_pm_counter.set_pms(unread_msgs.pms);
|
|
|
|
exports.unread_topic_counter.set_streams(unread_msgs.streams);
|
|
|
|
exports.unread_mentions_counter.add_many(unread_msgs.mentions);
|
|
|
|
|
|
|
|
_.each(unread_msgs.huddles, function (obj) {
|
|
|
|
unread_messages.add_many(obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
_.each(unread_msgs.pms, function (obj) {
|
|
|
|
unread_messages.add_many(obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
_.each(unread_msgs.streams, function (obj) {
|
|
|
|
unread_messages.add_many(obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
unread_messages.add_many(unread_msgs.mentions);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.initialize = function () {
|
2017-09-22 19:20:19 +02:00
|
|
|
if (feature_flags.load_server_counts) {
|
|
|
|
exports.load_server_counts();
|
|
|
|
}
|
2017-08-01 14:50:40 +02:00
|
|
|
};
|
|
|
|
|
2013-05-17 21:32:26 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
2013-07-28 18:40:50 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = unread;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.unread = unread;
|