storeRoomUpdate method

  1. @override
Future<void> storeRoomUpdate(
  1. String roomId,
  2. SyncRoomUpdate roomUpdate,
  3. Client client
)
override

Stores a RoomUpdate object in the database. Must be called inside of transaction.

Implementation

@override
Future<void> storeRoomUpdate(
    String roomId, SyncRoomUpdate roomUpdate, Client client) async {
  // Leave room if membership is leave
  if (roomUpdate is LeftRoomUpdate) {
    await forgetRoom(roomId);
    return;
  }
  final membership = roomUpdate is LeftRoomUpdate
      ? Membership.leave
      : roomUpdate is InvitedRoomUpdate
          ? Membership.invite
          : Membership.join;
  // Make sure room exists
  final currentRawRoom = await _roomsBox.get(roomId);
  if (currentRawRoom == null) {
    await _roomsBox.put(
        roomId,
        roomUpdate is JoinedRoomUpdate
            ? Room(
                client: client,
                id: roomId,
                membership: membership,
                highlightCount:
                    roomUpdate.unreadNotifications?.highlightCount?.toInt() ??
                        0,
                notificationCount: roomUpdate
                        .unreadNotifications?.notificationCount
                        ?.toInt() ??
                    0,
                prev_batch: roomUpdate.timeline?.prevBatch,
                summary: roomUpdate.summary,
              ).toJson()
            : Room(
                client: client,
                id: roomId,
                membership: membership,
              ).toJson());
  } else if (roomUpdate is JoinedRoomUpdate) {
    final currentRoom = Room.fromJson(copyMap(currentRawRoom), client);
    await _roomsBox.put(
        roomId,
        Room(
          client: client,
          id: roomId,
          membership: membership,
          highlightCount:
              roomUpdate.unreadNotifications?.highlightCount?.toInt() ??
                  currentRoom.highlightCount,
          notificationCount:
              roomUpdate.unreadNotifications?.notificationCount?.toInt() ??
                  currentRoom.notificationCount,
          prev_batch:
              roomUpdate.timeline?.prevBatch ?? currentRoom.prev_batch,
          summary: RoomSummary.fromJson(currentRoom.summary.toJson()
            ..addAll(roomUpdate.summary?.toJson() ?? {})),
        ).toJson());
  }

  // Is the timeline limited? Then all previous messages should be
  // removed from the database!
  if (roomUpdate is JoinedRoomUpdate &&
      roomUpdate.timeline?.limited == true) {
    await _timelineFragmentsBox.delete(TupleKey(roomId, '').toString());
  }
}