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: Handle a bad request when fetching subscribers with retry.

Shubham Padia6aff9eb20084
diff --git a/web/src/add_group_members_pill.ts b/web/src/add_group_members_pill.ts
index 2a4ca998703..76a1c424ed7 100644
--- a/web/src/add_group_members_pill.ts
+++ b/web/src/add_group_members_pill.ts
@@ -7,6 +7,7 @@ import * as loading from "./loading.ts";
import * as people from "./people.ts";
import type {User} from "./people.ts";
import * as stream_pill from "./stream_pill.ts";
+import type {UserIdsFetchResult} from "./stream_pill.ts";
import type {CombinedPill, CombinedPillContainer} from "./typeahead_helper.ts";
import * as user_group_components from "./user_group_components.ts";
import * as user_group_pill from "./user_group_pill.ts";
@@ -14,12 +15,17 @@ import * as user_groups from "./user_groups.ts";
import type {UserGroup} from "./user_groups.ts";
import * as user_pill from "./user_pill.ts";
-async function get_pill_user_ids(pill_widget: CombinedPillContainer): Promise<number[]> {
- const stream_user_ids = await stream_pill.get_user_ids(pill_widget);
+async function get_pill_user_ids(
+ pill_widget: CombinedPillContainer,
+): Promise<UserIdsFetchResult> {
+ const stream_result = await stream_pill.get_user_ids(pill_widget);
+ if (stream_result.status === "failed") {
+ return stream_result;
+ }
// Read the user pills only after waiting for subscriber data,
// since pills may have been removed while we were waiting.
const user_ids = user_pill.get_user_ids(pill_widget);
- return [...user_ids, ...stream_user_ids];
+ return {status: "success", user_ids: [...user_ids, ...stream_result.user_ids]};
}
function get_pill_group_ids(pill_widget: CombinedPillContainer): number[] {
@@ -124,11 +130,13 @@ export function create({
loading.make_indicator($loading_spinner, {
height: 28, // 2em at 14px / 1em
});
- const user_ids = await get_pill_user_ids(pill_widget);
+ const result = await get_pill_user_ids(pill_widget);
if (fetch_id !== latest_fetch_id) {
return;
}
- onPillCreateAction(user_ids, get_pill_group_ids(pill_widget));
+ if (result.status === "success") {
+ onPillCreateAction(result.user_ids, get_pill_group_ids(pill_widget));
+ }
loading.destroy_indicator($loading_spinner);
})();
});
@@ -137,8 +145,11 @@ export function create({
if (onPillRemoveAction) {
pill_widget.onPillRemove(() => {
void (async () => {
- const user_ids = await get_pill_user_ids(pill_widget);
- onPillRemoveAction(user_ids, get_pill_group_ids(pill_widget));
+ const result = await get_pill_user_ids(pill_widget);
+ if (result.status === "failed") {
+ return;
+ }
+ onPillRemoveAction(result.user_ids, get_pill_group_ids(pill_widget));
})();
});
}
@@ -207,7 +218,7 @@ export function set_up_handlers({
loading.make_indicator($loading_spinner, {
height: 28, // 2em at 14px / 1em
});
- const pill_user_ids = await get_pill_user_ids(pill_widget);
+ const result = await get_pill_user_ids(pill_widget);
// If we're no longer in the same view after fetching
// subscriber data, don't update the UI. We don't need
// to destroy the loading spinner because the tab re-renders
@@ -217,8 +228,11 @@ export function set_up_handlers({
return;
}
loading.destroy_indicator($loading_spinner);
+ if (result.status === "failed") {
+ return;
+ }
const pill_group_ids = get_pill_group_ids(pill_widget);
- action({pill_user_ids, pill_group_ids});
+ action({pill_user_ids: result.user_ids, pill_group_ids});
})();
}
diff --git a/web/src/add_subscribers_pill.ts b/web/src/add_subscribers_pill.ts
index e413484820c..fb75f5436b7 100644
--- a/web/src/add_subscribers_pill.ts
+++ b/web/src/add_subscribers_pill.ts
@@ -7,6 +7,7 @@ import * as loading from "./loading.ts";
import type {User} from "./people.ts";
import * as pill_typeahead from "./pill_typeahead.ts";
import * as stream_pill from "./stream_pill.ts";
+import type {UserIdsFetchResult} from "./stream_pill.ts";
import type {CombinedPill, CombinedPillContainer} from "./typeahead_helper.ts";
import * as user_group_pill from "./user_group_pill.ts";
import * as user_groups from "./user_groups.ts";
@@ -180,11 +181,13 @@ export function create({
loading.make_indicator($loading_spinner, {
height: 28, // 2em at 14px / 1em
});
- const user_ids = await get_pill_user_ids(pill_widget);
+ const result = await get_pill_user_ids(pill_widget);
if (fetch_id !== latest_fetch_id) {
return;
}
- onPillCreateAction(user_ids);
+ if (result.status === "success") {
+ onPillCreateAction(result.user_ids);
+ }
loading.destroy_indicator($loading_spinner);
})();
});
@@ -193,8 +196,11 @@ export function create({
if (onPillRemoveAction) {
pill_widget.onPillRemove(() => {
void (async () => {
- const user_ids = await get_pill_user_ids(pill_widget);
- onPillRemoveAction(user_ids);
+ const result = await get_pill_user_ids(pill_widget);
+ if (result.status === "failed") {
+ return;
+ }
+ onPillRemoveAction(result.user_ids);
})();
});
}
@@ -250,13 +256,21 @@ export function append_user_group_from_name(
user_group_pill.append_user_group(user_group, pill_widget);
}
-export async function get_pill_user_ids(pill_widget: CombinedPillContainer): Promise<number[]> {
- const stream_user_ids = await stream_pill.get_user_ids(pill_widget);
+export async function get_pill_user_ids(
+ pill_widget: CombinedPillContainer,
+): Promise<UserIdsFetchResult> {
+ const stream_result = await stream_pill.get_user_ids(pill_widget);
+ if (stream_result.status === "failed") {
+ return stream_result;
+ }
// Read the other pills only after waiting for subscriber data,
// since pills may have been removed while we were waiting.
const user_ids = user_pill.get_user_ids(pill_widget);
const group_user_ids = user_group_pill.get_user_ids(pill_widget);
- return [...user_ids, ...stream_user_ids, ...group_user_ids];
+ return {
+ status: "success",
+ user_ids: [...user_ids, ...stream_result.user_ids, ...group_user_ids],
+ };
}
export function set_up_handlers({
@@ -308,7 +322,7 @@ export function set_up_handlers({
loading.make_indicator($loading_spinner, {
height: 28, // 2em at 14px / 1em
});
- const pill_user_ids = await get_pill_user_ids(pill_widget);
+ const result = await get_pill_user_ids(pill_widget);
// If we're no longer in the same view after fetching
// subscriber data, don't update the UI. We don't need
// to destroy the loading spinner because the tab re-renders
@@ -318,7 +332,10 @@ export function set_up_handlers({
return;
}
loading.destroy_indicator($loading_spinner);
- action({pill_user_ids});
+ if (result.status === "failed") {
+ return;
+ }
+ action({pill_user_ids: result.user_ids});
})();
}
diff --git a/web/src/peer_data.ts b/web/src/peer_data.ts
index 439bd065753..065f2aeeab7 100644
--- a/web/src/peer_data.ts
+++ b/web/src/peer_data.ts
@@ -195,11 +195,6 @@ function get_loaded_subscriber_subset(stream_id: number): LazySet {
return subscribers;
}
-async function get_full_subscriber_set(stream_id: number, retry_on_failure: true): Promise<LazySet>;
-async function get_full_subscriber_set(
- stream_id: number,
- retry_on_failure: boolean,
-): Promise<LazySet | null>;
async function get_full_subscriber_set(
stream_id: number,
retry_on_failure: boolean,
@@ -388,8 +383,9 @@ export async function get_subscribers_with_possible_fetch(
// This function parallels `get_subscribers` but ensures we include all
// subscribers, possibly fetching that data from the server.
const subscribers = await get_full_subscriber_set(stream_id, retry_on_failure);
- // This means the request failed, which can only happen if `retry_on_failure`
- // is false.
+ // This means the request failed. Even with `retry_on_failure`, a
+ // bad request is not retried, since the channel was deleted or we
+ // lost access to it.
if (subscribers === null) {
return null;
}
@@ -523,7 +519,7 @@ export async function get_unique_subscriber_count_for_streams(
stream_ids: number[],
): Promise<number> {
const valid_subscribers = new LazySet([]);
- const promises: Record<number, Promise<LazySet>> = {};
+ const promises: Record<number, Promise<LazySet | null>> = {};
for (const stream_id of stream_ids) {
promises[stream_id] = get_full_subscriber_set(stream_id, true);
}
@@ -531,7 +527,7 @@ export async function get_unique_subscriber_count_for_streams(
for (const stream_id of stream_ids) {
// If it's `null`, that means a request failed and we don't know the
// full subscribers set, so just use whatever we have already.
- const subscribers = await promises[stream_id]!;
+ const subscribers = (await promises[stream_id]!) ?? get_loaded_subscriber_subset(stream_id);
for (const user_id of subscribers.keys()) {
if (!people.is_valid_bot_user(user_id)) {
diff --git a/web/src/stream_edit_subscribers.ts b/web/src/stream_edit_subscribers.ts
index c58614cbb41..20b47a40947 100644
--- a/web/src/stream_edit_subscribers.ts
+++ b/web/src/stream_edit_subscribers.ts
@@ -133,9 +133,12 @@ async function stream_edit_update_notification_choice($loading_spinner: JQuery):
loading.make_indicator($loading_spinner, {
height: 28, // 2em at 14px / 1em
});
- const pill_count = (await add_subscribers_pill.get_pill_user_ids(pill_widget)).length;
- update_notification_choice_checkbox(pill_count);
+ const result = await add_subscribers_pill.get_pill_user_ids(pill_widget);
loading.destroy_indicator($loading_spinner);
+ if (result.status === "failed") {
+ return;
+ }
+ update_notification_choice_checkbox(result.user_ids.length);
}
export function enable_subscriber_management({
@@ -189,10 +192,12 @@ async function render_subscriber_list_widget(
text: $t({defaultMessage: "Loading…"}),
});
- // Because we're using `retry_on_failure=true`, this will only return once it
- // succeeds, so we can't get `null`.
const user_ids = await peer_data.get_subscribers_with_possible_fetch(sub.stream_id, true);
- assert(user_ids !== null);
+ // The channel was deleted or we lost access to it while waiting.
+ if (user_ids === null) {
+ loading.destroy_indicator($(".subscriber-list-settings-loading"));
+ return;
+ }
// Make sure we're still editing this stream after waiting for subscriber data.
if (!hash_parser.is_editing_stream(sub.stream_id)) {
diff --git a/web/src/stream_pill.ts b/web/src/stream_pill.ts
index 672c6f152fc..029c7790bef 100644
--- a/web/src/stream_pill.ts
+++ b/web/src/stream_pill.ts
@@ -17,6 +17,9 @@ export type StreamPillWidget = InputPillContainer<StreamPill>;
export type StreamPillData = StreamSubscription & {type: "stream"};
+export type UserIdsFetchResult =
+ {status: "success"; user_ids: number[]} | {status: "failed"; failed_stream_id: number};
+
export function create_item_from_stream_name(
stream_name: string,
current_items: CombinedPill[],
@@ -57,9 +60,11 @@ export function get_stream_name_from_item(item: StreamPill): string {
return stream.name;
}
+// Returns the user ids the pills stand for, or the channel whose
+// subscribers could not be fetched.
export async function get_user_ids(
pill_widget: StreamPillWidget | CombinedPillContainer,
-): Promise<number[]> {
+): Promise<UserIdsFetchResult> {
const stream_ids = get_stream_ids(pill_widget);
const results = await Promise.all(
stream_ids.map(async (stream_id) =>
@@ -74,13 +79,18 @@ export async function get_user_ids(
// Double check if the stream pill has been removed from the pill
// widget while we were doing fetches.
if (current_stream_ids_in_widget.includes(stream_id)) {
+ // The fetch failed, so we don't know who the subscribers
+ // of this channel are.
+ if (subscribers === null) {
+ return {status: "failed", failed_stream_id: stream_id};
+ }
user_ids = [...user_ids, ...subscribers];
}
}
user_ids = [...new Set(user_ids)];
user_ids.sort((a, b) => a - b);
- return user_ids;
+ return {status: "success", user_ids};
}
export function get_display_value_from_item(item: StreamPill): string {
diff --git a/web/tests/peer_data.test.cjs b/web/tests/peer_data.test.cjs
index 5e5e018c5f0..42e57ed66b9 100644
--- a/web/tests/peer_data.test.cjs
+++ b/web/tests/peer_data.test.cjs
@@ -356,6 +356,14 @@ test("maybe_fetch_stream_subscribers", async () => {
);
blueslip.reset();
+ // A bad request is not retried, so we get null even with retry.
+ blueslip.expect("error", "Bad request to fetch channel subscribers.");
+ assert.deepEqual(
+ await peer_data.get_subscribers_with_possible_fetch(india.stream_id, true),
+ null,
+ );
+ blueslip.reset();
+
peer_data.clear_for_testing();
mock_channel_get(channel, (opts) => {
opts.error({readyState: 0});
@@ -619,6 +627,16 @@ test("get_unique_subscriber_count_for_streams", async () => {
const count = await peer_data.get_unique_subscriber_count_for_streams([stream_id]);
assert.equal(count, 2);
+
+ // If we only have partial data and the fetch fails, count what we have.
+ peer_data.clear_for_testing();
+ peer_data.set_subscribers(stream_id, [me.user_id, fred.user_id, bot_botson.user_id], false);
+ mock_channel_get(channel, (opts) => {
+ opts.error({status: 400, responseJSON: ""});
+ });
+ blueslip.expect("error", "Bad request to fetch channel subscribers.");
+ assert.equal(await peer_data.get_unique_subscriber_count_for_streams([stream_id]), 2);
+ blueslip.reset();
});
test("fetch_subscriptions_for_user", async () => {
diff --git a/web/tests/stream_pill.test.cjs b/web/tests/stream_pill.test.cjs
index d11acb3e1b3..f03000fa149 100644
--- a/web/tests/stream_pill.test.cjs
+++ b/web/tests/stream_pill.test.cjs
@@ -7,8 +7,12 @@ const {make_realm} = require("./lib/example_realm.cjs");
const {server_supported_permission_settings} = require("./lib/example_settings.cjs");
const {make_stream} = require("./lib/example_stream.cjs");
const {make_user} = require("./lib/example_user.cjs");
-const {zrequire} = require("./lib/namespace.cjs");
+const {mock_channel_get} = require("./lib/mock_channel.cjs");
+const {mock_esm, zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
+const blueslip = require("./lib/zblueslip.cjs");
+
+const channel = mock_esm("../src/channel");
const peer_data = zrequire("peer_data");
const people = zrequire("people");
@@ -77,6 +81,10 @@ const sweden_pill = {
type: "stream",
stream_id: sweden.stream_id,
};
+const germany_pill = {
+ type: "stream",
+ stream_id: germany.stream_id,
+};
const subs = [denmark, sweden, germany];
for (const sub of subs) {
@@ -132,8 +140,26 @@ run_test("get_user_ids", async () => {
const items = [denmark_pill, sweden_pill];
const widget = {items: () => items};
- const user_ids = await stream_pill.get_user_ids(widget);
- assert.deepEqual(user_ids, [1, 2, 3, 4, 5, 77]);
+ assert.deepEqual(await stream_pill.get_user_ids(widget), {
+ status: "success",
+ user_ids: [1, 2, 3, 4, 5, 77],
+ });
+});
+
+run_test("get_user_ids_fetch_failed", async () => {
+ // Germany's subscribers have not been fetched, and the fetch
+ // fails with a bad request, so we don't know who they are.
+ const widget = {items: () => [denmark_pill, germany_pill]};
+ mock_channel_get(channel, (opts) => {
+ assert.equal(opts.url, `/json/streams/${germany.stream_id}/members`);
+ opts.error({status: 400, responseJSON: ""});
+ });
+ blueslip.expect("error", "Bad request to fetch channel subscribers.");
+ assert.deepEqual(await stream_pill.get_user_ids(widget), {
+ status: "failed",
+ failed_stream_id: germany.stream_id,
+ });
+ blueslip.reset();
});
run_test("get_stream_ids", () => {