unread: Replace key_to_bucket Dict/FoldDict with Map.

Fixes type confusion in unread_topic_counter, which uses stream IDs as
keys.

Since unread_topic_counter calls message_store.get now, update the
mocks so that message_store.get knows about our mocked messages.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2020-02-01 00:47:58 +00:00
committed by Tim Abbott
parent fd9557aa0c
commit d84646f091
3 changed files with 58 additions and 52 deletions

View File

@@ -67,7 +67,7 @@ const unread_messages = make_id_set();
function make_bucketer(options) {
const self = {};
const key_to_bucket = options.fold_case ? new FoldDict() : new Dict();
const key_to_bucket = new Map();
const reverse_lookup = new Dict();
self.clear = function () {
@@ -80,10 +80,10 @@ function make_bucketer(options) {
const item_id = opts.item_id;
const add_callback = opts.add_callback;
let bucket = key_to_bucket.get(bucket_key);
let bucket = self.get_bucket(bucket_key);
if (!bucket) {
bucket = options.make_bucket();
key_to_bucket.set(bucket_key, bucket);
key_to_bucket.set(options.fold_case ? bucket_key.toLowerCase() : bucket_key, bucket);
}
if (add_callback) {
add_callback(bucket, item_id);
@@ -102,15 +102,15 @@ function make_bucketer(options) {
};
self.get_bucket = function (bucket_key) {
return key_to_bucket.get(bucket_key);
return key_to_bucket.get(options.fold_case ? bucket_key.toLowerCase() : bucket_key);
};
self.each = function (callback) {
key_to_bucket.each(callback);
key_to_bucket.forEach(callback);
};
self.keys = function () {
return key_to_bucket.keys();
return [...key_to_bucket.keys()];
};
return self;
@@ -330,10 +330,13 @@ exports.unread_topic_counter = (function () {
const result = _.map(topic_names, function (topic_name) {
const msgs = per_stream_bucketer.get_bucket(topic_name);
const message_id = msgs.max();
return {
pretty_name: topic_name,
message_id: msgs.max(),
// retrieve the topic with its original case, since topic_name
// has been lowercased
pretty_name: message_store.get(message_id).topic,
message_id,
};
});