feat: Converted to ios models

This commit is contained in:
Deven Joshi
2021-04-13 18:20:43 +05:30
parent 391eb8c1ce
commit fe07b8dbb0
36 changed files with 406 additions and 344 deletions
@@ -15,7 +15,7 @@ class Attachment extends Equatable {
/// Constructor used for json serialization
Attachment({
String? id,
this.type,
required this.type,
this.titleLink,
String? title,
this.thumbUrl,
@@ -57,7 +57,7 @@ class Attachment extends Equatable {
///The attachment type based on the URL resource. This can be: audio,
///image or video
final String? type;
final String type;
///The link to which the attachment message points to.
final String? titleLink;
@@ -9,7 +9,7 @@ part of 'attachment.dart';
Attachment _$AttachmentFromJson(Map json) {
return Attachment(
id: json['id'] as String?,
type: json['type'] as String?,
type: json['type'] as String,
titleLink: json['title_link'] as String?,
title: json['title'] as String?,
thumbUrl: json['thumb_url'] as String?,
@@ -44,7 +44,9 @@ Attachment _$AttachmentFromJson(Map json) {
}
Map<String, dynamic> _$AttachmentToJson(Attachment instance) {
final val = <String, dynamic>{};
final val = <String, dynamic>{
'type': instance.type,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
@@ -52,7 +54,6 @@ Map<String, dynamic> _$AttachmentToJson(Attachment instance) {
}
}
writeNotNull('type', instance.type);
writeNotNull('title_link', instance.titleLink);
writeNotNull('title', instance.title);
writeNotNull('thumb_url', instance.thumbUrl);
@@ -12,19 +12,34 @@ class ChannelModel {
ChannelModel({
this.id,
this.type,
this.cid,
this.config,
required this.cid,
required this.config,
this.createdBy,
this.frozen,
this.frozen = false,
this.lastMessageAt,
this.createdAt,
this.updatedAt,
required this.createdAt,
required this.updatedAt,
this.deletedAt,
this.memberCount,
this.memberCount = 0,
this.extraData,
this.team,
});
ChannelModel.temp({
this.id,
this.type,
required this.cid,
this.createdBy,
this.frozen = false,
this.lastMessageAt,
this.deletedAt,
this.memberCount = 0,
this.extraData,
this.team,
}) : createdAt = DateTime.now(),
updatedAt = DateTime.now(),
config = ChannelConfig();
/// Create a new instance from a json
factory ChannelModel.fromJson(Map<String, dynamic>? json) =>
_$ChannelModelFromJson(
@@ -38,11 +53,11 @@ class ChannelModel {
/// The cid of this channel
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final String? cid;
final String cid;
/// The channel configuration data
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final ChannelConfig? config;
final ChannelConfig config;
/// The user that created this channel
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -50,7 +65,7 @@ class ChannelModel {
/// True if this channel is frozen
@JsonKey(includeIfNull: false)
final bool? frozen;
final bool frozen;
/// The date of the last message
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -58,11 +73,11 @@ class ChannelModel {
/// The date of channel creation
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? createdAt;
final DateTime createdAt;
/// The date of the last channel update
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? updatedAt;
final DateTime updatedAt;
/// The date of channel deletion
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -70,7 +85,7 @@ class ChannelModel {
/// The count of this channel members
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final int? memberCount;
final int memberCount;
/// Map of custom channel extraData
@JsonKey(includeIfNull: false)
@@ -10,30 +10,24 @@ ChannelModel _$ChannelModelFromJson(Map json) {
return ChannelModel(
id: json['id'] as String?,
type: json['type'] as String?,
cid: json['cid'] as String?,
config: json['config'] == null
? null
: ChannelConfig.fromJson(
Map<String, dynamic>.from(json['config'] as Map)),
cid: json['cid'] as String,
config: ChannelConfig.fromJson(
Map<String, dynamic>.from(json['config'] as Map)),
createdBy: json['created_by'] == null
? null
: User.fromJson((json['created_by'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
frozen: json['frozen'] as bool?,
frozen: json['frozen'] as bool,
lastMessageAt: json['last_message_at'] == null
? null
: DateTime.parse(json['last_message_at'] as String),
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
deletedAt: json['deleted_at'] == null
? null
: DateTime.parse(json['deleted_at'] as String),
memberCount: json['member_count'] as int?,
memberCount: json['member_count'] as int,
extraData: (json['extra_data'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
),
@@ -56,7 +50,7 @@ Map<String, dynamic> _$ChannelModelToJson(ChannelModel instance) {
writeNotNull('cid', readonly(instance.cid));
writeNotNull('config', readonly(instance.config));
writeNotNull('created_by', readonly(instance.createdBy));
writeNotNull('frozen', instance.frozen);
val['frozen'] = instance.frozen;
writeNotNull('last_message_at', readonly(instance.lastMessageAt));
writeNotNull('created_at', readonly(instance.createdAt));
writeNotNull('updated_at', readonly(instance.updatedAt));
@@ -25,22 +25,22 @@ class ChannelState {
final ChannelModel? channel;
/// A paginated list of channel messages
final List<Message>? messages;
final List<Message> messages;
/// A paginated list of channel members
final List<Member?>? members;
final List<Member?> members;
/// A paginated list of pinned messages
final List<Message>? pinnedMessages;
final List<Message> pinnedMessages;
/// The count of users watching the channel
final int? watcherCount;
/// A paginated list of users watching the channel
final List<User>? watchers;
final List<User> watchers;
/// The list of channel reads
final List<Read>? read;
final List<Read> read;
/// Create a new instance from a json
static ChannelState fromJson(Map<String, dynamic> json) =>
@@ -13,29 +13,29 @@ ChannelState _$ChannelStateFromJson(Map json) {
: ChannelModel.fromJson((json['channel'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
messages: (json['messages'] as List<dynamic>?)
?.map((e) => Message.fromJson((e as Map?)?.map(
messages: (json['messages'] as List<dynamic>)
.map((e) => Message.fromJson((e as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)))
.toList(),
members: (json['members'] as List<dynamic>?)
?.map((e) => e == null
members: (json['members'] as List<dynamic>)
.map((e) => e == null
? null
: Member.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
pinnedMessages: (json['pinned_messages'] as List<dynamic>?)
?.map((e) => Message.fromJson((e as Map?)?.map(
pinnedMessages: (json['pinned_messages'] as List<dynamic>)
.map((e) => Message.fromJson((e as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)))
.toList(),
watcherCount: json['watcher_count'] as int?,
watchers: (json['watchers'] as List<dynamic>?)
?.map((e) => User.fromJson((e as Map?)?.map(
watchers: (json['watchers'] as List<dynamic>)
.map((e) => User.fromJson((e as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)))
.toList(),
read: (json['read'] as List<dynamic>?)
?.map((e) => Read.fromJson(Map<String, dynamic>.from(e as Map)))
read: (json['read'] as List<dynamic>)
.map((e) => Read.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
);
}
@@ -43,11 +43,11 @@ ChannelState _$ChannelStateFromJson(Map json) {
Map<String, dynamic> _$ChannelStateToJson(ChannelState instance) =>
<String, dynamic>{
'channel': instance.channel?.toJson(),
'messages': instance.messages?.map((e) => e.toJson()).toList(),
'members': instance.members?.map((e) => e?.toJson()).toList(),
'messages': instance.messages.map((e) => e.toJson()).toList(),
'members': instance.members.map((e) => e?.toJson()).toList(),
'pinned_messages':
instance.pinnedMessages?.map((e) => e.toJson()).toList(),
instance.pinnedMessages.map((e) => e.toJson()).toList(),
'watcher_count': instance.watcherCount,
'watchers': instance.watchers?.map((e) => e.toJson()).toList(),
'read': instance.read?.map((e) => e.toJson()).toList(),
'watchers': instance.watchers.map((e) => e.toJson()).toList(),
'read': instance.read.map((e) => e.toJson()).toList(),
};
@@ -7,7 +7,7 @@ part 'device.g.dart';
class Device {
/// Constructor used for json serialization
Device({
this.id,
required this.id,
this.pushProvider,
});
@@ -15,7 +15,7 @@ class Device {
factory Device.fromJson(Map<String, dynamic> json) => _$DeviceFromJson(json);
/// The id of the device
final String? id;
final String id;
/// The notification push provider
final String? pushProvider;
@@ -8,7 +8,7 @@ part of 'device.dart';
Device _$DeviceFromJson(Map json) {
return Device(
id: json['id'] as String?,
id: json['id'] as String,
pushProvider: json['push_provider'] as String?,
);
}
@@ -171,15 +171,15 @@ class EventChannel extends ChannelModel {
this.members,
String? id,
String? type,
String? cid,
ChannelConfig? config,
required String cid,
required ChannelConfig config,
User? createdBy,
bool? frozen,
bool frozen = false,
DateTime? lastMessageAt,
DateTime? createdAt,
DateTime? updatedAt,
required DateTime createdAt,
required DateTime updatedAt,
DateTime? deletedAt,
int? memberCount,
required int memberCount,
Map<String, dynamic>? extraData,
}) : super(
id: id,
@@ -92,30 +92,24 @@ EventChannel _$EventChannelFromJson(Map json) {
.toList(),
id: json['id'] as String?,
type: json['type'] as String?,
cid: json['cid'] as String?,
config: json['config'] == null
? null
: ChannelConfig.fromJson(
Map<String, dynamic>.from(json['config'] as Map)),
cid: json['cid'] as String,
config: ChannelConfig.fromJson(
Map<String, dynamic>.from(json['config'] as Map)),
createdBy: json['created_by'] == null
? null
: User.fromJson((json['created_by'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
frozen: json['frozen'] as bool?,
frozen: json['frozen'] as bool,
lastMessageAt: json['last_message_at'] == null
? null
: DateTime.parse(json['last_message_at'] as String),
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
deletedAt: json['deleted_at'] == null
? null
: DateTime.parse(json['deleted_at'] as String),
memberCount: json['member_count'] as int?,
memberCount: json['member_count'] as int,
extraData: (json['extra_data'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
),
@@ -137,7 +131,7 @@ Map<String, dynamic> _$EventChannelToJson(EventChannel instance) {
writeNotNull('cid', readonly(instance.cid));
writeNotNull('config', readonly(instance.config));
writeNotNull('created_by', readonly(instance.createdBy));
writeNotNull('frozen', instance.frozen);
val['frozen'] = instance.frozen;
writeNotNull('last_message_at', readonly(instance.lastMessageAt));
writeNotNull('created_at', readonly(instance.createdAt));
writeNotNull('updated_at', readonly(instance.updatedAt));
+12 -12
View File
@@ -12,14 +12,14 @@ class Member {
this.user,
this.inviteAcceptedAt,
this.inviteRejectedAt,
this.invited,
this.role,
this.invited = false,
required this.role,
this.userId,
this.isModerator,
this.createdAt,
this.updatedAt,
this.banned,
this.shadowBanned,
required this.createdAt,
required this.updatedAt,
this.banned = false,
this.shadowBanned = false,
});
/// Create a new instance from a json
@@ -40,10 +40,10 @@ class Member {
final DateTime? inviteRejectedAt;
/// True if the user has been invited to the channel
final bool? invited;
final bool invited;
/// The role of the user in the channel
final String? role;
final String role;
/// The id of the interested user
final String? userId;
@@ -52,16 +52,16 @@ class Member {
final bool? isModerator;
/// True if the member is banned from the channel
final bool? banned;
final bool banned;
/// True if the member is shadow banned from the channel
final bool? shadowBanned;
final bool shadowBanned;
/// The date of creation
final DateTime? createdAt;
final DateTime createdAt;
/// The last date of update
final DateTime? updatedAt;
final DateTime updatedAt;
/// Creates a copy of [Member] with specified attributes overridden.
Member copyWith({
@@ -19,18 +19,14 @@ Member _$MemberFromJson(Map json) {
inviteRejectedAt: json['invite_rejected_at'] == null
? null
: DateTime.parse(json['invite_rejected_at'] as String),
invited: json['invited'] as bool?,
role: json['role'] as String?,
invited: json['invited'] as bool,
role: json['role'] as String,
userId: json['user_id'] as String?,
isModerator: json['is_moderator'] as bool?,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
banned: json['banned'] as bool?,
shadowBanned: json['shadow_banned'] as bool?,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
banned: json['banned'] as bool,
shadowBanned: json['shadow_banned'] as bool,
);
}
@@ -44,6 +40,6 @@ Map<String, dynamic> _$MemberToJson(Member instance) => <String, dynamic>{
'is_moderator': instance.isModerator,
'banned': instance.banned,
'shadow_banned': instance.shadowBanned,
'created_at': instance.createdAt?.toIso8601String(),
'updated_at': instance.updatedAt?.toIso8601String(),
'created_at': instance.createdAt.toIso8601String(),
'updated_at': instance.updatedAt.toIso8601String(),
};
@@ -46,8 +46,8 @@ class Message extends Equatable {
/// Constructor used for json serialization
Message({
String? id,
this.text,
this.type,
required this.text,
required this.type,
this.attachments,
this.mentionedUsers,
this.silent,
@@ -63,8 +63,8 @@ class Message extends Equatable {
this.threadParticipants,
this.showInChannel,
this.command,
this.createdAt,
this.updatedAt,
required this.createdAt,
required this.updatedAt,
this.user,
this.pinned = false,
this.pinnedAt,
@@ -77,6 +77,40 @@ class Message extends Equatable {
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc();
/// Constructor for creating temporary/throwaway message with id and text
Message.temp({
String? id,
required this.text,
this.attachments,
this.mentionedUsers,
this.silent,
this.shadowed,
this.reactionCounts,
this.reactionScores,
this.latestReactions,
this.ownReactions,
this.parentId,
this.quotedMessage,
this.quotedMessageId,
this.replyCount = 0,
this.threadParticipants,
this.showInChannel,
this.command,
this.user,
this.pinned = false,
this.pinnedAt,
DateTime? pinExpires,
this.pinnedBy,
this.extraData,
this.deletedAt,
this.status = MessageSendingStatus.sent,
this.skipPush,
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc(),
createdAt = DateTime.now(),
updatedAt = DateTime.now(),
type = '';
/// Create a new instance from a json
factory Message.fromJson(Map<String, dynamic>? json) => _$MessageFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)!);
@@ -86,7 +120,7 @@ class Message extends Equatable {
final String id;
/// The text of this message
final String? text;
final String text;
/// The status of a sending message
@JsonKey(ignore: true)
@@ -94,7 +128,7 @@ class Message extends Equatable {
/// The message type
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final String? type;
final String type;
/// The list of attachments, either provided by the user or generated from a
/// command or as a result of URL scraping.
@@ -158,11 +192,11 @@ class Message extends Equatable {
/// Reserved field indicating when the message was created.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? createdAt;
final DateTime createdAt;
/// Reserved field indicating when the message was updated last time.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? updatedAt;
final DateTime updatedAt;
/// User who sent the message
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -384,7 +418,12 @@ class Message extends Equatable {
@JsonSerializable()
class TranslatedMessage extends Message {
/// Constructor used for json serialization
TranslatedMessage(this.i18n);
TranslatedMessage(this.i18n)
: super(
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
text: '',
type: '');
/// Create a new instance from a json
factory TranslatedMessage.fromJson(Map<String, dynamic>? json) =>
@@ -9,8 +9,8 @@ part of 'message.dart';
Message _$MessageFromJson(Map json) {
return Message(
id: json['id'] as String?,
text: json['text'] as String?,
type: json['type'] as String?,
text: json['text'] as String,
type: json['type'] as String,
attachments: (json['attachments'] as List<dynamic>?)
?.map((e) => Attachment.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
@@ -52,12 +52,8 @@ Message _$MessageFromJson(Map json) {
.toList(),
showInChannel: json['show_in_channel'] as bool?,
command: json['command'] as String?,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
user: json['user'] == null
? null
: User.fromJson((json['user'] as Map?)?.map(
@@ -12,19 +12,19 @@ part 'own_user.g.dart';
class OwnUser extends User {
/// Constructor used for json serialization
OwnUser({
this.devices,
this.mutes,
this.totalUnreadCount,
this.devices = const [],
this.mutes = const [],
this.totalUnreadCount = 0,
this.unreadChannels,
this.channelMutes,
String? id,
String? role,
DateTime? createdAt,
DateTime? updatedAt,
this.channelMutes = const [],
required String id,
required String role,
required DateTime createdAt,
required DateTime updatedAt,
DateTime? lastActive,
bool? online,
Map<String, dynamic>? extraData,
bool? banned,
bool online = false,
Map<String, dynamic> extraData = const {},
bool banned = false,
}) : super(
id: id,
role: role,
@@ -36,25 +36,45 @@ class OwnUser extends User {
banned: banned,
);
/// Create a temporary/throwaway user with an ID
OwnUser.temp({
required String id,
this.devices = const [],
this.mutes = const [],
this.totalUnreadCount = 0,
this.unreadChannels,
this.channelMutes = const [],
DateTime? lastActive,
bool online = false,
Map<String, dynamic> extraData = const {},
bool banned = false,
}) : super.temp(
id: id,
lastActive: lastActive,
online: online,
extraData: extraData,
banned: banned,
);
/// Create a new instance from a json
factory OwnUser.fromJson(Map<String, dynamic>? json) => _$OwnUserFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)!);
/// List of user devices
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final List<Device>? devices;
final List<Device> devices;
/// List of users muted by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final List<Mute>? mutes;
final List<Mute> mutes;
/// List of users muted by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final List<Mute>? channelMutes;
final List<Mute> channelMutes;
/// Total unread messages by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final int? totalUnreadCount;
final int totalUnreadCount;
/// Total unread channels by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -7,34 +7,29 @@ part of 'own_user.dart';
// **************************************************************************
OwnUser _$OwnUserFromJson(Map json) {
print(json);
return OwnUser(
devices: (json['devices'] as List<dynamic>?)
?.map((e) => Device.fromJson(Map<String, dynamic>.from(e as Map)))
devices: (json['devices'] as List<dynamic>)
.map((e) => Device.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
mutes: (json['mutes'] as List<dynamic>?)
?.map((e) => Mute.fromJson(Map<String, dynamic>.from(e as Map)))
mutes: (json['mutes'] as List<dynamic>)
.map((e) => Mute.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
totalUnreadCount: json['total_unread_count'] as int?,
totalUnreadCount: json['total_unread_count'] as int,
unreadChannels: json['unread_channels'] as int?,
channelMutes: (json['channel_mutes'] as List<dynamic>?)
?.map((e) => Mute.fromJson(Map<String, dynamic>.from(e as Map)))
channelMutes: (json['channel_mutes'] as List<dynamic>)
.map((e) => Mute.fromJson(Map<String, dynamic>.from(e as Map)))
.toList(),
id: json['id'] as String?,
role: json['role'] as String?,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
id: json['id'] as String,
role: json['role'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
lastActive: json['last_active'] == null
? null
: DateTime.parse(json['last_active'] as String),
online: json['online'] as bool?,
extraData: (json['extra_data'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
),
banned: json['banned'] as bool?,
online: json['online'] as bool,
extraData: Map<String, dynamic>.from(json['extra_data'] as Map),
banned: json['banned'] as bool,
);
}
@@ -55,7 +50,7 @@ Map<String, dynamic> _$OwnUserToJson(OwnUser instance) {
writeNotNull('last_active', readonly(instance.lastActive));
writeNotNull('online', readonly(instance.online));
writeNotNull('banned', readonly(instance.banned));
writeNotNull('extra_data', instance.extraData);
val['extra_data'] = instance.extraData;
writeNotNull('devices', readonly(instance.devices));
writeNotNull('mutes', readonly(instance.mutes));
writeNotNull('channel_mutes', readonly(instance.channelMutes));
@@ -10,13 +10,13 @@ class Reaction {
/// Constructor used for json serialization
Reaction({
this.messageId,
this.createdAt,
this.type,
this.user,
required this.createdAt,
required this.type,
required this.user,
String? userId,
this.score,
required this.score,
this.extraData,
}) : userId = userId ?? user?.id;
}) : userId = userId ?? user.id;
/// Create a new instance from a json
factory Reaction.fromJson(Map<String, dynamic>? json) => _$ReactionFromJson(
@@ -26,18 +26,18 @@ class Reaction {
final String? messageId;
/// The type of the reaction
final String? type;
final String type;
/// The date of the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? createdAt;
final DateTime createdAt;
/// The user that sent the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final User? user;
final User user;
/// The score of the reaction (ie. number of reactions sent)
final int? score;
final int score;
/// The userId that sent the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -9,17 +9,13 @@ part of 'reaction.dart';
Reaction _$ReactionFromJson(Map json) {
return Reaction(
messageId: json['message_id'] as String?,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
type: json['type'] as String?,
user: json['user'] == null
? null
: User.fromJson((json['user'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
createdAt: DateTime.parse(json['created_at'] as String),
type: json['type'] as String,
user: User.fromJson((json['user'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
userId: json['user_id'] as String?,
score: json['score'] as int?,
score: json['score'] as int,
extraData: (json['extra_data'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
),
@@ -8,22 +8,22 @@ part 'read.g.dart';
class Read {
/// Constructor used for json serialization
Read({
this.lastRead,
this.user,
this.unreadMessages,
required this.lastRead,
required this.user,
required this.unreadMessages,
});
/// Create a new instance from a json
factory Read.fromJson(Map<String, dynamic> json) => _$ReadFromJson(json);
/// Date of the read event
final DateTime? lastRead;
final DateTime lastRead;
/// User who sent the event
final User? user;
final User user;
/// Number of unread messages
final int? unreadMessages;
final int unreadMessages;
/// Serialize to json
Map<String, dynamic> toJson() => _$ReadToJson(this);
@@ -8,20 +8,16 @@ part of 'read.dart';
Read _$ReadFromJson(Map json) {
return Read(
lastRead: json['last_read'] == null
? null
: DateTime.parse(json['last_read'] as String),
user: json['user'] == null
? null
: User.fromJson((json['user'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
unreadMessages: json['unread_messages'] as int?,
lastRead: DateTime.parse(json['last_read'] as String),
user: User.fromJson((json['user'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
)),
unreadMessages: json['unread_messages'] as int,
);
}
Map<String, dynamic> _$ReadToJson(Read instance) => <String, dynamic>{
'last_read': instance.lastRead?.toIso8601String(),
'user': instance.user?.toJson(),
'last_read': instance.lastRead.toIso8601String(),
'user': instance.user.toJson(),
'unread_messages': instance.unreadMessages,
};
+38 -26
View File
@@ -8,17 +8,29 @@ part 'user.g.dart';
class User {
/// Constructor used for json serialization
User({
this.id,
this.role,
this.createdAt,
this.updatedAt,
required this.id,
required this.role,
required this.createdAt,
required this.updatedAt,
this.lastActive,
this.online,
this.extraData,
this.banned,
this.teams,
this.online = false,
this.extraData = const {},
this.banned = false,
this.teams = const [],
});
/// Use constructor for a temporary/throwaway user with an ID
User.temp({
required this.id,
this.role = '',
this.lastActive,
this.online = false,
this.extraData = const {},
this.banned = false,
this.teams = const [],
}) : createdAt = DateTime.now(),
updatedAt = DateTime.now();
/// Create a new instance from a json
factory User.fromJson(Map<String, dynamic>? json) => _$UserFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)!);
@@ -26,14 +38,14 @@ class User {
/// Use this named constructor to create a new user instance
User.init(
this.id, {
this.online,
this.extraData,
}) : createdAt = null,
updatedAt = null,
lastActive = null,
banned = null,
teams = null,
role = null;
this.online = false,
this.extraData = const {},
required this.createdAt,
required this.updatedAt,
this.teams = const [],
required this.role,
}) : lastActive = null,
banned = false;
/// Known top level fields.
/// Useful for [Serialization] methods.
@@ -49,23 +61,23 @@ class User {
];
/// User id
final String? id;
final String id;
/// User role
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final String? role;
final String role;
/// User role
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final List<String>? teams;
final List<String> teams;
/// Date of user creation
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? createdAt;
final DateTime createdAt;
/// Date of last user update
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final DateTime? updatedAt;
final DateTime updatedAt;
/// Date of last user connection
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
@@ -73,23 +85,23 @@ class User {
/// True if user is online
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final bool? online;
final bool online;
/// True if user is banned from the chat
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
final bool? banned;
final bool banned;
/// Map of custom user extraData
@JsonKey(includeIfNull: false)
final Map<String, dynamic>? extraData;
final Map<String, dynamic> extraData;
@override
int get hashCode => id.hashCode;
/// Shortcut for user name
String? get name =>
(extraData?.containsKey('name') == true && extraData!['name'] != '')
? extraData!['name']
(extraData.containsKey('name') == true && extraData['name'] != '')
? extraData['name']
: id;
@override
@@ -8,23 +8,17 @@ part of 'user.dart';
User _$UserFromJson(Map json) {
return User(
id: json['id'] as String?,
role: json['role'] as String?,
createdAt: json['created_at'] == null
? null
: DateTime.parse(json['created_at'] as String),
updatedAt: json['updated_at'] == null
? null
: DateTime.parse(json['updated_at'] as String),
id: json['id'] as String,
role: json['role'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
updatedAt: DateTime.parse(json['updated_at'] as String),
lastActive: json['last_active'] == null
? null
: DateTime.parse(json['last_active'] as String),
online: json['online'] as bool?,
extraData: (json['extra_data'] as Map?)?.map(
(k, e) => MapEntry(k as String, e),
),
banned: json['banned'] as bool?,
teams: (json['teams'] as List<dynamic>?)?.map((e) => e as String).toList(),
online: json['online'] as bool,
extraData: Map<String, dynamic>.from(json['extra_data'] as Map),
banned: json['banned'] as bool,
teams: (json['teams'] as List<dynamic>).map((e) => e as String).toList(),
);
}
@@ -46,6 +40,6 @@ Map<String, dynamic> _$UserToJson(User instance) {
writeNotNull('last_active', readonly(instance.lastActive));
writeNotNull('online', readonly(instance.online));
writeNotNull('banned', readonly(instance.banned));
writeNotNull('extra_data', instance.extraData);
val['extra_data'] = instance.extraData;
return val;
}