good framework for content models
This commit is contained in:
parent
6af56ccb3e
commit
9caff33154
6 changed files with 66 additions and 1 deletions
|
|
@ -123,6 +123,7 @@ class MessageController extends AsyncNotifier<Message?> {
|
||||||
text: "Unable to decrypt message.",
|
text: "Unable to decrypt message.",
|
||||||
metadata: {...metadata, "body": "Unable to decrypt message."},
|
metadata: {...metadata, "body": "Unable to decrypt message."},
|
||||||
),
|
),
|
||||||
|
|
||||||
// "org.matrix.msc3381.poll.start" => Message.custom(
|
// "org.matrix.msc3381.poll.start" => Message.custom(
|
||||||
// metadata: {
|
// metadata: {
|
||||||
// ...metadata,
|
// ...metadata,
|
||||||
|
|
|
||||||
17
lib/models/content/content.dart
Normal file
17
lib/models/content/content.dart
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
import "package:nexus/models/content/encrypted.dart";
|
||||||
|
import "package:nexus/models/content/membership.dart";
|
||||||
|
import "package:nexus/models/content/message.dart";
|
||||||
|
|
||||||
|
class Content {
|
||||||
|
Content();
|
||||||
|
factory Content.fromJson(Map<String, dynamic> json) => Content();
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {};
|
||||||
|
static Content fromEventJson(Map<String, dynamic> eventJson) =>
|
||||||
|
switch (eventJson["type"]) {
|
||||||
|
EncryptedContent.type => EncryptedContent.fromJson,
|
||||||
|
MembershipContent.type => MembershipContent.fromJson,
|
||||||
|
MessageContent.type => MessageContent.fromJson,
|
||||||
|
_ => Content.fromJson,
|
||||||
|
}(eventJson);
|
||||||
|
}
|
||||||
9
lib/models/content/encrypted.dart
Normal file
9
lib/models/content/encrypted.dart
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import "package:nexus/models/content/content.dart";
|
||||||
|
|
||||||
|
class EncryptedContent extends Content {
|
||||||
|
EncryptedContent();
|
||||||
|
factory EncryptedContent.fromJson(Map<String, dynamic> json) =>
|
||||||
|
EncryptedContent();
|
||||||
|
|
||||||
|
static const type = "m.room.encrypted";
|
||||||
|
}
|
||||||
21
lib/models/content/membership.dart
Normal file
21
lib/models/content/membership.dart
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import "package:freezed_annotation/freezed_annotation.dart";
|
||||||
|
import "package:nexus/models/content/content.dart";
|
||||||
|
import "package:nexus/models/membership_status.dart";
|
||||||
|
part "membership.freezed.dart";
|
||||||
|
part "membership.g.dart";
|
||||||
|
|
||||||
|
@freezed
|
||||||
|
abstract class MembershipContent extends Content with _$MembershipContent {
|
||||||
|
static const type = "m.room.membership";
|
||||||
|
|
||||||
|
MembershipContent._();
|
||||||
|
const factory MembershipContent({
|
||||||
|
@JsonKey(name: "displayname") required String displayName,
|
||||||
|
required MembershipStatus membership,
|
||||||
|
required String? avatarUrl,
|
||||||
|
required String? reason,
|
||||||
|
}) = _MembershipContent;
|
||||||
|
|
||||||
|
factory MembershipContent.fromJson(Map<String, Object?> json) =>
|
||||||
|
_$MembershipContentFromJson(json);
|
||||||
|
}
|
||||||
16
lib/models/content/message.dart
Normal file
16
lib/models/content/message.dart
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import "package:freezed_annotation/freezed_annotation.dart";
|
||||||
|
import "package:nexus/models/content/content.dart";
|
||||||
|
import "package:nexus/models/membership_status.dart";
|
||||||
|
part "message.freezed.dart";
|
||||||
|
part "message.g.dart";
|
||||||
|
|
||||||
|
@freezed
|
||||||
|
abstract class MessageContent extends Content with _$MessageContent {
|
||||||
|
static const type = "m.room.message";
|
||||||
|
|
||||||
|
MessageContent._();
|
||||||
|
const factory MessageContent({required String msgtype}) = _MessageContent;
|
||||||
|
|
||||||
|
factory MessageContent.fromJson(Map<String, Object?> json) =>
|
||||||
|
_$MessageContentFromJson(json);
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||||
import "package:freezed_annotation/freezed_annotation.dart";
|
import "package:freezed_annotation/freezed_annotation.dart";
|
||||||
|
import "package:nexus/models/content/content.dart";
|
||||||
import "package:nexus/models/epoch_date_time_converter.dart";
|
import "package:nexus/models/epoch_date_time_converter.dart";
|
||||||
part "event.freezed.dart";
|
part "event.freezed.dart";
|
||||||
part "event.g.dart";
|
part "event.g.dart";
|
||||||
|
|
@ -15,7 +16,6 @@ abstract class Event with _$Event {
|
||||||
required String type,
|
required String type,
|
||||||
String? stateKey,
|
String? stateKey,
|
||||||
@EpochDateTimeConverter() required DateTime timestamp,
|
@EpochDateTimeConverter() required DateTime timestamp,
|
||||||
required IMap<String, dynamic> content,
|
|
||||||
IMap<String, dynamic>? decrypted,
|
IMap<String, dynamic>? decrypted,
|
||||||
String? decryptedType,
|
String? decryptedType,
|
||||||
@Default(IMap.empty()) IMap<String, dynamic> unsigned,
|
@Default(IMap.empty()) IMap<String, dynamic> unsigned,
|
||||||
|
|
@ -29,6 +29,7 @@ abstract class Event with _$Event {
|
||||||
@Default(IMap.empty()) IMap<String, int> reactions,
|
@Default(IMap.empty()) IMap<String, int> reactions,
|
||||||
@JsonKey(name: "last_edit_rowid") int? lastEditRowId,
|
@JsonKey(name: "last_edit_rowid") int? lastEditRowId,
|
||||||
@UnreadTypeConverter() UnreadType? unreadType,
|
@UnreadTypeConverter() UnreadType? unreadType,
|
||||||
|
@JsonKey(fromJson: Content.fromJson) required Content content,
|
||||||
}) = _Event;
|
}) = _Event;
|
||||||
|
|
||||||
factory Event.fromJson(Map<String, Object?> json) => _$EventFromJson(json);
|
factory Event.fromJson(Map<String, Object?> json) => _$EventFromJson(json);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue