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
  if (!_roomsBox.containsKey(roomId.toHiveKey)) {
    await _roomsBox.put(
        roomId.toHiveKey,
        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 currentRawRoom = await _roomsBox.get(roomId.toHiveKey);
    final currentRoom = Room.fromJson(convertToJson(currentRawRoom), client);
    await _roomsBox.put(
        roomId.toHiveKey,
        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(MultiKey(roomId, '').toString());
  }
}