import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; import "package:nexus/models/event.dart"; import "package:nexus/models/read_receipt.dart"; import "package:nexus/models/room_metadata.dart"; part "room.freezed.dart"; part "room.g.dart"; @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const Room({ @JsonKey(name: "meta") final RoomMetadata? metadata, @JsonKey(fromJson: Room.timelineTupleJsonToIMap) final IMap timeline = const IMap.empty(), final ISet sticky = const ISet.empty(), @JsonKey(fromJson: Room.eventsJsonToIMap) final IMap events = const IMap.empty(), final bool reset = false, final bool hasFetchedState = false, final bool hasFetchedMembers = false, final IMap> state = const IMap.empty(), final IMap> receipts = const IMap.empty(), final bool dismissNotifications = false, final bool hasMore = true, // IMap accountData, // IList notifications, }) with _$Room { /// [timeline] is an IMap of timelineRowId to eventRowId /// [events] is an IMap of eventRowId to event /// [sticky] is an ISet of eventRowId static IMap timelineTupleJsonToIMap(List json) => IMap.fromEntries( json.map( (timelineTuple) => MapEntry( timelineTuple["timeline_rowid"], timelineTuple["event_rowid"], ), ), ); static IMap eventsJsonToIMap(List json) => IMap.fromEntries( json.map((eventJson) { final event = Event.fromJson(eventJson); return MapEntry(event.rowId, event); }), ); Map toJson() => _$RoomToJson(this); factory Room.fromJson(Map json) => _$RoomFromJson(json); }