Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

peer_data: Return null if fetching subscribers fails.

Evy Kassirer2be8277a689a
diff --git a/web/src/buddy_list.ts b/web/src/buddy_list.ts
index b87d0548ea2..cc50d117a00 100644
--- a/web/src/buddy_list.ts
+++ b/web/src/buddy_list.ts
@@ -172,7 +172,7 @@ export class BuddyList extends BuddyListConf {
current_filter: Filter | undefined | "unset" = "unset";
initialize_tooltips(): void {
- const non_participant_users_matching_view_count = async (): Promise<number> =>
+ const non_participant_users_matching_view_count = async (): Promise<number | null> =>
await this.non_participant_users_matching_view_count();
const total_human_subscribers_count = (): number =>
this.render_data.total_human_subscribers_count;
@@ -241,6 +241,11 @@ export class BuddyList extends BuddyListConf {
}
const users_matching_view_count =
await non_participant_users_matching_view_count();
+ // This means a request failed and we don't know the count. So we can
+ // leave the text as the total subscriber count.
+ if (users_matching_view_count === null) {
+ return;
+ }
tooltip_text = $t(
{
defaultMessage:
@@ -253,6 +258,7 @@ export class BuddyList extends BuddyListConf {
// to fetch subscriber data.
const users_matching_view_count =
await non_participant_users_matching_view_count();
+ assert(users_matching_view_count !== null);
tooltip_text = $t(
{
defaultMessage:
@@ -285,7 +291,7 @@ export class BuddyList extends BuddyListConf {
);
}
- async non_participant_users_matching_view_count(): Promise<number> {
+ async non_participant_users_matching_view_count(): Promise<number | null> {
const {current_sub, get_all_participant_ids} = this.render_data;
// We don't show "participants" for DMs, we just show the
// "in this narrow" section (i.e. everyone in the conversation).
@@ -300,6 +306,10 @@ export class BuddyList extends BuddyListConf {
current_sub.stream_id,
user_id,
);
+ // This means a request failed and we don't know the count.
+ if (is_subscribed === null) {
+ return null;
+ }
if (is_subscribed && !people.is_valid_bot_user(user_id)) {
subscribed_human_participant_ids.push(user_id);
}
@@ -410,8 +420,14 @@ export class BuddyList extends BuddyListConf {
const {current_sub, get_all_participant_ids} = this.render_data;
$("#buddy-list-users-matching-view .empty-list-message").remove();
- if ((await this.non_participant_users_matching_view_count()) > 0) {
- // There are more subscribers, so we don't need an empty list message.
+ const non_participant_users_matching_view_count =
+ await this.non_participant_users_matching_view_count();
+ if (
+ non_participant_users_matching_view_count === null ||
+ non_participant_users_matching_view_count > 0
+ ) {
+ // There are more subscribers (or we don't know how many subscribers there are)
+ // so we don't need an empty list message.
return;
}
// After the `await`, we might have changed to a different channel view.
@@ -447,6 +463,10 @@ export class BuddyList extends BuddyListConf {
const formatted_participants_count = get_formatted_user_count(all_participant_ids.size);
const non_participant_users_matching_view_count =
await this.non_participant_users_matching_view_count();
+ // This means a request failed and we can't calculate the counts.
+ if (non_participant_users_matching_view_count === null) {
+ return;
+ }
const formatted_matching_users_count = get_formatted_user_count(
non_participant_users_matching_view_count,
);
@@ -689,9 +709,15 @@ export class BuddyList extends BuddyListConf {
async render_view_user_list_links(): Promise<void> {
const {current_sub, other_users_count} = this.render_data;
+ const non_participant_users_matching_view_count =
+ await this.non_participant_users_matching_view_count();
+ // This means a request failed and we can't calculate the counts. We choose
+ // not to render the links in that case.
+ if (non_participant_users_matching_view_count === null) {
+ return;
+ }
const has_inactive_users_matching_view =
- (await this.non_participant_users_matching_view_count()) >
- this.users_matching_view_ids.length;
+ non_participant_users_matching_view_count > this.users_matching_view_ids.length;
const has_inactive_other_users = other_users_count > this.other_user_ids.length;
// After the `await`, we might have changed to a different channel view.
diff --git a/web/src/peer_data.ts b/web/src/peer_data.ts
index 7296103e81a..38088da6966 100644
--- a/web/src/peer_data.ts
+++ b/web/src/peer_data.ts
@@ -18,7 +18,7 @@ export function has_full_subscriber_data(stream_id: number): boolean {
const pending_subscriber_requests = new Map<
number,
{
- subscribers_promise: Promise<LazySet>;
+ subscribers_promise: Promise<LazySet | null>;
pending_peer_events: {
type: "peer_add" | "peer_remove";
user_ids: number[];
@@ -35,7 +35,7 @@ const fetch_stream_subscribers_response_schema = z.object({
subscribers: z.array(z.number()),
});
-export async function maybe_fetch_stream_subscribers(stream_id: number): Promise<LazySet> {
+export async function maybe_fetch_stream_subscribers(stream_id: number): Promise<LazySet | null> {
if (pending_subscriber_requests.has(stream_id)) {
return pending_subscriber_requests.get(stream_id)!.subscribers_promise;
}
@@ -51,8 +51,7 @@ export async function maybe_fetch_stream_subscribers(stream_id: number): Promise
stream_id,
});
pending_subscriber_requests.delete(stream_id);
- // Fall back to what we already have.
- return get_loaded_subscriber_subset(stream_id);
+ return null;
}
set_subscribers(stream_id, subscribers);
@@ -94,12 +93,16 @@ function get_loaded_subscriber_subset(stream_id: number): LazySet {
return subscribers;
}
-async function get_full_subscriber_set(stream_id: number): Promise<LazySet> {
+async function get_full_subscriber_set(stream_id: number): Promise<LazySet | null> {
assert(!page_params.is_spectator);
// This function parallels `get_loaded_subscriber_subset` but ensures we include all
// subscribers, possibly fetching that data from the server.
if (!fetched_stream_ids.has(stream_id) && sub_store.get(stream_id)) {
const fetched_subscribers = await maybe_fetch_stream_subscribers(stream_id);
+ // This means a request failed and we don't know who the subscribers are.
+ if (fetched_subscribers === null) {
+ return null;
+ }
stream_subscribers.set(stream_id, fetched_subscribers);
}
return get_loaded_subscriber_subset(stream_id);
@@ -263,8 +266,17 @@ export function is_user_subscribed(stream_id: number, user_id: number): boolean
export async function maybe_fetch_is_user_subscribed(
stream_id: number,
user_id: number,
-): Promise<boolean> {
+): Promise<boolean | null> {
const subscribers = await get_full_subscriber_set(stream_id);
+ // This means the request failed. We will return `null` here if
+ // we can't determine if this user is subscribed or not.
+ if (subscribers === null) {
+ const subscribers = get_loaded_subscriber_subset(stream_id);
+ if (subscribers.has(user_id)) {
+ return true;
+ }
+ return null;
+ }
return subscribers.has(user_id);
}
diff --git a/web/tests/peer_data.test.cjs b/web/tests/peer_data.test.cjs
index 9b1d47a70da..9ba832cd830 100644
--- a/web/tests/peer_data.test.cjs
+++ b/web/tests/peer_data.test.cjs
@@ -295,7 +295,14 @@ test("maybe_fetch_stream_subscribers", async () => {
};
peer_data.clear_for_testing();
blueslip.expect("error", "Failure fetching channel subscribers");
- peer_data.maybe_fetch_stream_subscribers(india.stream_id);
+ assert.equal(await peer_data.maybe_fetch_is_user_subscribed(india.stream_id, 5), null);
+ // If we know they're subscribed, we return `true` even though we don't have complete
+ // data.
+ peer_data.bulk_add_subscribers({
+ stream_ids: [india.stream_id],
+ user_ids: [5],
+ });
+ assert.equal(await peer_data.maybe_fetch_is_user_subscribed(india.stream_id, 5), true);
});
test("get_subscriber_count", () => {