import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; part "capabilities.freezed.dart"; part "capabilities.g.dart"; @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const Capabilities({ @JsonKey(name: "org.matrix.msc4174.webpush") final WebPush? webpush, @JsonKey(name: "m.room_versions") final RoomVersions? roomVersions, }) with _$Capabilities { /// The VAPID key to register Web Push pushers with, or null if the /// homeserver can't send Web Push. Continuwuity sends a key even when the /// feature is disabled, so `enabled` must be checked too. String? get webPushVapid => switch (webpush) { WebPush(enabled: true, :final vapid) => vapid, _ => null, }; Map toJson() => _$CapabilitiesToJson(this); factory Capabilities.fromJson(Map json) => _$CapabilitiesFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const WebPush({required final bool enabled, final String? vapid}) with _$WebPush { Map toJson() => _$WebPushToJson(this); factory WebPush.fromJson(Map json) => _$WebPushFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const RoomVersions({ @JsonKey(name: "default") required final String defaultVersion, @JsonKey(fromJson: RoomVersionStability.mapFromJson) required final IMap available, }) with _$RoomVersions { Map toJson() => _$RoomVersionsToJson(this); factory RoomVersions.fromJson(Map json) => _$RoomVersionsFromJson(json); } enum RoomVersionStability { stable, unstable, unknown; static IMap mapFromJson( Map json, ) => IMap.fromJson( json, (version) => version as String, (stability) => values.asNameMap()[stability] ?? unknown, ); }