message_fetch: Extract function to standardize API parameters.

There some common checks we need to perform before requesting
messages from the server. We extract it to be used elsewhere.
This commit is contained in:
Aman Agrawal 2024-11-19 09:22:05 +05:30 committed by Tim Abbott
parent 4edd4af68c
commit 25e77e65fd
1 changed files with 16 additions and 10 deletions

View File

@ -51,6 +51,14 @@ type MessageFetchOptions = {
validate_filter_topic_post_fetch?: boolean | undefined;
};
type MessageFetchAPIParams = {
anchor: number | string;
num_before: number;
num_after: number;
client_gravatar: boolean;
narrow?: string;
};
let first_messages_fetch = true;
export let initial_narrow_pointer: number | undefined;
export let initial_narrow_offset: number | undefined;
@ -309,7 +317,7 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str
return JSON.stringify(narrow_terms);
}
export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
function get_parameters_for_message_fetch_api(opts: MessageFetchOptions): MessageFetchAPIParams {
if (typeof opts.anchor === "number") {
// Messages that have been locally echoed messages have
// floating point temporary IDs, which is intended to be a.
@ -317,13 +325,7 @@ export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
// the nearest integer before sending a request to the server.
opts.anchor = opts.anchor.toFixed(0);
}
const data: {
anchor: number | string;
num_before: number;
num_after: number;
client_gravatar: boolean;
narrow?: string;
} = {
const data: MessageFetchAPIParams = {
anchor: opts.anchor,
num_before: opts.num_before,
num_after: opts.num_after,
@ -353,11 +355,15 @@ export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
const narrow_param_string = JSON.stringify(narrow_data);
data.narrow = handle_operators_supporting_id_based_api(narrow_param_string);
}
return data;
}
export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
const data = get_parameters_for_message_fetch_api(opts);
let update_loading_indicator =
message_lists.current !== undefined && opts.msg_list === message_lists.current;
if (opts.num_before > 0) {
msg_list_data.fetch_status.start_older_batch({
opts.msg_list_data.fetch_status.start_older_batch({
update_loading_indicator,
});
}
@ -365,7 +371,7 @@ export function load_messages(opts: MessageFetchOptions, attempt = 1): void {
if (opts.num_after > 0) {
// We hide the bottom loading indicator when we're fetching both top and bottom messages.
update_loading_indicator = update_loading_indicator && opts.num_before === 0;
msg_list_data.fetch_status.start_newer_batch({
opts.msg_list_data.fetch_status.start_newer_batch({
update_loading_indicator,
});
}