muted_users: Convert muted_users to typescript.

muted_users.js is another dependency of people.js to be converted into
typescript. Note that for filter_muted_users, we avoid typing persons
with a type that might get added to people.ts in the future, as it
otherwise might introduce cyclic dependency.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-12-27 20:49:03 +08:00 committed by Tim Abbott
parent bec8e48616
commit 6d9aba8aff
2 changed files with 21 additions and 10 deletions

View File

@ -1,23 +1,33 @@
import {page_params} from "./page_params";
import * as timerender from "./timerender"; import * as timerender from "./timerender";
import {get_time_from_date_muted} from "./util"; import {get_time_from_date_muted} from "./util";
const muted_users = new Map(); type RawMutedUser = {
id: number;
timestamp: number;
};
export function add_muted_user(user_id, date_muted) { type MutedUser = {
id: number;
date_muted: number;
date_muted_str: string;
};
const muted_users: Map<number, number> = new Map();
export function add_muted_user(user_id: number, date_muted?: number): void {
const time = get_time_from_date_muted(date_muted); const time = get_time_from_date_muted(date_muted);
if (user_id) { if (user_id) {
muted_users.set(user_id, time); muted_users.set(user_id, time);
} }
} }
export function remove_muted_user(user_id) { export function remove_muted_user(user_id: number): void {
if (user_id) { if (user_id) {
muted_users.delete(user_id); muted_users.delete(user_id);
} }
} }
export function is_user_muted(user_id) { export function is_user_muted(user_id: number): boolean {
if (user_id === undefined) { if (user_id === undefined) {
return false; return false;
} }
@ -25,19 +35,19 @@ export function is_user_muted(user_id) {
return muted_users.has(user_id); return muted_users.has(user_id);
} }
export function filter_muted_user_ids(user_ids) { export function filter_muted_user_ids(user_ids: number[]): number[] {
// Returns a copy of the user ID list, after removing muted user IDs. // Returns a copy of the user ID list, after removing muted user IDs.
const base_user_ids = [...user_ids]; const base_user_ids = [...user_ids];
return base_user_ids.filter((user_id) => !is_user_muted(user_id)); return base_user_ids.filter((user_id) => !is_user_muted(user_id));
} }
export function filter_muted_users(persons) { export function filter_muted_users<T extends {user_id: number}>(persons: T[]): T[] {
// Returns a copy of the people list, after removing muted users. // Returns a copy of the people list, after removing muted users.
const base_users = [...persons]; const base_users = [...persons];
return base_users.filter((person) => !is_user_muted(person.user_id)); return base_users.filter((person) => !is_user_muted(person.user_id));
} }
export function get_muted_users() { export function get_muted_users(): MutedUser[] {
const users = []; const users = [];
for (const [id, date_muted] of muted_users) { for (const [id, date_muted] of muted_users) {
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str; const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
@ -50,7 +60,7 @@ export function get_muted_users() {
return users; return users;
} }
export function set_muted_users(list) { export function set_muted_users(list: RawMutedUser[]): void {
muted_users.clear(); muted_users.clear();
for (const user of list) { for (const user of list) {
@ -60,6 +70,6 @@ export function set_muted_users(list) {
} }
} }
export function initialize(params) { export function initialize(params: {muted_users: RawMutedUser[]}): void {
set_muted_users(params.muted_users); set_muted_users(params.muted_users);
} }

View File

@ -36,6 +36,7 @@ export const page_params: {
server_web_public_streams_enabled: boolean; server_web_public_streams_enabled: boolean;
translation_data: Record<string, string>; translation_data: Record<string, string>;
zulip_plan_is_not_limited: boolean; zulip_plan_is_not_limited: boolean;
muted_users: {id: number; timestamp: number}[];
} = $("#page-params").remove().data("params"); } = $("#page-params").remove().data("params");
const t2 = performance.now(); const t2 = performance.now();
export const page_params_parse_time = t2 - t1; export const page_params_parse_time = t2 - t1;