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.

people: Handle inaccessible users not present in user fetch.

Aman Agrawal1eeb5b106652
diff --git a/web/src/people.ts b/web/src/people.ts
index 5f689689629..9c6f1c0752b 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -2102,7 +2102,7 @@ function start_fetch_for_requested_users(): void {
fetch_request_with_retry();
}
-export async function fetch_users_from_ids_internal(user_ids: number[]): Promise<unknown> {
+export let fetch_users_from_ids_internal = async (user_ids: number[]): Promise<unknown> => {
// NOTE: NEVER USE THIS FUNCTION DIRECTLY.
// Call get_or_fetch_users_from_ids instead.
const unknown_user_ids = new Set(
@@ -2149,11 +2149,34 @@ export async function fetch_users_from_ids_internal(user_ids: number[]): Promise
}
}, 0);
return promise;
+};
+
+export function rewire_fetch_users_from_ids_internal(
+ value: typeof fetch_users_from_ids_internal,
+): void {
+ fetch_users_from_ids_internal = value;
}
export async function get_or_fetch_users_from_ids(user_ids: number[]): Promise<User[]> {
await fetch_users_from_ids_internal(user_ids);
- return get_users_from_ids(user_ids);
+ // In case `valid_user_ids` got updated while we were fetching,
+ // re-filter the user_ids to only return valid ones.
+ const user_ids_valid = valid_user_ids.intersection(new Set(user_ids));
+ // Server doesn't return data for inaccessible users, so we need to
+ // make fake user objects for them if needed.
+ const ignore_missing = !settings_data.user_can_access_all_other_users();
+ const users: User[] = [];
+ for (const user_id of user_ids_valid) {
+ const person = maybe_get_user_by_id(user_id, ignore_missing);
+ if (person) {
+ users.push(person);
+ } else {
+ // maybe_get_user_by_id will throw an error if ignore_missing is false.
+ // User is inaccessible, create a fake user object.
+ users.push(add_inaccessible_user(user_id));
+ }
+ }
+ return users;
}
export function fetch_users_from_server(opts: FetchUserDataParams): void {
@@ -2257,14 +2280,8 @@ export async function initialize(
user_ids_to_fetch.delete(person.user_id);
}
- // This check is a hack to avoid us from showing inaccessible users
- // as `Unknown users` to the guest user.
- // TODO: Find a way to identify inaccessible users and remove them
- // from this fetch.
- if (settings_data.user_can_access_all_other_users()) {
- // Fetch all the missing users. This code path is temporary: We
- // plan to move to a model where the web app expects to have an
- // incomplete users dataset in large organizations.
- await get_or_fetch_users_from_ids([...user_ids_to_fetch]);
- }
+ // Fetch all the missing users. This code path is temporary: We
+ // plan to move to a model where the web app expects to have an
+ // incomplete users dataset in large organizations.
+ await get_or_fetch_users_from_ids([...user_ids_to_fetch]);
}
diff --git a/web/tests/people.test.cjs b/web/tests/people.test.cjs
index 2b55a1c7482..2aa7d82df64 100644
--- a/web/tests/people.test.cjs
+++ b/web/tests/people.test.cjs
@@ -2041,3 +2041,14 @@ run_test("fetch_users corner case", async ({override, override_rewire}) => {
assert.equal(user2.full_name, "Alice");
assert.equal(user3.full_name, "Third user");
});
+
+run_test("fetch inaccessible user", async ({override, override_rewire}) => {
+ initialize();
+ override_rewire(people, "fetch_users_from_ids_internal", noop);
+ people.add_valid_user_id(1);
+
+ override(settings_data, "user_can_access_all_other_users", () => false);
+ const [inaccessible_user] = await people.get_or_fetch_users_from_ids([1]);
+ assert.equal(inaccessible_user.user_id, 1);
+ assert.equal(inaccessible_user.is_inaccessible_user, true);
+});