loadArchiveWithTimeline method

Future<List<ArchivedRoom>> loadArchiveWithTimeline()

Fetch the archived rooms from the server and return them as a list of ArchivedRoom objects containing the Room and the associated Timeline.

Implementation

Future<List<ArchivedRoom>> loadArchiveWithTimeline() async {
  _archivedRooms.clear();
  final syncResp = await sync(
    filter: '{"room":{"include_leave":true,"timeline":{"limit":10}}}',
    timeout: _archiveCacheBusterTimeout,
    setPresence: syncPresence,
  );
  // wrap around and hope there are not more than 30 leaves in 2 minutes :)
  _archiveCacheBusterTimeout = (_archiveCacheBusterTimeout + 1) % 30;

  final leave = syncResp.rooms?.leave;
  if (leave != null) {
    for (final entry in leave.entries) {
      await _storeArchivedRoom(entry.key, entry.value);
    }
  }

  // Sort the archived rooms by last event originServerTs as this is the
  // best indicator we have to sort them. For archived rooms where we don't
  // have any, we move them to the bottom.
  final beginningOfTime = DateTime.fromMillisecondsSinceEpoch(0);
  _archivedRooms.sort((b, a) =>
      (a.room.lastEvent?.originServerTs ?? beginningOfTime)
          .compareTo(b.room.lastEvent?.originServerTs ?? beginningOfTime));

  return _archivedRooms;
}