diff --git a/packages/stream_chat/example/lib/main.dart b/packages/stream_chat/example/lib/main.dart index 878b5fa1..d13dda4c 100644 --- a/packages/stream_chat/example/lib/main.dart +++ b/packages/stream_chat/example/lib/main.dart @@ -11,7 +11,7 @@ Future main() async { /// Please see the following for more information: /// https://getstream.io/chat/docs/ios_user_setup_and_tokens/ await client.connectUser( - User.temp( + User( id: 'cool-shadow-7', extraData: { 'image': diff --git a/packages/stream_chat/lib/src/api/channel.dart b/packages/stream_chat/lib/src/api/channel.dart index dd25976c..01d8399e 100644 --- a/packages/stream_chat/lib/src/api/channel.dart +++ b/packages/stream_chat/lib/src/api/channel.dart @@ -1312,7 +1312,7 @@ class ChannelClientState { updateChannelState(channelState!.copyWith( members: [ ...channelState!.members, - member, + member!, ], )); })); @@ -1323,7 +1323,7 @@ class ChannelClientState { final user = e.user; updateChannelState(channelState!.copyWith( members: List.from( - channelState!.members..removeWhere((m) => m!.userId == user!.id)), + channelState!.members..removeWhere((m) => m.userId == user!.id)), )); })); } @@ -1549,7 +1549,7 @@ class ChannelClientState { /// Channel members list List get members => _channelState!.members - .map((e) => e!.copyWith(user: _channel.client.state!.users[e.user!.id])) + .map((e) => e.copyWith(user: _channel.client.state!.users[e.user!.id])) .toList(); /// Channel members list as a stream @@ -1664,7 +1664,7 @@ class ChannelClientState { [], ]; - final newMembers = [ + final newMembers = [ ...updatedState.members, ]; diff --git a/packages/stream_chat/lib/src/client.dart b/packages/stream_chat/lib/src/client.dart index 147a08d0..816d780c 100644 --- a/packages/stream_chat/lib/src/client.dart +++ b/packages/stream_chat/lib/src/client.dart @@ -311,7 +311,7 @@ class StreamChatClient { httpClient.unlock(); - await connectUser(User.temp(id: userId), newToken); + await connectUser(User(id: userId), newToken); try { handler.resolve( @@ -753,7 +753,7 @@ class StreamChatClient { final users = channels .expand((it) => it.members) - .map((it) => it!.user) + .map((it) => it.user) .toList(growable: false); state!._updateUsers(users); @@ -956,7 +956,7 @@ class StreamChatClient { _anonymous = true; const uuid = Uuid(); - state!.user = OwnUser.temp(id: uuid.v4()); + state!.user = OwnUser(id: uuid.v4()); return connect().then((event) { _connectCompleter!.complete(event); diff --git a/packages/stream_chat/lib/src/db/chat_persistence_client.dart b/packages/stream_chat/lib/src/db/chat_persistence_client.dart index daabc26a..2646665a 100644 --- a/packages/stream_chat/lib/src/db/chat_persistence_client.dart +++ b/packages/stream_chat/lib/src/db/chat_persistence_client.dart @@ -76,7 +76,7 @@ abstract class ChatPersistenceClient { getPinnedMessagesByCid(cid, messagePagination: pinnedMessagePagination), ]); return ChannelState( - members: (data[0] as List?)!, + members: (data[0] as List?)!, read: (data[1] as List?)!, channel: data[2] as ChannelModel?, messages: (data[3] as List?)!, @@ -212,7 +212,7 @@ abstract class ChatPersistenceClient { ]) .expand((v) => v), ...cs.read.map((r) => r.user), - ...cs.members.map((m) => m!.user), + ...cs.members.map((m) => m.user), ]) .expand((it) => it) .where((it) => it != null); diff --git a/packages/stream_chat/lib/src/models/attachment.dart b/packages/stream_chat/lib/src/models/attachment.dart index 61f7afdf..7a302447 100644 --- a/packages/stream_chat/lib/src/models/attachment.dart +++ b/packages/stream_chat/lib/src/models/attachment.dart @@ -15,7 +15,7 @@ class Attachment extends Equatable { /// Constructor used for json serialization Attachment({ String? id, - required this.type, + String? type, this.titleLink, String? title, this.thumbUrl, @@ -38,6 +38,7 @@ class Attachment extends Equatable { UploadState? uploadState, }) : id = id ?? const Uuid().v4(), title = title ?? file?.name, + type = type ?? '', localUri = file?.path != null ? Uri.parse(file!.path!) : null { this.uploadState = uploadState ?? ((assetUrl != null || imageUrl != null) diff --git a/packages/stream_chat/lib/src/models/attachment.g.dart b/packages/stream_chat/lib/src/models/attachment.g.dart index ff371a5f..acee04fa 100644 --- a/packages/stream_chat/lib/src/models/attachment.g.dart +++ b/packages/stream_chat/lib/src/models/attachment.g.dart @@ -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?, diff --git a/packages/stream_chat/lib/src/models/channel_model.dart b/packages/stream_chat/lib/src/models/channel_model.dart index caffc30a..5a45f48d 100644 --- a/packages/stream_chat/lib/src/models/channel_model.dart +++ b/packages/stream_chat/lib/src/models/channel_model.dart @@ -12,33 +12,20 @@ class ChannelModel { ChannelModel({ this.id, this.type, - required this.cid, - required this.config, + this.cid = '', + ChannelConfig? config, this.createdBy, this.frozen = false, this.lastMessageAt, - required this.createdAt, - required this.updatedAt, + DateTime? createdAt, + DateTime? updatedAt, this.deletedAt, 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(); + }) : config = config ?? ChannelConfig(), + createdAt = createdAt ?? DateTime.now(), + updatedAt = updatedAt ?? DateTime.now(); /// Create a new instance from a json factory ChannelModel.fromJson(Map? json) => @@ -64,7 +51,7 @@ class ChannelModel { final User? createdBy; /// True if this channel is frozen - @JsonKey(includeIfNull: false) + @JsonKey(includeIfNull: false, defaultValue: false) final bool frozen; /// The date of the last message @@ -84,7 +71,8 @@ class ChannelModel { final DateTime? deletedAt; /// The count of this channel members - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) final int memberCount; /// Map of custom channel extraData diff --git a/packages/stream_chat/lib/src/models/channel_model.g.dart b/packages/stream_chat/lib/src/models/channel_model.g.dart index eb292e8d..19c4c085 100644 --- a/packages/stream_chat/lib/src/models/channel_model.g.dart +++ b/packages/stream_chat/lib/src/models/channel_model.g.dart @@ -11,20 +11,26 @@ ChannelModel _$ChannelModelFromJson(Map json) { id: json['id'] as String?, type: json['type'] as String?, cid: json['cid'] as String, - config: ChannelConfig.fromJson(json['config'] as Map), + config: json['config'] == null + ? null + : ChannelConfig.fromJson(json['config'] as Map), createdBy: json['created_by'] == null ? null : User.fromJson(json['created_by'] as Map?), - frozen: json['frozen'] as bool, + frozen: json['frozen'] as bool? ?? false, lastMessageAt: json['last_message_at'] == null ? null : DateTime.parse(json['last_message_at'] as String), - createdAt: DateTime.parse(json['created_at'] as String), - updatedAt: DateTime.parse(json['updated_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), 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? ?? 0, extraData: json['extra_data'] as Map?, team: json['team'] as String?, ); diff --git a/packages/stream_chat/lib/src/models/channel_state.dart b/packages/stream_chat/lib/src/models/channel_state.dart index 5f9f02bf..9d18f434 100644 --- a/packages/stream_chat/lib/src/models/channel_state.dart +++ b/packages/stream_chat/lib/src/models/channel_state.dart @@ -25,21 +25,26 @@ class ChannelState { final ChannelModel? channel; /// A paginated list of channel messages + @JsonKey(defaultValue: []) final List messages; /// A paginated list of channel members - final List members; + @JsonKey(defaultValue: []) + final List members; /// A paginated list of pinned messages + @JsonKey(defaultValue: []) final List pinnedMessages; /// The count of users watching the channel final int? watcherCount; /// A paginated list of users watching the channel + @JsonKey(defaultValue: []) final List watchers; /// The list of channel reads + @JsonKey(defaultValue: []) final List read; /// Create a new instance from a json @@ -53,7 +58,7 @@ class ChannelState { ChannelState copyWith({ ChannelModel? channel, List? messages, - List? members, + List? members, List? pinnedMessages, int? watcherCount, List? watchers, diff --git a/packages/stream_chat/lib/src/models/channel_state.g.dart b/packages/stream_chat/lib/src/models/channel_state.g.dart index 713d324c..67e98782 100644 --- a/packages/stream_chat/lib/src/models/channel_state.g.dart +++ b/packages/stream_chat/lib/src/models/channel_state.g.dart @@ -11,23 +11,27 @@ ChannelState _$ChannelStateFromJson(Map json) { channel: json['channel'] == null ? null : ChannelModel.fromJson(json['channel'] as Map?), - messages: (json['messages'] as List) - .map((e) => Message.fromJson(e as Map?)) - .toList(), - members: (json['members'] as List) - .map((e) => - e == null ? null : Member.fromJson(e as Map)) - .toList(), - pinnedMessages: (json['pinned_messages'] as List) - .map((e) => Message.fromJson(e as Map?)) - .toList(), + messages: (json['messages'] as List?) + ?.map((e) => Message.fromJson(e as Map?)) + .toList() ?? + [], + members: (json['members'] as List?) + ?.map((e) => Member.fromJson(e as Map)) + .toList() ?? + [], + pinnedMessages: (json['pinned_messages'] as List?) + ?.map((e) => Message.fromJson(e as Map?)) + .toList() ?? + [], watcherCount: json['watcher_count'] as int?, - watchers: (json['watchers'] as List) - .map((e) => User.fromJson(e as Map?)) - .toList(), - read: (json['read'] as List) - .map((e) => Read.fromJson(e as Map)) - .toList(), + watchers: (json['watchers'] as List?) + ?.map((e) => User.fromJson(e as Map?)) + .toList() ?? + [], + read: (json['read'] as List?) + ?.map((e) => Read.fromJson(e as Map)) + .toList() ?? + [], ); } @@ -35,7 +39,7 @@ Map _$ChannelStateToJson(ChannelState instance) => { 'channel': instance.channel?.toJson(), 'messages': instance.messages.map((e) => e.toJson()).toList(), - 'members': instance.members.map((e) => e?.toJson()).toList(), + 'members': instance.members.map((e) => e.toJson()).toList(), 'pinned_messages': instance.pinnedMessages.map((e) => e.toJson()).toList(), 'watcher_count': instance.watcherCount, diff --git a/packages/stream_chat/lib/src/models/device.dart b/packages/stream_chat/lib/src/models/device.dart index dc613b3c..24dcb678 100644 --- a/packages/stream_chat/lib/src/models/device.dart +++ b/packages/stream_chat/lib/src/models/device.dart @@ -7,7 +7,7 @@ part 'device.g.dart'; class Device { /// Constructor used for json serialization Device({ - required this.id, + this.id = '', this.pushProvider, }); diff --git a/packages/stream_chat/lib/src/models/event.g.dart b/packages/stream_chat/lib/src/models/event.g.dart index aa38a032..651c5837 100644 --- a/packages/stream_chat/lib/src/models/event.g.dart +++ b/packages/stream_chat/lib/src/models/event.g.dart @@ -85,7 +85,7 @@ EventChannel _$EventChannelFromJson(Map json) { createdBy: json['created_by'] == null ? null : User.fromJson(json['created_by'] as Map?), - frozen: json['frozen'] as bool, + frozen: json['frozen'] as bool? ?? false, lastMessageAt: json['last_message_at'] == null ? null : DateTime.parse(json['last_message_at'] as String), @@ -94,7 +94,7 @@ EventChannel _$EventChannelFromJson(Map json) { 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? ?? 0, extraData: json['extra_data'] as Map?, ); } diff --git a/packages/stream_chat/lib/src/models/member.dart b/packages/stream_chat/lib/src/models/member.dart index 735e4ff3..ad5f64c8 100644 --- a/packages/stream_chat/lib/src/models/member.dart +++ b/packages/stream_chat/lib/src/models/member.dart @@ -13,14 +13,15 @@ class Member { this.inviteAcceptedAt, this.inviteRejectedAt, this.invited = false, - required this.role, + this.role = '', this.userId, this.isModerator, - required this.createdAt, - required this.updatedAt, + DateTime? createdAt, + DateTime? updatedAt, this.banned = false, this.shadowBanned = false, - }); + }) : createdAt = createdAt ?? DateTime.now(), + updatedAt = updatedAt ?? DateTime.now(); /// Create a new instance from a json factory Member.fromJson(Map json) { @@ -40,9 +41,11 @@ class Member { final DateTime? inviteRejectedAt; /// True if the user has been invited to the channel + @JsonKey(defaultValue: false) final bool invited; /// The role of the user in the channel + @JsonKey(defaultValue: '') final String role; /// The id of the interested user @@ -52,9 +55,11 @@ class Member { final bool? isModerator; /// True if the member is banned from the channel + @JsonKey(defaultValue: false) final bool banned; /// True if the member is shadow banned from the channel + @JsonKey(defaultValue: false) final bool shadowBanned; /// The date of creation diff --git a/packages/stream_chat/lib/src/models/member.g.dart b/packages/stream_chat/lib/src/models/member.g.dart index 6c014f9f..df9b6641 100644 --- a/packages/stream_chat/lib/src/models/member.g.dart +++ b/packages/stream_chat/lib/src/models/member.g.dart @@ -17,14 +17,18 @@ 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? ?? false, + role: json['role'] as String? ?? '', userId: json['user_id'] as String?, isModerator: json['is_moderator'] 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, + 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? ?? false, + shadowBanned: json['shadow_banned'] as bool? ?? false, ); } diff --git a/packages/stream_chat/lib/src/models/message.dart b/packages/stream_chat/lib/src/models/message.dart index 8f260b1b..f7fcfbf1 100644 --- a/packages/stream_chat/lib/src/models/message.dart +++ b/packages/stream_chat/lib/src/models/message.dart @@ -46,41 +46,8 @@ class Message extends Equatable { /// Constructor used for json serialization Message({ String? id, - required this.text, - required this.type, - 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, - required this.createdAt, - required this.updatedAt, - 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(); - - /// Constructor for creating temporary/throwaway message with id and text - Message.temp({ - String? id, - required this.text, + this.text = '', + this.type = '', this.attachments, this.mentionedUsers, this.silent, @@ -96,6 +63,8 @@ class Message extends Equatable { this.threadParticipants, this.showInChannel, this.command, + DateTime? createdAt, + DateTime? updatedAt, this.user, this.pinned = false, this.pinnedAt, @@ -107,9 +76,8 @@ class Message extends Equatable { this.skipPush, }) : id = id ?? const Uuid().v4(), pinExpires = pinExpires?.toUtc(), - createdAt = DateTime.now(), - updatedAt = DateTime.now(), - type = ''; + createdAt = createdAt ?? DateTime.now(), + updatedAt = updatedAt ?? DateTime.now(); /// Create a new instance from a json factory Message.fromJson(Map? json) => _$MessageFromJson( @@ -418,12 +386,7 @@ class Message extends Equatable { @JsonSerializable() class TranslatedMessage extends Message { /// Constructor used for json serialization - TranslatedMessage(this.i18n) - : super( - createdAt: DateTime.now(), - updatedAt: DateTime.now(), - text: '', - type: ''); + TranslatedMessage(this.i18n) : super(); /// Create a new instance from a json factory TranslatedMessage.fromJson(Map? json) => diff --git a/packages/stream_chat/lib/src/models/message.g.dart b/packages/stream_chat/lib/src/models/message.g.dart index 54e485a3..fddbdd61 100644 --- a/packages/stream_chat/lib/src/models/message.g.dart +++ b/packages/stream_chat/lib/src/models/message.g.dart @@ -42,8 +42,12 @@ Message _$MessageFromJson(Map json) { .toList(), showInChannel: json['show_in_channel'] as bool?, command: json['command'] as String?, - createdAt: DateTime.parse(json['created_at'] as String), - updatedAt: DateTime.parse(json['updated_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), user: json['user'] == null ? null : User.fromJson(json['user'] as Map?), diff --git a/packages/stream_chat/lib/src/models/own_user.dart b/packages/stream_chat/lib/src/models/own_user.dart index 9ebc633b..2ba29aa0 100644 --- a/packages/stream_chat/lib/src/models/own_user.dart +++ b/packages/stream_chat/lib/src/models/own_user.dart @@ -17,10 +17,10 @@ class OwnUser extends User { this.totalUnreadCount = 0, this.unreadChannels, this.channelMutes = const [], - required String id, - required String role, - required DateTime createdAt, - required DateTime updatedAt, + String id = '', + String role = '', + DateTime? createdAt, + DateTime? updatedAt, DateTime? lastActive, bool online = false, Map extraData = const {}, @@ -36,44 +36,34 @@ 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 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? json) => _$OwnUserFromJson( Serialization.moveToExtraDataFromRoot(json, topLevelFields)!); /// List of user devices - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, + toJson: Serialization.readOnly, + defaultValue: []) final List devices; /// List of users muted by the user - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, + toJson: Serialization.readOnly, + defaultValue: []) final List mutes; /// List of users muted by the user - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, + toJson: Serialization.readOnly, + defaultValue: []) final List channelMutes; /// Total unread messages by the user - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) final int totalUnreadCount; /// Total unread channels by the user diff --git a/packages/stream_chat/lib/src/models/own_user.g.dart b/packages/stream_chat/lib/src/models/own_user.g.dart index 19629e42..5802060c 100644 --- a/packages/stream_chat/lib/src/models/own_user.g.dart +++ b/packages/stream_chat/lib/src/models/own_user.g.dart @@ -8,27 +8,34 @@ part of 'own_user.dart'; OwnUser _$OwnUserFromJson(Map json) { return OwnUser( - devices: (json['devices'] as List) - .map((e) => Device.fromJson(e as Map)) - .toList(), - mutes: (json['mutes'] as List) - .map((e) => Mute.fromJson(e as Map)) - .toList(), - totalUnreadCount: json['total_unread_count'] as int, + devices: (json['devices'] as List?) + ?.map((e) => Device.fromJson(e as Map)) + .toList() ?? + [], + mutes: (json['mutes'] as List?) + ?.map((e) => Mute.fromJson(e as Map)) + .toList() ?? + [], + totalUnreadCount: json['total_unread_count'] as int? ?? 0, unreadChannels: json['unread_channels'] as int?, - channelMutes: (json['channel_mutes'] as List) - .map((e) => Mute.fromJson(e as Map)) - .toList(), + channelMutes: (json['channel_mutes'] as List?) + ?.map((e) => Mute.fromJson(e as Map)) + .toList() ?? + [], 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), + 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), lastActive: json['last_active'] == null ? null : DateTime.parse(json['last_active'] as String), - online: json['online'] as bool, + online: json['online'] as bool? ?? false, extraData: json['extra_data'] as Map, - banned: json['banned'] as bool, + banned: json['banned'] as bool? ?? false, ); } diff --git a/packages/stream_chat/lib/src/models/read.dart b/packages/stream_chat/lib/src/models/read.dart index 3fccc20b..ef20eb6e 100644 --- a/packages/stream_chat/lib/src/models/read.dart +++ b/packages/stream_chat/lib/src/models/read.dart @@ -23,6 +23,7 @@ class Read { final User user; /// Number of unread messages + @JsonKey(defaultValue: 0) final int unreadMessages; /// Serialize to json diff --git a/packages/stream_chat/lib/src/models/read.g.dart b/packages/stream_chat/lib/src/models/read.g.dart index 158f6f8f..82aeba22 100644 --- a/packages/stream_chat/lib/src/models/read.g.dart +++ b/packages/stream_chat/lib/src/models/read.g.dart @@ -10,7 +10,7 @@ Read _$ReadFromJson(Map json) { return Read( lastRead: DateTime.parse(json['last_read'] as String), user: User.fromJson(json['user'] as Map?), - unreadMessages: json['unread_messages'] as int, + unreadMessages: json['unread_messages'] as int? ?? 0, ); } diff --git a/packages/stream_chat/lib/src/models/user.dart b/packages/stream_chat/lib/src/models/user.dart index 1488696a..ab690e8f 100644 --- a/packages/stream_chat/lib/src/models/user.dart +++ b/packages/stream_chat/lib/src/models/user.dart @@ -8,28 +8,17 @@ part 'user.g.dart'; class User { /// Constructor used for json serialization User({ - required this.id, - required this.role, - required this.createdAt, - required this.updatedAt, - this.lastActive, - 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.id = '', this.role = '', + DateTime? createdAt, + DateTime? updatedAt, this.lastActive, this.online = false, this.extraData = const {}, this.banned = false, this.teams = const [], - }) : createdAt = DateTime.now(), - updatedAt = DateTime.now(); + }) : createdAt = createdAt ?? DateTime.now(), + updatedAt = updatedAt ?? DateTime.now(); /// Create a new instance from a json factory User.fromJson(Map? json) => _$UserFromJson( @@ -64,11 +53,15 @@ class User { final String id; /// User role - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, toJson: Serialization.readOnly, defaultValue: '') final String role; /// User role - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, + toJson: Serialization.readOnly, + defaultValue: []) final List teams; /// Date of user creation @@ -84,11 +77,13 @@ class User { final DateTime? lastActive; /// True if user is online - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) final bool online; /// True if user is banned from the chat - @JsonKey(includeIfNull: false, toJson: Serialization.readOnly) + @JsonKey( + includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) final bool banned; /// Map of custom user extraData diff --git a/packages/stream_chat/lib/src/models/user.g.dart b/packages/stream_chat/lib/src/models/user.g.dart index 457999a6..18408d76 100644 --- a/packages/stream_chat/lib/src/models/user.g.dart +++ b/packages/stream_chat/lib/src/models/user.g.dart @@ -9,16 +9,22 @@ part of 'user.dart'; User _$UserFromJson(Map json) { return User( 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), + 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), lastActive: json['last_active'] == null ? null : DateTime.parse(json['last_active'] as String), - online: json['online'] as bool, + online: json['online'] as bool? ?? false, extraData: json['extra_data'] as Map, - banned: json['banned'] as bool, - teams: (json['teams'] as List).map((e) => e as String).toList(), + banned: json['banned'] as bool? ?? false, + teams: + (json['teams'] as List?)?.map((e) => e as String).toList() ?? + [], ); } diff --git a/packages/stream_chat/test/src/api/channel_test.dart b/packages/stream_chat/test/src/api/channel_test.dart index 97d4fa6f..5eee577e 100644 --- a/packages/stream_chat/test/src/api/channel_test.dart +++ b/packages/stream_chat/test/src/api/channel_test.dart @@ -35,7 +35,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'hey', id: 'test'); + final message = Message(text: 'hey', id: 'test'); when( () => mockDio.post( @@ -168,8 +168,7 @@ void main() { requestOptions: FakeRequestOptions(), )); - await channelClient.sendAction( - Message.temp(id: 'messageid', text: ''), data); + await channelClient.sendAction(Message(id: 'messageid'), data); verify(() => mockDio.post('/messages/messageid/action', data: { 'id': 'testid', @@ -335,7 +334,7 @@ void main() { final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'Hello'); + final message = Message(text: 'Hello'); expect( () => channelClient.pinMessage(message, 'InvalidType'), @@ -355,7 +354,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'Hello', id: 'test'); + final message = Message(text: 'Hello', id: 'test'); when( () => mockDio.post( @@ -389,7 +388,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'Hello', id: 'test'); + final message = Message(text: 'Hello', id: 'test'); when( () => mockDio.post( @@ -567,7 +566,7 @@ void main() { tokenProvider: (_) async => '', ); - client.state?.user = OwnUser.temp(id: 'test-id'); + client.state?.user = OwnUser(id: 'test-id'); final channelClient = client.channel('messaging', id: 'testid'); const reactionType = 'test'; @@ -591,13 +590,8 @@ void main() { ); await channelClient.sendReaction( - Message.temp( + Message( id: 'messageid', - text: '', - reactionCounts: const {}, - reactionScores: const {}, - latestReactions: const [], - ownReactions: const [], ), reactionType, ); @@ -623,7 +617,7 @@ void main() { tokenProvider: (_) async => '', ); - client.state?.user = OwnUser.temp(id: 'test-id'); + client.state?.user = OwnUser(id: 'test-id'); final channelClient = client.channel('messaging', id: 'testid'); @@ -638,19 +632,14 @@ void main() { ); await channelClient.deleteReaction( - Message.temp( + Message( id: 'messageid', - text: '', - reactionCounts: const {}, - reactionScores: const {}, - latestReactions: const [], - ownReactions: const [], ), Reaction( type: 'test', createdAt: DateTime.now(), score: 0, - user: User.temp( + user: User( id: client.state?.user?.id ?? '', ), ), @@ -709,7 +698,7 @@ void main() { ); final channelClient = client.channel('messaging', id: 'testid'); final members = ['vishal']; - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( @@ -743,7 +732,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( @@ -2091,7 +2080,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( @@ -2190,7 +2179,7 @@ void main() { tokenProvider: (_) async => '', ); final channelClient = client.channel('messaging', id: 'testid'); - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( @@ -2225,7 +2214,7 @@ void main() { ); final channelClient = client.channel('messaging', id: 'testid'); final members = ['vishal']; - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( @@ -2259,7 +2248,7 @@ void main() { ); final channelClient = client.channel('messaging', id: 'testid'); final members = ['vishal']; - final message = Message.temp(text: 'test'); + final message = Message(text: 'test'); when( () => mockDio.post( diff --git a/packages/stream_chat/test/src/api/websocket_test.dart b/packages/stream_chat/test/src/api/websocket_test.dart index 8f16a25d..156b203e 100644 --- a/packages/stream_chat/test/src/api/websocket_test.dart +++ b/packages/stream_chat/test/src/api/websocket_test.dart @@ -38,7 +38,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -76,7 +76,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -112,7 +112,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -150,7 +150,7 @@ void main() { final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -184,7 +184,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -229,7 +229,7 @@ void main() { Logger.root.level = Level.ALL; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -273,7 +273,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, @@ -310,7 +310,7 @@ void main() { final connectFunc = MockFunctions().connectFunc; final ws = WebSocket( baseUrl: 'baseurl', - user: User.temp(id: 'testid'), + user: User(id: 'testid'), logger: Logger('ws'), connectParams: {'test': 'true'}, connectPayload: {'payload': 'test'}, diff --git a/packages/stream_chat/test/src/client_test.dart b/packages/stream_chat/test/src/client_test.dart index 775d68b0..8f95b3a4 100644 --- a/packages/stream_chat/test/src/client_test.dart +++ b/packages/stream_chat/test/src/client_test.dart @@ -479,7 +479,7 @@ void main() { final client = StreamChatClient('api-key', httpClient: mockDio); - expect(() => client.connectUserWithProvider(User.temp(id: 'test-id')), + expect(() => client.connectUserWithProvider(User(id: 'test-id')), throwsA(isA())); }); @@ -517,7 +517,7 @@ void main() { when(() => mockDio.interceptors).thenReturn(Interceptors()); final client = StreamChatClient('api-key', httpClient: mockDio); - final user = User.temp(id: 'test-id'); + final user = User(id: 'test-id'); final data = { 'users': {user.id: user.toJson()}, }; @@ -542,8 +542,8 @@ void main() { when(() => mockDio.interceptors).thenReturn(Interceptors()); final client = StreamChatClient('api-key', httpClient: mockDio); - final user = User.temp(id: 'test-id'); - final user2 = User.temp(id: 'test-id2'); + final user = User(id: 'test-id'); + final user2 = User(id: 'test-id2'); final data = { 'users': { @@ -739,10 +739,6 @@ void main() { final client = StreamChatClient('api-key', httpClient: mockDio); final message = Message( id: 'test', - updatedAt: DateTime.now(), - createdAt: DateTime.now(), - text: '', - type: '', ); when( @@ -781,7 +777,7 @@ void main() { ), ); - await client.deleteMessage(Message.temp(id: messageId, text: '')); + await client.deleteMessage(Message(id: messageId, text: '')); verify(() => mockDio.delete('/messages/$messageId')).called(1); }); @@ -1101,7 +1097,7 @@ void main() { ); test('should throw argument error', () { - final message = Message.temp(text: 'Hello'); + final message = Message(text: 'Hello'); expect( () => client.pinMessage(message, 'InvalidType'), throwsArgumentError, @@ -1110,7 +1106,7 @@ void main() { test('should complete successfully', () async { const timeout = 30; - final message = Message.temp(text: 'Hello'); + final message = Message(text: 'Hello'); when( () => mockDio.post( @@ -1132,7 +1128,7 @@ void main() { }); test('should unpin message successfully', () async { - final message = Message.temp(text: 'Hello'); + final message = Message(text: 'Hello'); when( () => mockDio.post( diff --git a/packages/stream_chat/test/src/models/channel_state_test.dart b/packages/stream_chat/test/src/models/channel_state_test.dart index 79b8b0fa..98d08596 100644 --- a/packages/stream_chat/test/src/models/channel_state_test.dart +++ b/packages/stream_chat/test/src/models/channel_state_test.dart @@ -851,8 +851,8 @@ void main() { expect(channelState.channel?.type, 'team'); expect(channelState.channel?.config, isA()); expect(channelState.channel?.config, isNotNull); - expect(channelState.channel?.config?.commands, hasLength(1)); - expect(channelState.channel?.config?.commands![0], isA()); + expect(channelState.channel?.config.commands, hasLength(1)); + expect(channelState.channel?.config.commands![0], isA()); expect(channelState.channel?.lastMessageAt, DateTime.parse('2020-01-30T13:43:41.062362Z')); expect(channelState.channel?.createdAt, @@ -868,13 +868,13 @@ void main() { 'https://cdn.chrisshort.net/testing-certificate-chains-in-go/GOPHER_MIC_DROP.png', ); expect(channelState.messages, hasLength(25)); - expect(channelState.messages![0], isA()); - expect(channelState.messages![0], isNotNull); + expect(channelState.messages[0], isA()); + expect(channelState.messages[0], isNotNull); expect( - channelState.messages![0].createdAt, + channelState.messages[0].createdAt, DateTime.parse('2020-01-29T03:23:02.843948Z'), ); - expect(channelState.messages![0].user, isA()); + expect(channelState.messages[0].user, isA()); expect(channelState.watcherCount, 5); }); @@ -889,8 +889,8 @@ void main() { "image": "https://cdn.chrisshort.net/testing-certificate-chains-in-go/GOPHER_MIC_DROP.png", "example": 1 }, - "watchers": null, - "read": null, + "watchers": [], + "read": [], "messages": [ { "id": "dry-meadow-0-2b73cc8b-cd86-4a01-8d40-bd82ad07a030", diff --git a/packages/stream_chat/test/src/models/channel_test.dart b/packages/stream_chat/test/src/models/channel_test.dart index 6d0475e0..d2d78fd5 100644 --- a/packages/stream_chat/test/src/models/channel_test.dart +++ b/packages/stream_chat/test/src/models/channel_test.dart @@ -25,7 +25,7 @@ void main() { }); test('should serialize to json correctly', () { - final channel = ChannelModel.temp( + final channel = ChannelModel( type: 'type', id: 'id', cid: 'a:a', @@ -34,12 +34,12 @@ void main() { expect( channel.toJson(), - {'id': 'id', 'type': 'type', 'name': 'cool'}, + {'id': 'id', 'type': 'type', 'frozen': false, 'name': 'cool'}, ); }); test('should serialize to json correctly when frozen is provided', () { - final channel = ChannelModel.temp( + final channel = ChannelModel( type: 'type', id: 'id', cid: 'a:a', diff --git a/packages/stream_chat/test/src/models/event_test.dart b/packages/stream_chat/test/src/models/event_test.dart index 4e657bdb..a7c669df 100644 --- a/packages/stream_chat/test/src/models/event_test.dart +++ b/packages/stream_chat/test/src/models/event_test.dart @@ -51,12 +51,12 @@ void main() { test('should serialize to json correctly', () { final event = Event( - user: User.temp(id: 'id'), + user: User(id: 'id'), type: 'type', cid: 'cid', connectionId: 'connectionId', createdAt: DateTime.parse('2020-01-29T03:22:47.63613Z'), - me: OwnUser.temp(id: 'id2'), + me: OwnUser(id: 'id2'), totalUnreadCount: 1, unreadChannels: 1, online: true, diff --git a/packages/stream_chat/test/src/models/message_test.dart b/packages/stream_chat/test/src/models/message_test.dart index 72b9ba09..0898e902 100644 --- a/packages/stream_chat/test/src/models/message_test.dart +++ b/packages/stream_chat/test/src/models/message_test.dart @@ -97,7 +97,7 @@ void main() { }); test('should serialize to json correctly', () { - final message = Message.temp( + final message = Message( id: '4637f7e4-a06b-42db-ba5a-8d8270dd926f', text: 'https://giphy.com/gifs/the-lion-king-live-action-5zvN79uTGfLMOVfQaA', diff --git a/packages/stream_chat/test/src/models/reaction_test.dart b/packages/stream_chat/test/src/models/reaction_test.dart index 2a884962..49d226ea 100644 --- a/packages/stream_chat/test/src/models/reaction_test.dart +++ b/packages/stream_chat/test/src/models/reaction_test.dart @@ -34,7 +34,7 @@ void main() { expect(reaction.type, 'wow'); expect( reaction.user.toJson(), - User.temp(id: '2de0297c-f3f2-489d-b930-ef77342edccf', extraData: { + User(id: '2de0297c-f3f2-489d-b930-ef77342edccf', extraData: { 'image': 'https://randomuser.me/api/portraits/women/45.jpg', 'name': 'Daisy Morgan' }).toJson(), @@ -49,7 +49,7 @@ void main() { messageId: '76cd8c82-b557-4e48-9d12-87995d3a0e04', createdAt: DateTime.parse('2020-01-28T22:17:31.108742Z'), type: 'wow', - user: User.temp(id: '2de0297c-f3f2-489d-b930-ef77342edccf', extraData: { + user: User(id: '2de0297c-f3f2-489d-b930-ef77342edccf', extraData: { 'image': 'https://randomuser.me/api/portraits/women/45.jpg', 'name': 'Daisy Morgan' }), diff --git a/packages/stream_chat/test/src/models/read_test.dart b/packages/stream_chat/test/src/models/read_test.dart index 24b56cd8..66efe809 100644 --- a/packages/stream_chat/test/src/models/read_test.dart +++ b/packages/stream_chat/test/src/models/read_test.dart @@ -19,14 +19,14 @@ void main() { test('should parse json correctly', () { final read = Read.fromJson(json.decode(jsonExample)); expect(read.lastRead, DateTime.parse('2020-01-28T22:17:30.966485504Z')); - expect(read.user?.id, 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'); + expect(read.user.id, 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'); expect(read.unreadMessages, 10); }); test('should serialize to json correctly', () { final read = Read( lastRead: DateTime.parse('2020-01-28T22:17:30.966485504Z'), - user: User.temp(id: 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'), + user: User(id: 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'), unreadMessages: 10, ); diff --git a/packages/stream_chat/test/src/models/user_test.dart b/packages/stream_chat/test/src/models/user_test.dart index b28888c4..8f56f7e1 100644 --- a/packages/stream_chat/test/src/models/user_test.dart +++ b/packages/stream_chat/test/src/models/user_test.dart @@ -18,7 +18,7 @@ void main() { }); test('should serialize to json correctly', () { - final user = User.temp( + final user = User( id: 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e', role: 'abc', );