loadMoreMessages method

Future<LoadMessagesResult> loadMoreMessages(
  1. String conversationId, {
  2. String? beforeId,
})

Load more messages (pagination).

Fetches older messages, persists them to DB (so watchMessages picks them up), and tracks the pagination cursor for the next call.

Returns the result including LoadMessagesResult.hasMore to indicate whether more history is available.

Implementation

Future<LoadMessagesResult> loadMoreMessages(String conversationId,
    {String? beforeId}) async {
  _ensureInitialized();
  final cursor = beforeId ?? _conversationPrevBatch[conversationId];
  final result = await _registry.adapter
      .loadMessages(conversationId, before: cursor);

  // Persist to DB so watchMessages() picks them up
  for (final message in result.messages) {
    await _database.insertMessage(message);
  }

  // Track cursor for next pagination call
  _conversationPrevBatch[conversationId] = result.prevBatch;

  return result;
}