import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; import "package:nexus/models/content/membership.dart"; part "profile_response.freezed.dart"; part "profile_response.g.dart"; @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const ProfileResponse({ @JsonKey(fromJson: Profile.fromJson) required final Profile profile, final Bio? bio, }) with _$ProfileResponse { Map toJson() => _$ProfileResponseToJson(this); factory ProfileResponse.fromJson(Map json) => _$ProfileResponseFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const Bio({required final String html, final String? editSource}) with _$Bio { Map toJson() => _$BioToJson(this); factory Bio.fromJson(Map json) => _$BioFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const Profile({ final String? id, final String? parseError, @JsonKey(name: Profile.avatarUrlKey) final Uri? avatarUrl, @JsonKey( name: Profile.displayNameKey, fromJson: MembershipContent.displaynameFromJson, ) final String? displayName, @JsonKey(name: Profile.timezoneKey) final String? timezone, @JsonKey(readValue: Profile.readPronouns, name: Profile.pronounsKey) final IList pronouns = const .empty(), }) with _$Profile { static const avatarUrlKey = "avatar_url"; static const displayNameKey = "displayname"; static const timezoneKey = "m.tz"; static const pronounsKey = "m.pronouns"; static const unstablePronounsKey = "io.fsky.nyx.pronouns"; Map toJson() => _$ProfileToJson(this); Profile withPronouns(String language, Iterable summaries) => copyWith( pronouns: pronouns .where((pronoun) => pronoun.language != language) .toIList() .addAll( summaries.map( (summary) => Pronoun(language: language, summary: summary), ), ), ); static Object? readPronouns(Map map, String key) => /* until stable, commented out: map[key] ?? */ map[Profile .unstablePronounsKey]; factory Profile.fromJson(Map json) => _$ProfileFromJson(json); factory Profile.fromJsonWithCatch(Map json) { try { return Profile.fromJson(json); } catch (error) { return Profile(parseError: error.toString()); } } } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const Pronoun({ required final String language, required final String summary, }) with _$Pronoun { Map toJson() => _$PronounToJson(this); factory Pronoun.fromJson(Map json) => _$PronounFromJson(json); }