import "dart:math"; import "package:collection/collection.dart"; import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:freezed_annotation/freezed_annotation.dart"; import "package:nexus/models/content/content.dart"; part "poll_start.freezed.dart"; part "poll_start.g.dart"; @Freezed(toJson: false, fromJson: false) @JsonSerializable(createToJson: false) class const PollStartContent({ @JsonKey(name: "org.matrix.msc3381.poll.start") required final PollStart poll, }) extends Content with _$PollStartContent { @override Map toJson() => { "org.matrix.msc3381.poll.start": poll.toJson(), // Shown by clients without poll support "org.matrix.msc1767.text": [ poll.question, ...poll.answers.mapIndexed( (index, answer) => "${index + 1}. ${answer.text}", ), ].join("\n"), }; factory PollStartContent.fromJson(Map json) => _$PollStartContentFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable() class const PollStart({ @JsonKey(readValue: PollStart.questionFromJson) final String question = "", final int? maxSelections, @JsonKey(readValue: PollStart.answersFromJson) final IList answers = const .empty(), }) with _$PollStart { static const maxAnswers = 20; static String? extensibleTextFromJson(Object? json) => switch (json) { {"org.matrix.msc1767.text": final String text} => text, {"body": final String body} => body, {"org.matrix.msc1767.message": final List message} => message .whereType() .map((part) => part["body"]) .whereType() .firstOrNull, _ => null, }; static String? questionFromJson(Map json, String key) => PollStart.extensibleTextFromJson(json[key]); static List? answersFromJson( Map json, String key, ) => (json[key] as List?)?.take(maxAnswers).toList(); /// [maxSelections], defaulting to 1, and at most the number of answers int get selectionLimit => (maxSelections ?? 1).clamp(1, max(answers.length, 1)); Map toJson() => { ..._$PollStartToJson(this), // We don't allow undisclosed polls as they make no sense "kind": "org.matrix.msc3381.poll.disclosed", "question": {"org.matrix.msc1767.text": question}, "answers": answers.map((answer) => answer.toJson()).toList(), }; factory PollStart.fromJson(Map json) => _$PollStartFromJson(json); } @Freezed(toJson: false, fromJson: false) @JsonSerializable(createToJson: false) class const PollAnswer({ required final String id, @JsonKey(readValue: PollAnswer.textFromJson) final String text = "", }) with _$PollAnswer { static String? textFromJson(Map json, _) => PollStart.extensibleTextFromJson(json); Map toJson() => {"id": id, "org.matrix.msc1767.text": text}; factory PollAnswer.fromJson(Map json) => _$PollAnswerFromJson(json); }