ts: Migrate `fetch_status` to TypeScript.

Extended the type `RawMessage` which represents the message object received from
the server, now it matches correctly with the OpenAPI schema for it.
This commit is contained in:
Lalit 2023-05-15 19:11:49 +05:30 committed by Tim Abbott
parent d926144e13
commit 91d3a480b6
2 changed files with 76 additions and 19 deletions

View File

@ -1,6 +1,7 @@
import * as message_feed_loading from "./message_feed_loading"; import * as message_feed_loading from "./message_feed_loading";
import type {RawMessage} from "./types";
function max_id_for_messages(messages) { function max_id_for_messages(messages: RawMessage[]): number {
let max_id = 0; let max_id = 0;
for (const msg of messages) { for (const msg of messages) {
max_id = Math.max(max_id, msg.id); max_id = Math.max(max_id, msg.id);
@ -29,14 +30,18 @@ export class FetchStatus {
// (described in detail below). // (described in detail below).
_expected_max_message_id = 0; _expected_max_message_id = 0;
start_older_batch(opts) { start_older_batch(opts: {update_loading_indicator: boolean}): void {
this._loading_older = true; this._loading_older = true;
if (opts.update_loading_indicator) { if (opts.update_loading_indicator) {
message_feed_loading.show_loading_older(); message_feed_loading.show_loading_older();
} }
} }
finish_older_batch(opts) { finish_older_batch(opts: {
found_oldest: boolean;
history_limited: boolean;
update_loading_indicator: boolean;
}): void {
this._loading_older = false; this._loading_older = false;
this._found_oldest = opts.found_oldest; this._found_oldest = opts.found_oldest;
this._history_limited = opts.history_limited; this._history_limited = opts.history_limited;
@ -45,26 +50,29 @@ export class FetchStatus {
} }
} }
can_load_older_messages() { can_load_older_messages(): boolean {
return !this._loading_older && !this._found_oldest; return !this._loading_older && !this._found_oldest;
} }
has_found_oldest() { has_found_oldest(): boolean {
return this._found_oldest; return this._found_oldest;
} }
history_limited() { history_limited(): boolean {
return this._history_limited; return this._history_limited;
} }
start_newer_batch(opts) { start_newer_batch(opts: {update_loading_indicator: boolean}): void {
this._loading_newer = true; this._loading_newer = true;
if (opts.update_loading_indicator) { if (opts.update_loading_indicator) {
message_feed_loading.show_loading_newer(); message_feed_loading.show_loading_newer();
} }
} }
finish_newer_batch(messages, opts) { finish_newer_batch(
messages: RawMessage[],
opts: {update_loading_indicator: boolean; found_newest: boolean},
): boolean {
// Returns true if and only if the caller needs to trigger an // Returns true if and only if the caller needs to trigger an
// additional fetch due to the race described below. // additional fetch due to the race described below.
const found_max_message_id = max_id_for_messages(messages); const found_max_message_id = max_id_for_messages(messages);
@ -119,15 +127,15 @@ export class FetchStatus {
return false; return false;
} }
can_load_newer_messages() { can_load_newer_messages(): boolean {
return !this._loading_newer && !this._found_newest; return !this._loading_newer && !this._found_newest;
} }
has_found_newest() { has_found_newest(): boolean {
return this._found_newest; return this._found_newest;
} }
update_expected_max_message_id(messages) { update_expected_max_message_id(messages: RawMessage[]): void {
this._expected_max_message_id = Math.max( this._expected_max_message_id = Math.max(
this._expected_max_message_id, this._expected_max_message_id,
max_id_for_messages(messages), max_id_for_messages(messages),

View File

@ -6,18 +6,73 @@ export type MatchedMessage = {
// TODO/typescript: Move this to message_store // TODO/typescript: Move this to message_store
export type MessageType = "private" | "stream"; export type MessageType = "private" | "stream";
export type MessageReactionType = "unicode_emoji" | "realm_emoji" | "zulip_extra_emoji";
// TODO/typescript: Move these types to message_store
export type DisplayRecipientUser = {
email: string;
full_name: string;
id: number;
is_mirror_dummy: boolean;
};
export type DisplayRecipient = string | DisplayRecipientUser[];
export type MessageEditHistoryEntry = {
user_id: number | null;
timestamp: number;
prev_content?: string;
prev_rendered_content?: string;
prev_rendered_content_version?: number;
prev_stream?: number;
prev_topic?: string;
stream?: number;
topic?: string;
};
export type MessageReaction = {
emoji_name: string;
emoji_code: string;
reaction_type: MessageReactionType;
user_id: number;
};
// TODO/typescript: Move this to server_events
export type TopicLink = {
text: string;
url: string;
};
// TODO/typescript: Move this to message_store // TODO/typescript: Move this to message_store
export type RawMessage = { export type RawMessage = {
avatar_url: string | null;
client: string;
content: string;
content_type: "text/html";
display_recipient: DisplayRecipient;
edit_history?: MessageEditHistoryEntry[];
id: number;
is_me_message: boolean;
last_edit_timestamp?: number;
reactions: MessageReaction[];
recipient_id: number;
sender_email: string; sender_email: string;
stream_id: number; sender_full_name: string;
sender_id: number;
sender_realm_str: string;
stream_id?: number;
subject: string; subject: string;
submessages: string[];
timestamp: number;
topic_links: TopicLink[];
type: MessageType; type: MessageType;
flags: string[];
} & MatchedMessage; } & MatchedMessage;
// TODO/typescript: Move this to message_store // TODO/typescript: Move this to message_store
export type Message = RawMessage & { export type Message = RawMessage & {
to_user_ids: string; to_user_ids?: string;
topic: string; topic: string;
}; };
@ -32,12 +87,6 @@ export type UserGroupUpdateEvent = {
}; };
}; };
// TODO/typescript: Move this to server_events
export type TopicLink = {
text: string;
url: string;
};
// TODO/typescript: Move this to server_events // TODO/typescript: Move this to server_events
export type UpdateMessageEvent = { export type UpdateMessageEvent = {
id: number; id: number;