2023-08-15 09:06:16 +02:00
|
|
|
import assert from "minimalistic-assert";
|
|
|
|
|
2022-08-21 17:59:39 +02:00
|
|
|
import {page_params} from "./page_params";
|
|
|
|
import * as people from "./people";
|
2023-08-15 09:06:16 +02:00
|
|
|
import type {User} from "./people";
|
2022-08-21 17:59:39 +02:00
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
let user_id_set: Set<number>;
|
2022-08-21 17:59:39 +02:00
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function initialize_with_current_user(): void {
|
2022-08-21 17:59:39 +02:00
|
|
|
const current_user_id = page_params.user_id;
|
2023-08-15 09:06:16 +02:00
|
|
|
user_id_set = new Set<number>();
|
|
|
|
assert(current_user_id !== undefined, "Current user's id is undefined");
|
2022-08-21 17:59:39 +02:00
|
|
|
user_id_set.add(current_user_id);
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function sorted_user_ids(): number[] {
|
2023-03-02 01:58:25 +01:00
|
|
|
const users = people.get_users_from_ids([...user_id_set]);
|
2022-08-21 17:59:39 +02:00
|
|
|
people.sort_but_pin_current_user_on_top(users);
|
|
|
|
return users.map((user) => user.user_id);
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function get_all_user_ids(): number[] {
|
2022-08-21 17:59:39 +02:00
|
|
|
const potential_members = people.get_realm_users();
|
|
|
|
const user_ids = potential_members.map((user) => user.user_id);
|
|
|
|
// sort for determinism
|
|
|
|
user_ids.sort((a, b) => a - b);
|
|
|
|
return user_ids;
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function get_principals(): number[] {
|
2022-08-21 17:59:39 +02:00
|
|
|
// Return list of user ids which were selected by user.
|
2023-03-02 01:58:25 +01:00
|
|
|
return [...user_id_set];
|
2022-08-21 17:59:39 +02:00
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function get_potential_members(): User[] {
|
2022-08-21 17:59:39 +02:00
|
|
|
const potential_members = people.get_realm_users();
|
|
|
|
return potential_members.filter((user) => !user_id_set.has(user.user_id));
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function add_user_ids(user_ids: number[]): void {
|
2022-08-21 17:59:39 +02:00
|
|
|
for (const user_id of user_ids) {
|
|
|
|
if (!user_id_set.has(user_id)) {
|
2023-06-16 15:23:45 +02:00
|
|
|
const user = people.maybe_get_user_by_id(user_id);
|
2022-08-21 17:59:39 +02:00
|
|
|
if (user) {
|
|
|
|
user_id_set.add(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:06:16 +02:00
|
|
|
export function remove_user_ids(user_ids: number[]): void {
|
2022-08-21 17:59:39 +02:00
|
|
|
for (const user_id of user_ids) {
|
|
|
|
user_id_set.delete(user_id);
|
|
|
|
}
|
|
|
|
}
|