2021-06-27 21:38:26 +02:00
|
|
|
import * as timerender from "./timerender";
|
|
|
|
import {get_time_from_date_muted} from "./util";
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
type RawMutedUser = {
|
|
|
|
id: number;
|
|
|
|
timestamp: number;
|
|
|
|
};
|
2021-06-27 21:38:26 +02:00
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
type MutedUser = {
|
|
|
|
id: number;
|
|
|
|
date_muted: number;
|
|
|
|
date_muted_str: string;
|
|
|
|
};
|
|
|
|
|
2023-04-19 23:44:37 +02:00
|
|
|
const muted_users = new Map<number, number>();
|
2022-12-27 13:49:03 +01:00
|
|
|
|
|
|
|
export function add_muted_user(user_id: number, date_muted?: number): void {
|
2021-06-27 21:38:26 +02:00
|
|
|
const time = get_time_from_date_muted(date_muted);
|
|
|
|
if (user_id) {
|
|
|
|
muted_users.set(user_id, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function remove_muted_user(user_id: number): void {
|
2021-06-27 21:38:26 +02:00
|
|
|
if (user_id) {
|
|
|
|
muted_users.delete(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function is_user_muted(user_id: number): boolean {
|
2021-06-27 21:38:26 +02:00
|
|
|
if (user_id === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return muted_users.has(user_id);
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function filter_muted_user_ids(user_ids: number[]): number[] {
|
2021-06-27 21:38:26 +02:00
|
|
|
// Returns a copy of the user ID list, after removing muted user IDs.
|
|
|
|
const base_user_ids = [...user_ids];
|
|
|
|
return base_user_ids.filter((user_id) => !is_user_muted(user_id));
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function filter_muted_users<T extends {user_id: number}>(persons: T[]): T[] {
|
2021-06-27 21:38:26 +02:00
|
|
|
// Returns a copy of the people list, after removing muted users.
|
|
|
|
const base_users = [...persons];
|
|
|
|
return base_users.filter((person) => !is_user_muted(person.user_id));
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function get_muted_users(): MutedUser[] {
|
2021-06-27 21:38:26 +02:00
|
|
|
const users = [];
|
|
|
|
for (const [id, date_muted] of muted_users) {
|
|
|
|
const date_muted_str = timerender.render_now(new Date(date_muted)).time_str;
|
|
|
|
users.push({
|
|
|
|
id,
|
|
|
|
date_muted,
|
|
|
|
date_muted_str,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function set_muted_users(list: RawMutedUser[]): void {
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.clear();
|
|
|
|
|
|
|
|
for (const user of list) {
|
2023-04-20 00:44:42 +02:00
|
|
|
add_muted_user(user.id, user.timestamp);
|
2021-06-27 21:38:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 13:49:03 +01:00
|
|
|
export function initialize(params: {muted_users: RawMutedUser[]}): void {
|
2022-12-27 13:56:05 +01:00
|
|
|
set_muted_users(params.muted_users);
|
2021-06-27 21:38:26 +02:00
|
|
|
}
|