message_fetch: Populate rendered message lists with all_messages_data.

This will help us provide more messages in the current narrow for the
user after a reload without any extra server queries.
This commit is contained in:
Aman Agrawal
2024-12-03 15:06:34 +05:30
committed by Tim Abbott
parent 107559b1bb
commit 40eda337f9

View File

@@ -14,6 +14,7 @@ import * as message_feed_top_notices from "./message_feed_top_notices.ts";
import * as message_helper from "./message_helper.ts";
import type {MessageList} from "./message_list.ts";
import type {MessageListData} from "./message_list_data.ts";
import * as message_list_data_cache from "./message_list_data_cache.ts";
import * as message_lists from "./message_lists.ts";
import {raw_message_schema} from "./message_store.ts";
import * as message_util from "./message_util.ts";
@@ -61,6 +62,7 @@ type MessageFetchAPIParams = {
};
let first_messages_fetch = true;
let initial_backfill_for_all_messages_done = false;
export let initial_narrow_pointer: number | undefined;
export let initial_narrow_offset: number | undefined;
@@ -668,6 +670,7 @@ export function initialize(finished_initial_fetch: () => void): void {
}
if (data.found_oldest) {
initial_backfill_for_all_messages_done = true;
return;
}
@@ -680,10 +683,12 @@ export function initialize(finished_initial_fetch: () => void): void {
all_messages_data.num_items() >= consts.minimum_initial_backfill_size &&
latest_message.timestamp < fetch_target_day_timestamp
) {
initial_backfill_for_all_messages_done = true;
return;
}
if (all_messages_data.num_items() >= consts.maximum_initial_backfill_size) {
initial_backfill_for_all_messages_done = true;
return;
}
@@ -715,6 +720,54 @@ export function initialize(finished_initial_fetch: () => void): void {
} catch (error) {
blueslip.error("Error in recent_view_ui.process_messages", undefined, error);
}
if (
!recent_view_ui.is_backfill_in_progress &&
!first_messages_fetch &&
initial_backfill_for_all_messages_done
) {
// We only populate other cached data for major backfills.
return;
}
// Since we backfill a lot more messages here compared to rendered message list,
// we can try populating them if we can do so locally.
for (const msg_list_data of message_list_data_cache.all()) {
if (msg_list_data === all_messages_data) {
continue;
}
if (!msg_list_data.filter.can_apply_locally()) {
continue;
}
if (msg_list_data.visibly_empty()) {
// If the message list is visibly empty, we don't want to add
// message here to break the continuous message history.
// We will rely here on our backfill logic to render any visible
// messages if we can.
continue;
}
// This callback is only called when backfilling messages,
// so we need to check for the presence of any message from
// the message list in the all_messages_data to
// check for continuous message history for the message list.
const first_message = msg_list_data.first();
assert(first_message !== undefined);
if (all_messages_data.get(first_message.id) !== undefined) {
const messages_to_populate = all_messages_data.message_range(0, first_message.id);
if (msg_list_data.rendered_message_list_id) {
const msg_list = message_lists.rendered_message_lists.get(
msg_list_data.rendered_message_list_id,
);
assert(msg_list !== undefined);
msg_list.add_messages(messages_to_populate, {});
} else {
msg_list_data.add_messages(messages_to_populate);
}
}
}
});
// TODO: Ideally we'd have loading indicators for Recent Conversations