good framework for content models
This commit is contained in:
parent
6af56ccb3e
commit
66356202c0
8 changed files with 80 additions and 6 deletions
|
|
@ -231,8 +231,10 @@ class ClientController extends AsyncNotifier<int> {
|
|||
Future<Paginate> paginate(PaginateRequest request) async =>
|
||||
Paginate.fromJson(await _sendCommand("paginate", request.toJson()));
|
||||
|
||||
Future<Profile> getProfile(String userId) async =>
|
||||
Profile.fromJson(await _sendCommand("get_profile", {"user_id": userId}));
|
||||
Future<Profile> getProfile(String userId) async => Profile.fromJson({
|
||||
...(await _sendCommand("get_profile", {"user_id": userId})),
|
||||
"id": userId,
|
||||
});
|
||||
|
||||
Future<void> reportEvent(ReportRequest request) =>
|
||||
_sendCommand("report_event", request.toJson());
|
||||
|
|
|
|||
|
|
@ -123,6 +123,7 @@ class MessageController extends AsyncNotifier<Message?> {
|
|||
text: "Unable to decrypt message.",
|
||||
metadata: {...metadata, "body": "Unable to decrypt message."},
|
||||
),
|
||||
|
||||
// "org.matrix.msc3381.poll.start" => Message.custom(
|
||||
// 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 Uri? avatarUrl,
|
||||
required String? reason,
|
||||
}) = _MembershipContent;
|
||||
|
||||
factory MembershipContent.fromJson(Map<String, Object?> json) =>
|
||||
_$MembershipContentFromJson(json);
|
||||
}
|
||||
15
lib/models/content/message.dart
Normal file
15
lib/models/content/message.dart
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import "package:freezed_annotation/freezed_annotation.dart";
|
||||
import "package:nexus/models/content/content.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,9 +1,16 @@
|
|||
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/epoch_date_time_converter.dart";
|
||||
import "package:nexus/models/profile.dart";
|
||||
part "event.freezed.dart";
|
||||
part "event.g.dart";
|
||||
|
||||
Profile? pmpFromJson(Map<String, dynamic> json) {
|
||||
final pmp = json["content"]?["com.beeper.per_message_profile"];
|
||||
return pmp == null ? null : Profile.fromJson(pmp);
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class Event with _$Event {
|
||||
const factory Event({
|
||||
|
|
@ -15,7 +22,6 @@ abstract class Event with _$Event {
|
|||
required String type,
|
||||
String? stateKey,
|
||||
@EpochDateTimeConverter() required DateTime timestamp,
|
||||
required IMap<String, dynamic> content,
|
||||
IMap<String, dynamic>? decrypted,
|
||||
String? decryptedType,
|
||||
@Default(IMap.empty()) IMap<String, dynamic> unsigned,
|
||||
|
|
@ -29,6 +35,8 @@ abstract class Event with _$Event {
|
|||
@Default(IMap.empty()) IMap<String, int> reactions,
|
||||
@JsonKey(name: "last_edit_rowid") int? lastEditRowId,
|
||||
@UnreadTypeConverter() UnreadType? unreadType,
|
||||
@JsonKey(fromJson: pmpFromJson) Profile? pmp,
|
||||
@JsonKey(fromJson: Content.fromJson) required Content content,
|
||||
}) = _Event;
|
||||
|
||||
factory Event.fromJson(Map<String, Object?> json) => _$EventFromJson(json);
|
||||
|
|
|
|||
|
|
@ -12,13 +12,14 @@ Object? readTimezone(Map<dynamic, dynamic> map, _) =>
|
|||
@freezed
|
||||
abstract class Profile with _$Profile {
|
||||
const factory Profile({
|
||||
String? avatarUrl,
|
||||
required String id,
|
||||
Uri? avatarUrl,
|
||||
@JsonKey(name: "displayname") String? displayName,
|
||||
|
||||
@JsonKey(readValue: readTimezone) String? timezone,
|
||||
@JsonKey(readValue: readTimezone, name: "m.tz") String? timezone,
|
||||
|
||||
@Default(IList.empty())
|
||||
@JsonKey(readValue: readPronouns)
|
||||
@JsonKey(readValue: readPronouns, name: "io.fsky.nyx.pronouns")
|
||||
IList<Pronoun> pronouns,
|
||||
}) = _Profile;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue