2019-11-02 00:06:25 +01:00
|
|
|
const Dict = require('./dict').Dict;
|
2019-12-26 15:34:17 +01:00
|
|
|
const FoldDict = require('./fold_dict').FoldDict;
|
2019-12-30 14:42:00 +01:00
|
|
|
const IntDict = require('./int_dict').IntDict;
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-02-04 21:39:58 +01:00
|
|
|
// The unread module tracks the message IDs and locations of the
|
|
|
|
// user's unread messages. The tracking is initialized with
|
|
|
|
// server-provided data of the total set of unread messages in the
|
|
|
|
// user's history via page_params.unread_msgs (well, it cuts off at
|
|
|
|
// MAX_UNREAD_MESSAGES unreads for performance reasons). As a result,
|
|
|
|
// it can contain many thousands of messages that we don't have full
|
|
|
|
// data for in `message_store`, so we cannot in general look these
|
|
|
|
// messages up there.
|
|
|
|
|
|
|
|
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html
|
|
|
|
// for more details on how this system is designed.
|
2016-08-17 01:19:14 +02:00
|
|
|
|
2014-01-31 18:06:38 +01:00
|
|
|
exports.suppress_unread_counts = true;
|
2018-08-04 08:44:31 +02:00
|
|
|
exports.set_suppress_unread_counts = function (value) {
|
|
|
|
exports.suppress_unread_counts = value;
|
|
|
|
};
|
2016-04-03 16:45:07 +02:00
|
|
|
exports.messages_read_in_narrow = false;
|
2018-08-04 08:42:57 +02:00
|
|
|
exports.set_messages_read_in_narrow = function (value) {
|
|
|
|
exports.messages_read_in_narrow = value;
|
|
|
|
};
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2020-02-01 03:17:09 +01:00
|
|
|
const unread_messages = new Set();
|
2017-08-15 19:54:38 +02:00
|
|
|
|
2017-08-03 04:32:21 +02:00
|
|
|
function make_bucketer(options) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2020-02-04 02:48:42 +01:00
|
|
|
const key_to_bucket = new options.KeyDict();
|
2020-02-01 02:57:32 +01:00
|
|
|
const reverse_lookup = new IntDict();
|
2017-08-03 04:32:21 +02:00
|
|
|
|
|
|
|
self.clear = function () {
|
|
|
|
key_to_bucket.clear();
|
|
|
|
reverse_lookup.clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
self.add = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucket_key = opts.bucket_key;
|
|
|
|
const item_id = opts.item_id;
|
|
|
|
const add_callback = opts.add_callback;
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2020-02-04 02:48:42 +01:00
|
|
|
let bucket = key_to_bucket.get(bucket_key);
|
2017-08-03 04:32:21 +02:00
|
|
|
if (!bucket) {
|
|
|
|
bucket = options.make_bucket();
|
2020-02-04 02:48:42 +01:00
|
|
|
key_to_bucket.set(bucket_key, bucket);
|
2017-08-03 04:32:21 +02:00
|
|
|
}
|
|
|
|
if (add_callback) {
|
|
|
|
add_callback(bucket, item_id);
|
|
|
|
} else {
|
|
|
|
bucket.add(item_id);
|
|
|
|
}
|
|
|
|
reverse_lookup.set(item_id, bucket);
|
|
|
|
};
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
self.delete = function (item_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucket = reverse_lookup.get(item_id);
|
2017-08-03 04:32:21 +02:00
|
|
|
if (bucket) {
|
2020-02-01 03:28:35 +01:00
|
|
|
bucket.delete(item_id);
|
2020-02-03 07:41:38 +01:00
|
|
|
reverse_lookup.delete(item_id);
|
2017-08-03 04:32:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.get_bucket = function (bucket_key) {
|
2020-02-04 02:48:42 +01:00
|
|
|
return key_to_bucket.get(bucket_key);
|
2017-08-03 04:32:21 +02:00
|
|
|
};
|
|
|
|
|
2018-05-13 11:38:40 +02:00
|
|
|
self.keys = function () {
|
2020-02-04 02:48:42 +01:00
|
|
|
return key_to_bucket.keys();
|
2018-05-13 11:38:40 +02:00
|
|
|
};
|
|
|
|
|
2020-02-03 09:26:53 +01:00
|
|
|
self.values = function () {
|
|
|
|
return key_to_bucket.values();
|
|
|
|
};
|
|
|
|
|
|
|
|
self[Symbol.iterator] = function () {
|
|
|
|
return key_to_bucket[Symbol.iterator]();
|
|
|
|
};
|
|
|
|
|
2017-08-03 04:32:21 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-07-31 14:59:18 +02:00
|
|
|
exports.unread_pm_counter = (function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2017-08-03 04:32:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucketer = make_bucketer({
|
2020-02-04 02:48:42 +01:00
|
|
|
KeyDict: Dict,
|
2020-02-01 03:28:35 +01:00
|
|
|
make_bucket: () => new Set(),
|
2017-08-03 04:32:21 +02:00
|
|
|
});
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = obj.sender_id.toString();
|
2017-08-01 14:50:40 +02:00
|
|
|
self.set_message_ids(user_ids_string, obj.unread_message_ids);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_huddles = function (huddles) {
|
|
|
|
_.each(huddles, function (obj) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = people.pm_lookup_key(obj.user_ids_string);
|
2017-08-01 14:50:40 +02:00
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = people.pm_reply_user_string(message);
|
2017-07-31 14:59:18 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
self.delete = function (message_id) {
|
2020-02-01 03:28:35 +01:00
|
|
|
bucketer.delete(message_id);
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
self.get_counts = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_dict = new Dict(); // Hash by user_ids_string -> count
|
|
|
|
let total_count = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [user_ids_string, id_set] of bucketer) {
|
2020-02-01 03:28:35 +01:00
|
|
|
const count = id_set.size;
|
2017-07-31 14:59:18 +02:00
|
|
|
pm_dict.set(user_ids_string, count);
|
|
|
|
total_count += count;
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-07-31 14:59:18 +02:00
|
|
|
return {
|
|
|
|
total_count: total_count,
|
|
|
|
pm_dict: pm_dict,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
self.num_unread = function (user_ids_string) {
|
|
|
|
if (!user_ids_string) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucket = bucketer.get_bucket(user_ids_string);
|
2017-08-03 04:32:21 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
2017-07-31 14:59:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-02-01 03:28:35 +01:00
|
|
|
return bucket.size;
|
2017-07-31 14:59:18 +02:00
|
|
|
};
|
|
|
|
|
2018-05-02 19:50:25 +02:00
|
|
|
self.get_msg_ids = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const lists = [];
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const id_set of bucketer.values()) {
|
2020-02-04 23:46:56 +01:00
|
|
|
const members = Array.from(id_set);
|
2018-05-02 19:50:25 +02:00
|
|
|
lists.push(members);
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-02 19:50:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const ids = [].concat.apply([], lists);
|
2018-05-02 19:50:25 +02:00
|
|
|
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucket = bucketer.get_bucket(user_ids_string);
|
2018-05-02 13:02:32 +02:00
|
|
|
|
|
|
|
if (!bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(bucket);
|
2018-05-02 13:02:32 +02:00
|
|
|
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({
|
2020-02-04 02:48:42 +01:00
|
|
|
KeyDict: FoldDict, // bucket keys are topics
|
2020-02-01 03:28:35 +01:00
|
|
|
make_bucket: () => new Set(),
|
2017-08-03 05:01:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-30 03:20:29 +01:00
|
|
|
exports.unread_topic_counter = (function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2016-11-30 03:20:29 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bucketer = make_bucketer({
|
2020-02-04 02:48:42 +01:00
|
|
|
KeyDict: IntDict, // bucket keys are stream_ids
|
2017-08-03 05:01:47 +02:00
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = obj.stream_id;
|
|
|
|
const topic = obj.topic;
|
|
|
|
const unread_message_ids = obj.unread_message_ids;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
_.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
|
|
|
};
|
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
self.delete = function (msg_id) {
|
2020-02-01 03:28:35 +01:00
|
|
|
bucketer.delete(msg_id);
|
2016-11-30 03:20:29 +01:00
|
|
|
};
|
|
|
|
|
2016-12-15 01:53:33 +01:00
|
|
|
self.get_counts = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = {};
|
2016-12-15 01:53:33 +01:00
|
|
|
res.stream_unread_messages = 0;
|
2019-12-30 14:42:00 +01:00
|
|
|
res.stream_count = new IntDict(); // hash by stream_id -> count
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [stream_id, per_stream_bucketer] of bucketer) {
|
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.
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2017-05-13 19:26:54 +02:00
|
|
|
if (!sub || !stream_data.is_subscribed(sub.name)) {
|
2020-02-03 09:26:53 +01:00
|
|
|
continue;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream_count = 0;
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2020-02-01 03:28:35 +01:00
|
|
|
const topic_count = msgs.size;
|
2018-12-13 22:26:10 +01:00
|
|
|
if (!muting.is_topic_muted(stream_id, topic)) {
|
2017-08-03 05:01:47 +02:00
|
|
|
stream_count += topic_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
res.stream_count.set(stream_id, stream_count);
|
2019-05-21 09:33:21 +02:00
|
|
|
if (!stream_data.is_muted(stream_id)) {
|
2017-08-03 05:01:47 +02:00
|
|
|
res.stream_unread_messages += stream_count;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:26:53 +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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = opts.stream_id;
|
|
|
|
const topic_dict = opts.topic_dict;
|
2018-05-13 11:38:40 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2018-05-13 11:38:40 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
let topic_names = Array.from(per_stream_bucketer.keys());
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
topic_names = _.reject(topic_names, function (topic_name) {
|
|
|
|
return topic_dict.has(topic_name);
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = _.map(topic_names, function (topic_name) {
|
|
|
|
const msgs = per_stream_bucketer.get_bucket(topic_name);
|
2018-05-13 11:38:40 +02:00
|
|
|
|
|
|
|
return {
|
2020-02-04 02:48:42 +01:00
|
|
|
pretty_name: topic_name,
|
2020-02-04 23:48:49 +01:00
|
|
|
message_id: Math.max(...Array.from(msgs)),
|
2018-05-13 11:38:40 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
self.get_stream_count = function (stream_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let stream_count = 0;
|
2017-01-15 16:44:33 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
2017-01-15 16:44:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2018-12-13 22:26:10 +01:00
|
|
|
if (sub && !muting.is_topic_muted(stream_id, topic)) {
|
2020-02-01 03:28:35 +01:00
|
|
|
stream_count += msgs.size;
|
2017-01-15 16:44:33 +01:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return 0;
|
2016-11-30 03:20:29 +01:00
|
|
|
}
|
2017-08-03 05:01:47 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get_bucket(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return topic_bucket.size;
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_lists = [];
|
|
|
|
const sub = stream_data.get_sub_by_id(stream_id);
|
2020-02-03 09:26:53 +01:00
|
|
|
for (const [topic, msgs] of per_stream_bucketer) {
|
2018-12-13 22:26:10 +01:00
|
|
|
if (sub && !muting.is_topic_muted(stream_id, topic)) {
|
2020-02-04 23:46:56 +01:00
|
|
|
topic_lists.push(Array.from(msgs));
|
2018-05-01 21:22:49 +02:00
|
|
|
}
|
2020-02-03 09:26:53 +01:00
|
|
|
}
|
2018-05-01 21:22:49 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const ids = [].concat.apply([], topic_lists);
|
2018-05-01 21:22:49 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-04-25 22:48:51 +02:00
|
|
|
self.get_msg_ids_for_topic = function (stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const per_stream_bucketer = bucketer.get_bucket(stream_id);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!per_stream_bucketer) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_bucket = per_stream_bucketer.get_bucket(topic);
|
2018-04-25 22:48:51 +02:00
|
|
|
if (!topic_bucket) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(topic_bucket);
|
2018-04-25 22:48:51 +02:00
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
self.topic_has_any_unread = function (stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const id_set = per_stream_bucketer.get_bucket(topic);
|
2017-08-03 05:01:47 +02:00
|
|
|
if (!id_set) {
|
2017-04-21 18:00:19 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:28:35 +01:00
|
|
|
return id_set.size !== 0;
|
2017-04-21 18:00:19 +02:00
|
|
|
};
|
|
|
|
|
2016-11-30 03:20:29 +01:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2020-02-01 03:13:57 +01:00
|
|
|
exports.unread_mentions_counter = new 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) {
|
2020-02-01 03:17:09 +01:00
|
|
|
return _.filter(message_ids, message_id => unread_messages.has(message_id));
|
2017-12-16 16:53:27 +01:00
|
|
|
};
|
|
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_topic = util.get_edit_event_topic(event);
|
2018-12-22 17:45:18 +01:00
|
|
|
|
|
|
|
if (new_topic === undefined) {
|
2017-08-10 20:27:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!unread_messages.has(msg.id)) {
|
|
|
|
return;
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2017-08-10 20:27:23 +02:00
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
exports.unread_topic_counter.delete(
|
2017-08-10 20:27:23 +02:00
|
|
|
msg.id
|
|
|
|
);
|
|
|
|
|
|
|
|
exports.unread_topic_counter.add(
|
|
|
|
msg.stream_id,
|
2018-12-22 17:45:18 +01:00
|
|
|
new_topic,
|
2017-08-10 20:27:23 +02:00
|
|
|
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,
|
2018-12-23 16:01:30 +01:00
|
|
|
util.get_message_topic(message),
|
2016-11-30 03:20:29 +01:00
|
|
|
message.id
|
|
|
|
);
|
2013-05-17 21:32:26 +02:00
|
|
|
}
|
2013-05-30 22:15:59 +02:00
|
|
|
|
2019-09-27 11:11:40 +02:00
|
|
|
const is_unmuted_mention = message.type === 'stream' && message.mentioned &&
|
|
|
|
!muting.is_topic_muted(message.stream_id,
|
|
|
|
util.get_message_topic(message));
|
|
|
|
if (message.mentioned_me_directly || is_unmuted_mention) {
|
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.
|
2020-02-03 07:41:38 +01:00
|
|
|
exports.unread_pm_counter.delete(message_id);
|
|
|
|
exports.unread_topic_counter.delete(message_id);
|
2020-02-01 03:13:57 +01:00
|
|
|
exports.unread_mentions_counter.delete(message_id);
|
2020-02-01 03:17:09 +01:00
|
|
|
unread_messages.delete(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2017-12-16 21:42:41 +01:00
|
|
|
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 () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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;
|
2020-02-01 03:13:57 +01:00
|
|
|
res.mentioned_message_count = exports.unread_mentions_counter.size;
|
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
|
2019-11-02 00:06:25 +01:00
|
|
|
const 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;
|
2013-05-17 21:32:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_res = exports.unread_pm_counter.get_counts();
|
2017-07-31 14:59:18 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-07-20 14:45:56 +02:00
|
|
|
// Saves us from calling to get_counts() when we can avoid it.
|
|
|
|
exports.calculate_notifiable_count = function (res) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const only_show_notifiable = page_params.desktop_icon_count_display ===
|
2019-07-20 14:45:56 +02:00
|
|
|
settings_notifications.desktop_icon_count_display_values.notifiable.code;
|
2019-11-02 00:06:25 +01:00
|
|
|
const no_notifications = page_params.desktop_icon_count_display ===
|
2019-08-19 23:33:11 +02:00
|
|
|
settings_notifications.desktop_icon_count_display_values.none.code;
|
2019-07-20 14:45:56 +02:00
|
|
|
if (only_show_notifiable) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_NOTIFIABLE
|
|
|
|
new_message_count = res.mentioned_message_count + res.private_message_count;
|
2019-08-19 23:33:11 +02:00
|
|
|
} else if (no_notifications) {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_NONE
|
|
|
|
new_message_count = 0;
|
2019-07-20 14:45:56 +02:00
|
|
|
} else {
|
|
|
|
// DESKTOP_ICON_COUNT_DISPLAY_MESSAGES
|
|
|
|
new_message_count = res.home_unread_messages;
|
|
|
|
}
|
|
|
|
return new_message_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_notifiable_count = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = exports.get_counts();
|
2019-07-20 14:45:56 +02:00
|
|
|
return exports.calculate_notifiable_count(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
|
|
|
};
|
|
|
|
|
2018-12-22 15:20:54 +01:00
|
|
|
exports.num_unread_for_topic = function (stream_id, topic_name) {
|
|
|
|
return exports.unread_topic_counter.get(stream_id, topic_name);
|
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-12-22 15:20:54 +01:00
|
|
|
exports.get_msg_ids_for_topic = function (stream_id, topic_name) {
|
|
|
|
return exports.unread_topic_counter.get_msg_ids_for_topic(stream_id, topic_name);
|
2018-04-25 22:48:51 +02:00
|
|
|
};
|
|
|
|
|
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 () {
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(exports.unread_mentions_counter);
|
2018-05-02 19:54:36 +02:00
|
|
|
|
|
|
|
return util.sorted_ids(ids);
|
|
|
|
};
|
|
|
|
|
2018-05-10 13:11:53 +02:00
|
|
|
exports.get_all_msg_ids = function () {
|
2020-02-04 23:46:56 +01:00
|
|
|
const ids = Array.from(unread_messages);
|
2018-05-10 13:11:53 +02:00
|
|
|
|
|
|
|
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 [];
|
|
|
|
};
|
|
|
|
|
2019-07-18 22:29:07 +02:00
|
|
|
exports.initialize = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const unread_msgs = page_params.unread_msgs;
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
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);
|
2020-02-01 03:13:57 +01:00
|
|
|
_.each(unread_msgs.mentions, message_id => exports.unread_mentions_counter.add(message_id));
|
2017-08-01 14:50:40 +02:00
|
|
|
|
|
|
|
_.each(unread_msgs.huddles, function (obj) {
|
2020-02-01 03:17:09 +01:00
|
|
|
_.each(obj.unread_message_ids, message_id => unread_messages.add(message_id));
|
2017-08-01 14:50:40 +02:00
|
|
|
});
|
|
|
|
_.each(unread_msgs.pms, function (obj) {
|
2020-02-01 03:17:09 +01:00
|
|
|
_.each(obj.unread_message_ids, message_id => unread_messages.add(message_id));
|
2017-08-01 14:50:40 +02:00
|
|
|
});
|
|
|
|
_.each(unread_msgs.streams, function (obj) {
|
2020-02-01 03:17:09 +01:00
|
|
|
_.each(obj.unread_message_ids, message_id => unread_messages.add(message_id));
|
2017-08-01 14:50:40 +02:00
|
|
|
});
|
2020-02-01 03:17:09 +01:00
|
|
|
_.each(unread_msgs.mentions, message_id => unread_messages.add(message_id));
|
2017-08-01 14:50:40 +02:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.unread = exports;
|