ts: Migrate `stream_create_subscribers_data.js` to TypeScript.

While `user_id` from `page_params` is marked as `number | undefined`,
when fuctions in this file are called, `page_params.user_id` will
never be undefined. Verifying from all of callers of
`initialize_with_current_user`, this is because when user is able
to create and initialize a stream, this user is authenticated and
thus a `user_id` is always available. Therefore, adding an assertion
when undefined user id is detected to handle a confirmed error.
This commit is contained in:
Junyao Chen 2023-08-04 02:33:35 -04:00 committed by Tim Abbott
parent 80d0813dea
commit 6178a245b0
1 changed files with 14 additions and 10 deletions

View File

@ -1,21 +1,25 @@
import assert from "minimalistic-assert";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import type {User} from "./people";
let user_id_set; let user_id_set: Set<number>;
export function initialize_with_current_user() { export function initialize_with_current_user(): void {
const current_user_id = page_params.user_id; const current_user_id = page_params.user_id;
user_id_set = new Set(); user_id_set = new Set<number>();
assert(current_user_id !== undefined, "Current user's id is undefined");
user_id_set.add(current_user_id); user_id_set.add(current_user_id);
} }
export function sorted_user_ids() { export function sorted_user_ids(): number[] {
const users = people.get_users_from_ids([...user_id_set]); const users = people.get_users_from_ids([...user_id_set]);
people.sort_but_pin_current_user_on_top(users); people.sort_but_pin_current_user_on_top(users);
return users.map((user) => user.user_id); return users.map((user) => user.user_id);
} }
export function get_all_user_ids() { export function get_all_user_ids(): number[] {
const potential_subscribers = people.get_realm_users(); const potential_subscribers = people.get_realm_users();
const user_ids = potential_subscribers.map((user) => user.user_id); const user_ids = potential_subscribers.map((user) => user.user_id);
// sort for determinism // sort for determinism
@ -23,21 +27,21 @@ export function get_all_user_ids() {
return user_ids; return user_ids;
} }
export function get_principals() { export function get_principals(): number[] {
// Return list of user ids which were selected by user. // Return list of user ids which were selected by user.
return [...user_id_set]; return [...user_id_set];
} }
export function get_potential_subscribers() { export function get_potential_subscribers(): User[] {
const potential_subscribers = people.get_realm_users(); const potential_subscribers = people.get_realm_users();
return potential_subscribers.filter((user) => !user_id_set.has(user.user_id)); return potential_subscribers.filter((user) => !user_id_set.has(user.user_id));
} }
export function must_be_subscribed(user_id) { export function must_be_subscribed(user_id: number): boolean {
return !page_params.is_admin && user_id === page_params.user_id; return !page_params.is_admin && user_id === page_params.user_id;
} }
export function add_user_ids(user_ids) { export function add_user_ids(user_ids: number[]): void {
for (const user_id of user_ids) { for (const user_id of user_ids) {
if (!user_id_set.has(user_id)) { if (!user_id_set.has(user_id)) {
const user = people.maybe_get_user_by_id(user_id); const user = people.maybe_get_user_by_id(user_id);
@ -48,7 +52,7 @@ export function add_user_ids(user_ids) {
} }
} }
export function remove_user_ids(user_ids) { export function remove_user_ids(user_ids: number[]): void {
for (const user_id of user_ids) { for (const user_id of user_ids) {
user_id_set.delete(user_id); user_id_set.delete(user_id);
} }