ts: Migrate people.js to TypeScript.

We use `get_by_user_id` instead of directly accessing the global dict, because
when accessing person objects directly from one of the global dicts we
need callers to check for undefined values, this can be fixed by using
`get_by_user_id` method to get person objects because that functions
makes sure to assert that we indeed have a valid user id, so it will
never return undefined values.

Co-authored-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Lalit 2023-07-12 10:52:30 +05:30 committed by GitHub
parent fcede32420
commit 59e78f96ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 341 additions and 174 deletions

View File

@ -12,6 +12,7 @@ export const page_params: {
}[];
development_environment: boolean;
is_admin: boolean;
is_bot: boolean;
is_guest: boolean;
is_moderator: boolean;
is_owner: boolean;
@ -35,6 +36,7 @@ export const page_params: {
realm_name: string;
realm_org_type: number;
realm_plan_type: number;
realm_private_message_policy: number;
realm_push_notifications_enabled: boolean;
realm_sentry_key: string | undefined;
realm_uri: string;

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,7 @@ export type DisplayRecipientUser = {
full_name: string;
id: number;
is_mirror_dummy: boolean;
unknown_local_echo_user?: boolean;
};
export type DisplayRecipient = string | DisplayRecipientUser[];
@ -38,6 +39,15 @@ export type MessageReaction = {
user_id: number;
};
// TODO/typescript: Move this to submessage.js
export type Submessage = {
id: number;
sender_id: number;
message_id: number;
content: string;
msg_type: string;
};
// TODO/typescript: Move this to server_events
export type TopicLink = {
text: string;
@ -63,17 +73,60 @@ export type RawMessage = {
sender_realm_str: string;
stream_id?: number;
subject: string;
submessages: string[];
submessages: Submessage[];
timestamp: number;
topic_links: TopicLink[];
type: MessageType;
flags: string[];
} & MatchedMessage;
// We add these boolean properties to Raw message in `message_store.set_message_booleans` method.
export type MessageWithBooleans = Omit<RawMessage, "flags"> & {
unread: boolean;
historical: boolean;
starred: boolean;
mentioned: boolean;
mentioned_me_directly: boolean;
wildcard_mentioned: boolean;
collapsed: boolean;
alerted: boolean;
};
// TODO/typescript: Move this to message_store
export type Message = RawMessage & {
export type MessageCleanReaction = {
class: string;
count: number;
emoji_alt_code: boolean;
emoji_code: string;
emoji_name: string;
is_realm_emoji: boolean;
label: string;
local_id: string;
reaction_type: string;
user_ids: number[];
vote_text: string;
};
// TODO/typescript: Move this to message_store
export type Message = Omit<MessageWithBooleans, "reactions"> & {
// Added in `reactions.set_clean_reactions`.
clean_reactions: Map<string, MessageCleanReaction>;
// Added in `message_helper.process_new_message`.
sent_by_me: boolean;
is_private?: boolean;
is_stream?: boolean;
stream?: string;
reply_to: string;
display_reply_to?: string;
pm_with_url?: string;
to_user_ids?: string;
topic: string;
// These properties are used in `message_list_view.js`.
starred_status: string;
message_reactions: MessageCleanReaction[];
url: string;
};
// TODO/typescript: Move this to server_events_dispatch

View File

@ -153,7 +153,7 @@ test("peer event error handling (bad stream_ids/user_ids)", ({override}) => {
};
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
blueslip.expect("warn", "We have untracked user_ids");
dispatch(add_event);
blueslip.reset();
@ -165,7 +165,7 @@ test("peer event error handling (bad stream_ids/user_ids)", ({override}) => {
};
blueslip.expect("warn", "We have untracked stream_ids: 8888,9999");
blueslip.expect("warn", "We have untracked user_ids: 3333,4444");
blueslip.expect("warn", "We have untracked user_ids");
dispatch(remove_event);
});

View File

@ -819,10 +819,11 @@ run_test("narrow_compute_title", ({override}) => {
assert.equal(narrow.compute_narrow_title(filter), "joe");
filter = new Filter([{operator: "dm", operand: "joe@example.com,sally@doesnotexist.com"}]);
blueslip.expect("warn", "Unknown emails: joe@example.com,sally@doesnotexist.com");
blueslip.expect("warn", "Unknown emails");
assert.equal(narrow.compute_narrow_title(filter), "translated: Invalid users");
blueslip.reset();
filter = new Filter([{operator: "dm", operand: "sally@doesnotexist.com"}]);
blueslip.expect("warn", "Unknown emails: sally@doesnotexist.com");
blueslip.expect("warn", "Unknown emails");
assert.equal(narrow.compute_narrow_title(filter), "translated: Invalid user");
});

View File

@ -340,7 +340,7 @@ test_people("basics", () => {
assert.equal(people.get_active_human_count(), 1);
// Invalid user ID returns false and warns.
blueslip.expect("warn", "Unexpectedly invalid user_id in user popover query: 123412");
blueslip.expect("warn", "Unexpectedly invalid user_id in user popover query");
assert.equal(people.is_active_user_for_popover(123412), false);
// We can still get their info for non-realm needs.
@ -1203,7 +1203,7 @@ test_people("emails_strings_to_user_ids_array", () => {
let user_ids = people.emails_strings_to_user_ids_array(`${steven.email},${maria.email}`);
assert.deepEqual(user_ids, [steven.user_id, maria.user_id]);
blueslip.expect("warn", "Unknown emails: dummyuser@example.com");
blueslip.expect("warn", "Unknown emails");
user_ids = people.emails_strings_to_user_ids_array("dummyuser@example.com");
assert.equal(user_ids, undefined);
});

View File

@ -65,7 +65,7 @@ run_test("blueslip", () => {
blueslip.expect("warn", "Unknown user ids: 1,2");
people.user_ids_string_to_emails_string("1,2");
blueslip.expect("warn", "Unknown emails: " + unknown_email);
blueslip.expect("warn", "Unknown emails");
people.email_list_to_user_ids_string([unknown_email]);
let message = {