unread: Fix implicit use of any.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-05-04 10:31:19 -07:00 committed by Tim Abbott
parent 6ed50d1c80
commit b054050ee8
2 changed files with 59 additions and 48 deletions

View File

@ -448,13 +448,11 @@ function reset_data() {
topics_dict = new Map();
streams_dict = new Map();
const include_per_bucket_max_msg_id = true;
const unread_dms = unread.get_unread_pm(include_per_bucket_max_msg_id);
const unread_dms = unread.get_unread_pm();
const unread_dms_count = unread_dms.total_count;
const unread_dms_dict = unread_dms.pm_dict;
const include_per_topic_max_msg_id = true;
const unread_stream_message = unread.get_unread_topics(include_per_topic_max_msg_id);
const unread_stream_message = unread.get_unread_topics();
const unread_stream_msg_count = unread_stream_message.stream_unread_messages;
const unread_streams_dict = unread_stream_message.topic_counts;
@ -1023,13 +1021,11 @@ export function update() {
return;
}
const include_per_bucket_max_msg_id = true;
const unread_dms = unread.get_unread_pm(include_per_bucket_max_msg_id);
const unread_dms = unread.get_unread_pm();
const unread_dms_count = unread_dms.total_count;
const unread_dms_dict = unread_dms.pm_dict;
const include_per_topic_max_msg_id = true;
const unread_stream_message = unread.get_unread_topics(include_per_topic_max_msg_id);
const unread_stream_message = unread.get_unread_topics();
const unread_streams_dict = unread_stream_message.topic_counts;
let has_dms_post_filter = false;

View File

@ -42,7 +42,7 @@ const unread_messages = new Set<number>();
//
// Functionally a cache; see clear_and_populate_unread_mention_topics
// for how we can refresh it efficiently.
export const unread_mention_topics = new Map();
export const unread_mention_topics = new Map<string, Set<number>>();
export type StreamCountInfo = {
unmuted_count: number;
@ -56,6 +56,11 @@ type DirectMessageCountInfo = {
pm_dict: Map<string, number>;
};
type DirectMessageCountInfoWithLatestMsgId = {
total_count: number;
pm_dict: Map<string, {count: number; latest_msg_id: number}>;
};
class UnreadDirectMessageCounter {
bucketer = new Map<string, Set<number>>();
// Maps direct message id to the user id string that's the key for bucketer
@ -107,20 +112,30 @@ class UnreadDirectMessageCounter {
this.reverse_lookup.delete(message_id);
}
get_counts(include_latest_msg_id = false): DirectMessageCountInfo {
const pm_dict = new Map(); // Hash by user_ids_string -> count Optional[, max_id]
get_counts(): DirectMessageCountInfo {
const pm_dict = new Map<string, number>(); // Hash by user_ids_string -> count Optional[, max_id]
let total_count = 0;
for (const [user_ids_string, id_set] of this.bucketer) {
const count = id_set.size;
if (include_latest_msg_id) {
const latest_msg_id = Math.max(...id_set);
pm_dict.set(user_ids_string, {
count,
latest_msg_id,
});
} else {
pm_dict.set(user_ids_string, count);
}
pm_dict.set(user_ids_string, count);
total_count += count;
}
return {
total_count,
pm_dict,
};
}
get_counts_with_latest_msg_id(): DirectMessageCountInfoWithLatestMsgId {
const pm_dict = new Map<string, {count: number; latest_msg_id: number}>(); // Hash by user_ids_string -> count Optional[, max_id]
let total_count = 0;
for (const [user_ids_string, id_set] of this.bucketer) {
const count = id_set.size;
const latest_msg_id = Math.max(...id_set);
pm_dict.set(user_ids_string, {
count,
latest_msg_id,
});
total_count += count;
}
return {
@ -247,9 +262,12 @@ class UnreadTopicCounter {
this.reverse_lookup.delete(message_id);
}
get_counts_per_topic(include_per_topic_latest_msg_id = false): UnreadTopicCounts {
get_counts_per_topic(): UnreadTopicCounts {
let stream_unread_messages = 0;
const topic_counts_by_stream_id = new Map(); // hash by stream_id -> count
const topic_counts_by_stream_id = new Map<
number,
Map<string, {topic_count: number; latest_msg_id: number}>
>(); // hash by stream_id -> count
for (const [stream_id, per_stream_bucketer] of this.bucketer) {
// We track unread counts for streams that may be currently
// unsubscribed. Since users may re-subscribe, we don't
@ -260,19 +278,15 @@ class UnreadTopicCounter {
continue;
}
const topic_unread = new Map();
const topic_unread = new Map<string, {topic_count: number; latest_msg_id: number}>();
let stream_count = 0;
for (const [topic, msgs] of per_stream_bucketer) {
const topic_count = msgs.size;
if (include_per_topic_latest_msg_id) {
const latest_msg_id = Math.max(...msgs);
topic_unread.set(topic, {
topic_count,
latest_msg_id,
});
} else {
topic_unread.set(topic, topic_count);
}
const latest_msg_id = Math.max(...msgs);
topic_unread.set(topic, {
topic_count,
latest_msg_id,
});
stream_count += topic_count;
}
@ -289,7 +303,7 @@ class UnreadTopicCounter {
get_counts(): UnreadStreamCounts {
let stream_unread_messages = 0;
let followed_topic_unread_messages = 0;
const stream_counts_by_id = new Map(); // hash by stream_id -> count
const stream_counts_by_id = new Map<number, StreamCountInfo>(); // hash by stream_id -> count
for (const stream_id of this.bucketer.keys()) {
// We track unread counts for streams that may be currently
// unsubscribed. Since users may re-subscribe, we don't
@ -303,9 +317,10 @@ class UnreadTopicCounter {
// get_stream_count_info calculates both the number of
// unmuted unread as well as the number of muted
// unreads.
stream_counts_by_id.set(stream_id, this.get_stream_count_info(stream_id));
stream_unread_messages += stream_counts_by_id.get(stream_id).unmuted_count;
followed_topic_unread_messages += stream_counts_by_id.get(stream_id).followed_count;
const stream_count_info = this.get_stream_count_info(stream_id);
stream_counts_by_id.set(stream_id, stream_count_info);
stream_unread_messages += stream_count_info.unmuted_count;
followed_topic_unread_messages += stream_count_info.followed_count;
}
return {
@ -561,8 +576,9 @@ function add_message_to_unread_mention_topics(message_id: number): void {
return;
}
const topic_key = recent_view_util.get_topic_key(message.stream_id, message.topic);
if (unread_mention_topics.has(topic_key)) {
unread_mention_topics.get(topic_key).add(message_id);
const topic_message_ids = unread_mention_topics.get(topic_key);
if (topic_message_ids !== undefined) {
topic_message_ids.add(message_id);
} else {
unread_mention_topics.set(topic_key, new Set([message_id]));
}
@ -577,9 +593,7 @@ function remove_message_from_unread_mention_topics(message_id: number): void {
}
const {stream_id, topic} = stream_topic;
const topic_key = recent_view_util.get_topic_key(stream_id, topic);
if (unread_mention_topics.has(topic_key)) {
unread_mention_topics.get(topic_key).delete(message_id);
}
unread_mention_topics.get(topic_key)?.delete(message_id);
}
export function clear_and_populate_unread_mention_topics(): void {
@ -607,8 +621,9 @@ export function clear_and_populate_unread_mention_topics(): void {
const {stream_id, topic} = stream_topic;
const topic_key = recent_view_util.get_topic_key(stream_id, topic);
if (unread_mention_topics.has(topic_key)) {
unread_mention_topics.get(topic_key).add(message_id);
const topic_message_ids = unread_mention_topics.get(topic_key);
if (topic_message_ids !== undefined) {
topic_message_ids.add(message_id);
} else {
unread_mention_topics.set(topic_key, new Set([message_id]));
}
@ -836,12 +851,12 @@ export function declare_bankruptcy(): void {
unread_mention_topics.clear();
}
export function get_unread_pm(include_per_bucket_latest_msg_id = false): DirectMessageCountInfo {
return unread_direct_message_counter.get_counts(include_per_bucket_latest_msg_id);
export function get_unread_pm(): DirectMessageCountInfoWithLatestMsgId {
return unread_direct_message_counter.get_counts_with_latest_msg_id();
}
export function get_unread_topics(include_per_topic_latest_msg_id = false): UnreadTopicCounts {
return unread_topic_counter.get_counts_per_topic(include_per_topic_latest_msg_id);
export function get_unread_topics(): UnreadTopicCounts {
return unread_topic_counter.get_counts_per_topic();
}
export type FullUnreadCountsData = {
@ -955,7 +970,7 @@ export function topic_has_any_unread_mentions(stream_id: number, topic: string):
// Because this function is called in a loop for every displayed
// Recent Conversations row, it's important for it to run in O(1) time.
const topic_key = stream_id + ":" + topic.toLowerCase();
return unread_mention_topics.get(topic_key) && unread_mention_topics.get(topic_key).size > 0;
return (unread_mention_topics.get(topic_key)?.size ?? 0) > 0;
}
export function topic_has_any_unread(stream_id: number, topic: string): boolean {