allow sending emoji/stickers from emote packs
This commit is contained in:
parent
868023a4a1
commit
3754bc2feb
1 changed files with 701 additions and 74 deletions
68
lib/models/content/image_pack.dart
Normal file
68
lib/models/content/image_pack.dart
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:freezed_annotation/freezed_annotation.dart";
|
||||
import "package:nexus/models/content/content.dart";
|
||||
import "package:nexus/models/info/image.dart";
|
||||
part "image_pack.freezed.dart";
|
||||
part "image_pack.g.dart";
|
||||
|
||||
/// An image pack (MSC2545), from a room's state.
|
||||
@freezed
|
||||
sealed class ImagePackContent extends Content with _$ImagePackContent {
|
||||
ImagePackContent._();
|
||||
factory ImagePackContent({
|
||||
@Default(IMap.empty()) IMap<String, PackImage> images,
|
||||
PackInfo? pack,
|
||||
}) = _ImagePackContent;
|
||||
|
||||
IMap<String, PackImage> imagesFor(ImageUsage usage) => images.where(
|
||||
(_, image) => switch ((image.usage, pack?.usage ?? const .empty())) {
|
||||
(final usages, _) when usages.isNotEmpty => usages.contains(usage),
|
||||
(_, final packUsages) when packUsages.isNotEmpty => packUsages.contains(
|
||||
usage,
|
||||
),
|
||||
_ => true,
|
||||
},
|
||||
);
|
||||
|
||||
factory ImagePackContent.fromJson(Map<String, Object?> json) =>
|
||||
_$ImagePackContentFromJson(json);
|
||||
}
|
||||
|
||||
@Freezed(toJson: false, fromJson: false)
|
||||
@JsonSerializable()
|
||||
class const PackImage({
|
||||
required final Uri url,
|
||||
final String? body,
|
||||
final ImageInfo? info,
|
||||
@JsonKey(fromJson: ImageUsage.setFromJson)
|
||||
final ISet<ImageUsage> usage = const .empty(),
|
||||
}) with _$PackImage {
|
||||
Map<String, Object?> toJson() => _$PackImageToJson(this);
|
||||
|
||||
factory PackImage.fromJson(Map<String, Object?> json) =>
|
||||
_$PackImageFromJson(json);
|
||||
}
|
||||
|
||||
@Freezed(toJson: false, fromJson: false)
|
||||
@JsonSerializable()
|
||||
class const PackInfo({
|
||||
final String? displayName,
|
||||
final Uri? avatarUrl,
|
||||
final String? attribution,
|
||||
@JsonKey(fromJson: ImageUsage.setFromJson)
|
||||
final ISet<ImageUsage> usage = const .empty(),
|
||||
}) with _$PackInfo {
|
||||
Map<String, Object?> toJson() => _$PackInfoToJson(this);
|
||||
|
||||
factory PackInfo.fromJson(Map<String, Object?> json) =>
|
||||
_$PackInfoFromJson(json);
|
||||
}
|
||||
|
||||
enum ImageUsage {
|
||||
emoticon,
|
||||
sticker;
|
||||
|
||||
/// Ignores usages we don't know about.
|
||||
static ISet<ImageUsage> setFromJson(List<dynamic>? json) =>
|
||||
ISet(values.where((value) => json?.contains(value.name) ?? false));
|
||||
}
|
||||
Loading…
Reference in a new issue