user_events: Convert module to TypeScript (#32658)

This commit is contained in:
Anders Kaseorg
2024-12-11 09:18:52 -08:00
committed by GitHub
parent 666ce4519d
commit d446b0d672
9 changed files with 70 additions and 41 deletions

View File

@@ -268,7 +268,7 @@ EXEMPT_FILES = make_set(
"web/src/upload_widget.ts",
"web/src/user_card_popover.ts",
"web/src/user_deactivation_ui.ts",
"web/src/user_events.js",
"web/src/user_events.ts",
"web/src/user_group_components.ts",
"web/src/user_group_create.ts",
"web/src/user_group_create_members.ts",

View File

@@ -78,7 +78,7 @@ export function update_user_full_name(user_id: number, full_name: string): void
rerender_messages_view_for_user(user_id);
}
export function update_avatar(user_id: number, avatar_url: string): void {
export function update_avatar(user_id: number, avatar_url: string | null): void {
message_store.update_small_avatar_url(user_id, avatar_url);
rerender_messages_view_for_user(user_id);
}

View File

@@ -169,7 +169,7 @@ export type Message = (
// `convert_raw_message_to_message_with_booleans`
flags?: string[];
small_avatar_url?: string; // Used in `message_avatar.hbs`
small_avatar_url?: string | null; // Used in `message_avatar.hbs`
status_emoji_info?: UserStatusEmojiInfo | undefined; // Used in `message_body.hbs`
local_edit_timestamp?: number; // Used for edited messages
@@ -305,7 +305,7 @@ export function update_sender_full_name(user_id: number, new_name: string): void
}
}
export function update_small_avatar_url(user_id: number, new_url: string): void {
export function update_small_avatar_url(user_id: number, new_url: string | null): void {
for (const msg of stored_messages.values()) {
if (msg.sender_id && msg.sender_id === user_id) {
msg.small_avatar_url = new_url;

View File

@@ -1664,7 +1664,7 @@ export function set_full_name(person_obj: User, new_full_name: string): void {
export function set_custom_profile_field_data(
user_id: number,
field: {id: number} & ProfileDatum,
field: {id: number; value: string | null; rendered_value?: string},
): void {
if (field.id === undefined) {
blueslip.error("Trying to set undefined field id");
@@ -1672,10 +1672,15 @@ export function set_custom_profile_field_data(
}
const person = get_by_user_id(user_id);
assert(person.profile_data !== undefined);
if (field.value === null) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete person.profile_data[field.id];
} else {
person.profile_data[field.id] = {
value: field.value,
rendered_value: field.rendered_value,
};
}
}
export function is_current_user(email?: string | null): boolean {
@@ -1704,7 +1709,7 @@ export function my_current_user_id(): number {
return my_user_id;
}
export function my_custom_profile_data(field_id: number): ProfileDatum | null | undefined {
export function my_custom_profile_data(field_id: number): ProfileDatum | undefined {
if (field_id === undefined) {
blueslip.error("Undefined field id");
return undefined;
@@ -1715,23 +1720,19 @@ export function my_custom_profile_data(field_id: number): ProfileDatum | null |
export function get_custom_profile_data(
user_id: number,
field_id: number,
): ProfileDatum | null | undefined {
): ProfileDatum | undefined {
const person = get_by_user_id(user_id);
const profile_data = person.profile_data;
if (profile_data === undefined) {
return null;
}
return profile_data[field_id];
return person.profile_data?.[field_id];
}
export function get_custom_fields_by_type(
user_id: number,
field_type: number,
): (ProfileDatum | undefined)[] | null {
): (ProfileDatum | undefined)[] | undefined {
const person = get_by_user_id(user_id);
const profile_data = person.profile_data;
if (profile_data === undefined) {
return null;
return undefined;
}
const filteredProfileData: (ProfileDatum | undefined)[] = [];
for (const field of realm.custom_profile_fields) {

View File

@@ -86,7 +86,7 @@ import * as theme from "./theme.ts";
import * as typing_events from "./typing_events.ts";
import * as unread_ops from "./unread_ops.ts";
import * as unread_ui from "./unread_ui.ts";
import * as user_events from "./user_events.js";
import * as user_events from "./user_events.ts";
import * as user_group_edit from "./user_group_edit.ts";
import * as user_groups from "./user_groups.ts";
import {user_settings} from "./user_settings.ts";

View File

@@ -94,7 +94,7 @@ function delete_profile_field(this: HTMLElement, e: JQuery.ClickEvent): void {
for (const user_id of active_user_ids) {
const user_profile_data = people.get_custom_profile_data(user_id, profile_field_id);
if (user_profile_data) {
if (user_profile_data !== undefined) {
users_using_deleting_profile_field += 1;
}
}
@@ -327,7 +327,7 @@ function show_modal_for_deleting_options(
let users_count_with_deleted_option_selected = 0;
for (const user_id of active_user_ids) {
const field_value = people.get_custom_profile_data(user_id, field.id);
if (field_value && deleted_values[field_value.value]) {
if (field_value !== undefined && deleted_values[field_value.value]) {
users_count_with_deleted_option_selected += 1;
}
}

View File

@@ -3,6 +3,7 @@
// dependencies that existed when this code was in people.js.
// (We should do bot updates here too.)
import $ from "jquery";
import assert from "minimalistic-assert";
import * as activity_ui from "./activity_ui.ts";
import * as blueslip from "./blueslip.ts";
@@ -28,7 +29,31 @@ import * as stream_events from "./stream_events.ts";
import * as user_group_edit from "./user_group_edit.ts";
import * as user_profile from "./user_profile.ts";
export const update_person = function update(person) {
type UserUpdate = {user_id: number} & (
| {
avatar_source: string;
avatar_url: string | null;
avatar_url_medium: string | null;
avatar_version: number;
}
| {bot_owner_id: number}
| {
custom_profile_field: {
id: number;
value: string | null;
rendered_value?: string;
};
}
| {delivery_email: string | null}
| {new_email: string}
| {full_name: string}
| {is_billing_admin: boolean}
| {role: number}
| {email: string; timezone: string}
| {is_active: boolean}
);
export const update_person = function update(person: UserUpdate): void {
const person_obj = people.maybe_get_user_by_id(person.user_id);
if (!person_obj) {
@@ -36,7 +61,7 @@ export const update_person = function update(person) {
return;
}
if (Object.hasOwn(person, "new_email")) {
if ("new_email" in person) {
const user_id = person.user_id;
const new_email = person.new_email;
@@ -49,18 +74,19 @@ export const update_person = function update(person) {
}
}
if (Object.hasOwn(person, "delivery_email")) {
if ("delivery_email" in person) {
const delivery_email = person.delivery_email;
person_obj.delivery_email = delivery_email;
user_profile.update_profile_modal_ui(person_obj, person);
if (people.is_my_user_id(person.user_id)) {
assert(delivery_email !== null);
settings_account.update_email(delivery_email);
current_user.delivery_email = delivery_email;
settings_account.hide_confirm_email_banner();
}
}
if (Object.hasOwn(person, "full_name")) {
if ("full_name" in person) {
people.set_full_name(person_obj, person.full_name);
settings_users.update_user_data(person.user_id, person);
@@ -74,7 +100,7 @@ export const update_person = function update(person) {
}
}
if (Object.hasOwn(person, "role")) {
if ("role" in person) {
person_obj.role = person.role;
person_obj.is_owner = person.role === settings_config.user_role_values.owner.code;
person_obj.is_admin =
@@ -111,14 +137,14 @@ export const update_person = function update(person) {
}
}
if (Object.hasOwn(person, "is_billing_admin")) {
if ("is_billing_admin" in person) {
person_obj.is_billing_admin = person.is_billing_admin;
if (people.is_my_user_id(person.user_id)) {
current_user.is_billing_admin = person_obj.is_billing_admin;
}
}
if (Object.hasOwn(person, "avatar_url")) {
if ("avatar_url" in person) {
const url = person.avatar_url;
person_obj.avatar_url = url;
person_obj.avatar_version = person.avatar_version;
@@ -135,7 +161,7 @@ export const update_person = function update(person) {
user_profile.update_profile_modal_ui(person_obj, person);
}
if (Object.hasOwn(person, "custom_profile_field")) {
if ("custom_profile_field" in person) {
people.set_custom_profile_field_data(person.user_id, person.custom_profile_field);
user_profile.update_user_custom_profile_fields(person_obj);
if (person.user_id === people.my_current_user_id()) {
@@ -148,7 +174,7 @@ export const update_person = function update(person) {
)?.required;
if (is_field_required) {
const $custom_user_field = $(
`.profile-settings-form .custom_user_field[data-field-id="${CSS.escape(field_id)}"]`,
`.profile-settings-form .custom_user_field[data-field-id="${CSS.escape(`${field_id}`)}"]`,
);
const $field = $custom_user_field.find(".settings-profile-user-field");
const $required_symbol = $custom_user_field.find(".required-symbol");
@@ -167,16 +193,17 @@ export const update_person = function update(person) {
}
}
if (Object.hasOwn(person, "timezone")) {
if ("timezone" in person) {
person_obj.timezone = person.timezone;
}
if (Object.hasOwn(person, "bot_owner_id")) {
if ("bot_owner_id" in person) {
assert(person_obj.is_bot);
person_obj.bot_owner_id = person.bot_owner_id;
user_profile.update_profile_modal_ui(person_obj, person);
}
if (Object.hasOwn(person, "is_active")) {
if ("is_active" in person) {
if (person.is_active) {
people.add_active_user(person_obj);
settings_users.update_view_on_reactivate(person.user_id);

View File

@@ -137,9 +137,9 @@ export function update_profile_modal_ui(
user: User,
new_data: {
user_id?: number;
bot_owner_id?: string;
avatar_url?: string;
delivery_email?: string;
bot_owner_id?: number;
avatar_url?: string | null;
delivery_email?: string | null;
role?: number;
full_name?: string;
},
@@ -166,7 +166,7 @@ export function update_profile_modal_ui(
);
}
if (new_data.delivery_email !== undefined) {
$("#email").find(".value").text(new_data.delivery_email);
$("#email .value").text(new_data.delivery_email ?? "");
}
if (new_data.role !== undefined && !user.is_bot) {
const user_type = settings_config.user_role_map.get(new_data.role);
@@ -397,7 +397,7 @@ export function get_custom_profile_field_data(
const field_value = people.get_custom_profile_data(user.user_id, field.id);
const field_type = field.type;
if (!field_value) {
if (field_value === undefined) {
return undefined;
}
if (!field_value.value) {

View File

@@ -636,7 +636,7 @@ test_people("bot_custom_profile_data", () => {
// http://localhost:9991/#organization/bot-list-admin
// and then try to edit any of the bots.
people.add_active_user(bot_botson);
assert.equal(people.get_custom_profile_data(bot_botson.user_id, 3), null);
assert.equal(people.get_custom_profile_data(bot_botson.user_id, 3), undefined);
});
test_people("user_timezone", ({override}) => {
@@ -718,14 +718,15 @@ test_people("set_custom_profile_field_data", () => {
person.profile_data = {};
const field = {
id: 3,
name: "Custom long field",
type: "text",
value: "Field value",
rendered_value: "<p>Field value</p>",
};
people.set_custom_profile_field_data(person.user_id, field);
assert.equal(person.profile_data[field.id].value, "Field value");
assert.equal(person.profile_data[field.id].rendered_value, "<p>Field value</p>");
people.set_custom_profile_field_data(person.user_id, {id: 3, value: null});
assert.ok(!(field.id in person.profile_data));
});
test_people("is_current_user_only_owner", ({override}) => {