add extension_test.dart, serializer_test.dart and utils_test.dart

This commit is contained in:
Sahil Kumar
2021-06-03 21:44:12 +05:30
parent 63d391ba7a
commit 7a95192093
13 changed files with 194 additions and 85 deletions
@@ -4,7 +4,7 @@ import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/action.dart'; import 'package:stream_chat/src/core/models/action.dart';
import 'package:stream_chat/src/core/models/attachment_file.dart'; import 'package:stream_chat/src/core/models/attachment_file.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
part 'attachment.g.dart'; part 'attachment.g.dart';
@@ -49,11 +49,11 @@ class Attachment extends Equatable {
/// Create a new instance from a json /// Create a new instance from a json
factory Attachment.fromJson(Map<String, dynamic> json) => factory Attachment.fromJson(Map<String, dynamic> json) =>
_$AttachmentFromJson( _$AttachmentFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)); Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// Create a new instance from a db data /// Create a new instance from a db data
factory Attachment.fromData(Map<String, dynamic> json) => factory Attachment.fromData(Map<String, dynamic> json) =>
_$AttachmentFromJson(Serialization.moveToExtraDataFromRoot( _$AttachmentFromJson(Serializer.moveToExtraDataFromRoot(
json, topLevelFields + dbSpecificTopLevelFields)); json, topLevelFields + dbSpecificTopLevelFields));
///The attachment type based on the URL resource. This can be: audio, ///The attachment type based on the URL resource. This can be: audio,
@@ -122,7 +122,7 @@ class Attachment extends Equatable {
final String id; final String id;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static const topLevelFields = [ static const topLevelFields = [
'type', 'type',
'title_link', 'title_link',
@@ -145,7 +145,7 @@ class Attachment extends Equatable {
]; ];
/// Known db specific top level fields. /// Known db specific top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static const dbSpecificTopLevelFields = [ static const dbSpecificTopLevelFields = [
'id', 'id',
'upload_state', 'upload_state',
@@ -154,12 +154,12 @@ class Attachment extends Equatable {
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Map<String, dynamic> toJson() =>
Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this)) Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this))
..removeWhere((key, value) => dbSpecificTopLevelFields.contains(key)); ..removeWhere((key, value) => dbSpecificTopLevelFields.contains(key));
/// Serialize to db data /// Serialize to db data
Map<String, dynamic> toData() => Map<String, dynamic> toData() =>
Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this)); Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this));
Attachment copyWith({ Attachment copyWith({
String? id, String? id,
@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/channel_config.dart'; import 'package:stream_chat/src/core/models/channel_config.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
part 'channel_model.g.dart'; part 'channel_model.g.dart';
@@ -37,7 +37,7 @@ class ChannelModel {
/// Create a new instance from a json /// Create a new instance from a json
factory ChannelModel.fromJson(Map<String, dynamic> json) => factory ChannelModel.fromJson(Map<String, dynamic> json) =>
_$ChannelModelFromJson( _$ChannelModelFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)); Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// The id of this channel /// The id of this channel
final String id; final String id;
@@ -46,15 +46,15 @@ class ChannelModel {
final String type; final String type;
/// The cid of this channel /// The cid of this channel
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String cid; final String cid;
/// The channel configuration data /// The channel configuration data
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final ChannelConfig config; final ChannelConfig config;
/// The user that created this channel /// The user that created this channel
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final User? createdBy; final User? createdBy;
/// True if this channel is frozen /// True if this channel is frozen
@@ -62,24 +62,24 @@ class ChannelModel {
final bool frozen; final bool frozen;
/// The date of the last message /// The date of the last message
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? lastMessageAt; final DateTime? lastMessageAt;
/// The date of channel creation /// The date of channel creation
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt; final DateTime createdAt;
/// The date of the last channel update /// The date of the last channel update
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt; final DateTime updatedAt;
/// The date of channel deletion /// The date of channel deletion
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? deletedAt; final DateTime? deletedAt;
/// The count of this channel members /// The count of this channel members
@JsonKey( @JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0)
final int memberCount; final int memberCount;
/// Map of custom channel extraData /// Map of custom channel extraData
@@ -90,11 +90,11 @@ class ChannelModel {
final Map<String, Object?> extraData; final Map<String, Object?> extraData;
/// The team the channel belongs to /// The team the channel belongs to
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? team; final String? team;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static const topLevelFields = [ static const topLevelFields = [
'id', 'id',
'type', 'type',
@@ -115,7 +115,7 @@ class ChannelModel {
extraData.containsKey('name') ? extraData['name']! as String : cid; extraData.containsKey('name') ? extraData['name']! as String : cid;
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$ChannelModelToJson(this), _$ChannelModelToJson(this),
); );
@@ -1,7 +1,7 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/channel_model.dart'; import 'package:stream_chat/src/core/models/channel_model.dart';
import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/message.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat/stream_chat.dart';
part 'event.g.dart'; part 'event.g.dart';
@@ -33,7 +33,7 @@ class Event {
/// Create a new instance from a json /// Create a new instance from a json
factory Event.fromJson(Map<String, dynamic> json) => factory Event.fromJson(Map<String, dynamic> json) =>
_$EventFromJson(Serialization.moveToExtraDataFromRoot( _$EventFromJson(Serializer.moveToExtraDataFromRoot(
json, json,
topLevelFields, topLevelFields,
)); ));
@@ -96,7 +96,7 @@ class Event {
final Map<String, Object?> extraData; final Map<String, Object?> extraData;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static final topLevelFields = [ static final topLevelFields = [
'type', 'type',
'cid', 'cid',
@@ -118,7 +118,7 @@ class Event {
]; ];
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$EventToJson(this), _$EventToJson(this),
); );
@@ -198,7 +198,7 @@ class EventChannel extends ChannelModel {
/// Create a new instance from a json /// Create a new instance from a json
factory EventChannel.fromJson(Map<String, dynamic> json) => factory EventChannel.fromJson(Map<String, dynamic> json) =>
_$EventChannelFromJson(Serialization.moveToExtraDataFromRoot( _$EventChannelFromJson(Serializer.moveToExtraDataFromRoot(
json, json,
topLevelFields, topLevelFields,
)); ));
@@ -207,7 +207,7 @@ class EventChannel extends ChannelModel {
final List<Member>? members; final List<Member>? members;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static final topLevelFields = [ static final topLevelFields = [
'members', 'members',
...ChannelModel.topLevelFields, ...ChannelModel.topLevelFields,
@@ -215,7 +215,7 @@ class EventChannel extends ChannelModel {
/// Serialize to json /// Serialize to json
@override @override
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$EventChannelToJson(this), _$EventChannelToJson(this),
); );
} }
@@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/attachment.dart'; import 'package:stream_chat/src/core/models/attachment.dart';
import 'package:stream_chat/src/core/models/reaction.dart'; import 'package:stream_chat/src/core/models/reaction.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@@ -81,7 +81,7 @@ class Message extends Equatable {
/// Create a new instance from a json /// Create a new instance from a json
factory Message.fromJson(Map<String, dynamic> json) => _$MessageFromJson( factory Message.fromJson(Map<String, dynamic> json) => _$MessageFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)); Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// The message ID. This is either created by Stream or set client side when /// The message ID. This is either created by Stream or set client side when
/// the message is added. /// the message is added.
@@ -97,7 +97,7 @@ class Message extends Equatable {
/// The message type /// The message type
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: 'regular', defaultValue: 'regular',
) )
final String type; final String type;
@@ -118,37 +118,37 @@ class Message extends Equatable {
final List<User> mentionedUsers; final List<User> mentionedUsers;
/// A map describing the count of number of every reaction /// A map describing the count of number of every reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final Map<String, int>? reactionCounts; final Map<String, int>? reactionCounts;
/// A map describing the count of score of every reaction /// A map describing the count of score of every reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final Map<String, int>? reactionScores; final Map<String, int>? reactionScores;
/// The latest reactions to the message created by any user. /// The latest reactions to the message created by any user.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final List<Reaction>? latestReactions; final List<Reaction>? latestReactions;
/// The reactions added to the message by the current user. /// The reactions added to the message by the current user.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final List<Reaction>? ownReactions; final List<Reaction>? ownReactions;
/// The ID of the parent message, if the message is a thread reply. /// The ID of the parent message, if the message is a thread reply.
final String? parentId; final String? parentId;
/// A quoted reply message /// A quoted reply message
@JsonKey(toJson: Serialization.readOnly) @JsonKey(toJson: Serializer.readOnly)
final Message? quotedMessage; final Message? quotedMessage;
/// The ID of the quoted message, if the message is a quoted reply. /// The ID of the quoted message, if the message is a quoted reply.
final String? quotedMessageId; final String? quotedMessageId;
/// Reserved field indicating the number of replies for this message. /// Reserved field indicating the number of replies for this message.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final int? replyCount; final int? replyCount;
/// Reserved field indicating the thread participants for this message. /// Reserved field indicating the thread participants for this message.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final List<User>? threadParticipants; final List<User>? threadParticipants;
/// Check if this message needs to show in the channel. /// Check if this message needs to show in the channel.
@@ -165,25 +165,25 @@ class Message extends Equatable {
/// If true the message is shadowed /// If true the message is shadowed
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: false, defaultValue: false,
) )
final bool shadowed; final bool shadowed;
/// A used command name. /// A used command name.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? command; final String? command;
/// Reserved field indicating when the message was created. /// Reserved field indicating when the message was created.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt; final DateTime createdAt;
/// Reserved field indicating when the message was updated last time. /// Reserved field indicating when the message was updated last time.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt; final DateTime updatedAt;
/// User who sent the message /// User who sent the message
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final User? user; final User? user;
/// If true the message is pinned /// If true the message is pinned
@@ -191,7 +191,7 @@ class Message extends Equatable {
final bool pinned; final bool pinned;
/// Reserved field indicating when the message was pinned /// Reserved field indicating when the message was pinned
@JsonKey(toJson: Serialization.readOnly) @JsonKey(toJson: Serializer.readOnly)
final DateTime? pinnedAt; final DateTime? pinnedAt;
/// Reserved field indicating when the message will expire /// Reserved field indicating when the message will expire
@@ -200,7 +200,7 @@ class Message extends Equatable {
final DateTime? pinExpires; final DateTime? pinExpires;
/// Reserved field indicating who pinned the message /// Reserved field indicating who pinned the message
@JsonKey(toJson: Serialization.readOnly) @JsonKey(toJson: Serializer.readOnly)
final User? pinnedBy; final User? pinnedBy;
/// Message custom extraData /// Message custom extraData
@@ -220,11 +220,11 @@ class Message extends Equatable {
bool get isEphemeral => type == 'ephemeral'; bool get isEphemeral => type == 'ephemeral';
/// Reserved field indicating when the message was deleted. /// Reserved field indicating when the message was deleted.
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? deletedAt; final DateTime? deletedAt;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static const topLevelFields = [ static const topLevelFields = [
'id', 'id',
'text', 'text',
@@ -257,7 +257,7 @@ class Message extends Equatable {
]; ];
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$MessageToJson(this), _$MessageToJson(this),
); );
@@ -412,14 +412,14 @@ class TranslatedMessage extends Message {
/// Create a new instance from a json /// Create a new instance from a json
factory TranslatedMessage.fromJson(Map<String, dynamic> json) => factory TranslatedMessage.fromJson(Map<String, dynamic> json) =>
_$TranslatedMessageFromJson( _$TranslatedMessageFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields), Serializer.moveToExtraDataFromRoot(json, topLevelFields),
); );
/// A Map of /// A Map of
final Map<String, String>? i18n; final Map<String, String>? i18n;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static final topLevelFields = [ static final topLevelFields = [
'i18n', 'i18n',
...Message.topLevelFields, ...Message.topLevelFields,
@@ -427,7 +427,7 @@ class TranslatedMessage extends Message {
/// Serialize to json /// Serialize to json
@override @override
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$TranslatedMessageToJson(this), _$TranslatedMessageToJson(this),
); );
} }
@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/channel_model.dart'; import 'package:stream_chat/src/core/models/channel_model.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
part 'mute.g.dart'; part 'mute.g.dart';
@@ -20,19 +20,19 @@ class Mute {
factory Mute.fromJson(Map<String, dynamic> json) => _$MuteFromJson(json); factory Mute.fromJson(Map<String, dynamic> json) => _$MuteFromJson(json);
/// The user that performed the muting action /// The user that performed the muting action
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final User user; final User user;
/// The target user /// The target user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final ChannelModel channel; final ChannelModel channel;
/// The date in which the use was muted /// The date in which the use was muted
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt; final DateTime createdAt;
/// The date of the last update /// The date of the last update
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt; final DateTime updatedAt;
/// Serialize to json /// Serialize to json
@@ -1,7 +1,7 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/models/device.dart'; import 'package:stream_chat/src/core/models/device.dart';
import 'package:stream_chat/src/core/models/mute.dart'; import 'package:stream_chat/src/core/models/mute.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
part 'own_user.g.dart'; part 'own_user.g.dart';
@@ -38,7 +38,7 @@ class OwnUser extends User {
/// Create a new instance from a json /// Create a new instance from a json
factory OwnUser.fromJson(Map<String, dynamic> json) => _$OwnUserFromJson( factory OwnUser.fromJson(Map<String, dynamic> json) => _$OwnUserFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)); Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// Create a new instance from [User] object /// Create a new instance from [User] object
factory OwnUser.fromUser(User user) => OwnUser( factory OwnUser.fromUser(User user) => OwnUser(
@@ -55,35 +55,35 @@ class OwnUser extends User {
/// List of user devices /// List of user devices
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: <Device>[]) defaultValue: <Device>[])
final List<Device> devices; final List<Device> devices;
/// List of users muted by the user /// List of users muted by the user
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: <Mute>[]) defaultValue: <Mute>[])
final List<Mute> mutes; final List<Mute> mutes;
/// List of users muted by the user /// List of users muted by the user
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: <Mute>[]) defaultValue: <Mute>[])
final List<Mute> channelMutes; final List<Mute> channelMutes;
/// Total unread messages by the user /// Total unread messages by the user
@JsonKey( @JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0) includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0)
final int totalUnreadCount; final int totalUnreadCount;
/// Total unread channels by the user /// Total unread channels by the user
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final int? unreadChannels; final int? unreadChannels;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static final topLevelFields = [ static final topLevelFields = [
'devices', 'devices',
'mutes', 'mutes',
@@ -95,7 +95,7 @@ class OwnUser extends User {
/// Serialize to json /// Serialize to json
@override @override
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$OwnUserToJson(this), _$OwnUserToJson(this),
); );
} }
@@ -1,5 +1,5 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
part 'reaction.g.dart'; part 'reaction.g.dart';
@@ -21,7 +21,7 @@ class Reaction {
/// Create a new instance from a json /// Create a new instance from a json
factory Reaction.fromJson(Map<String, dynamic> json) => factory Reaction.fromJson(Map<String, dynamic> json) =>
_$ReactionFromJson(Serialization.moveToExtraDataFromRoot( _$ReactionFromJson(Serializer.moveToExtraDataFromRoot(
json, json,
topLevelFields, topLevelFields,
)); ));
@@ -33,11 +33,11 @@ class Reaction {
final String type; final String type;
/// The date of the reaction /// The date of the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt; final DateTime createdAt;
/// The user that sent the reaction /// The user that sent the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final User? user; final User? user;
/// The score of the reaction (ie. number of reactions sent) /// The score of the reaction (ie. number of reactions sent)
@@ -45,7 +45,7 @@ class Reaction {
final int score; final int score;
/// The userId that sent the reaction /// The userId that sent the reaction
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? userId; final String? userId;
/// Reaction custom extraData /// Reaction custom extraData
@@ -66,7 +66,7 @@ class Reaction {
]; ];
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$ReactionToJson(this), _$ReactionToJson(this),
); );
@@ -1,5 +1,5 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/core/util/serialization.dart'; import 'package:stream_chat/src/core/util/serializer.dart';
part 'user.g.dart'; part 'user.g.dart';
@@ -22,10 +22,10 @@ class User {
/// Create a new instance from a json /// Create a new instance from a json
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson( factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(
Serialization.moveToExtraDataFromRoot(json, topLevelFields)); Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serializer] methods.
static const topLevelFields = [ static const topLevelFields = [
'id', 'id',
'role', 'role',
@@ -41,36 +41,36 @@ class User {
final String id; final String id;
/// User role /// User role
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? role; final String? role;
/// User role /// User role
@JsonKey( @JsonKey(
includeIfNull: false, includeIfNull: false,
toJson: Serialization.readOnly, toJson: Serializer.readOnly,
defaultValue: <String>[]) defaultValue: <String>[])
final List<String> teams; final List<String> teams;
/// Date of user creation /// Date of user creation
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt; final DateTime createdAt;
/// Date of last user update /// Date of last user update
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt; final DateTime updatedAt;
/// Date of last user connection /// Date of last user connection
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly) @JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? lastActive; final DateTime? lastActive;
/// True if user is online /// True if user is online
@JsonKey( @JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool online; final bool online;
/// True if user is banned from the chat /// True if user is banned from the chat
@JsonKey( @JsonKey(
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false) includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool banned; final bool banned;
/// Map of custom user extraData /// Map of custom user extraData
@@ -102,7 +102,7 @@ class User {
other is User && runtimeType == other.runtimeType && id == other.id; other is User && runtimeType == other.runtimeType && id == other.id;
/// Serialize to json /// Serialize to json
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot( Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$UserToJson(this), _$UserToJson(this),
); );
@@ -9,10 +9,13 @@ extension IterableX<T> on Iterable<T?> {
} }
/// Useful extension functions for [Map] /// Useful extension functions for [Map]
extension MapX<K, V> on Map<K, V> { extension MapX<K, V> on Map<K?, V?> {
/// Returns a new map with null keys or values removed /// Returns a new map with null keys or values removed
Map<K, V> get nullProtected => Map<K, V> get nullProtected {
Map.from(this)..removeWhere((key, value) => key == null || value == null); final nullProtected = {...this}
..removeWhere((key, value) => key == null || value == null);
return nullProtected.cast();
}
} }
/// Useful extension functions for [String] /// Useful extension functions for [String]
@@ -3,7 +3,7 @@
Null readonly(_) => null; Null readonly(_) => null;
/// Helper class for serialization to and from json /// Helper class for serialization to and from json
class Serialization { class Serializer {
/// Used to avoid to serialize properties to json /// Used to avoid to serialize properties to json
static const Function readOnly = readonly; static const Function readOnly = readonly;
@@ -0,0 +1,48 @@
import 'package:test/test.dart';
import 'package:stream_chat/src/core/util/extension.dart';
void main() {
test('`.withNullifyer` converts the type into non-nullable', () {
final items = ['A', 'B', null, 'D'];
expect(items, isA<Iterable<String?>>());
expect(items.length, 4);
final nullifiedItems = items.withNullifyer;
expect(nullifiedItems, isA<Iterable<String>>());
expect(nullifiedItems.length, 3);
});
test('`.nullProtected should remove all the null keys, value`', () {
final map = {'name': 'sahil', 'age': null, null: 'India'};
expect(map, isA<Map<String?, String?>>());
expect(map.length, 3);
final nullProtectedMap = map.nullProtected;
expect(nullProtectedMap, isA<Map<String, String>>());
expect(nullProtectedMap.length, 1);
});
group('mimeType', () {
test('should return null if `String` is not a filename', () {
const fileName = 'not-a-file-name';
final mimeType = fileName.mimeType;
expect(mimeType, isNull);
});
test('should return mimeType if string is a filename', () {
const fileName = 'dummyFileName.jpeg';
final mimeType = fileName.mimeType;
expect(mimeType, isNotNull);
expect(mimeType!.type, 'image');
expect(mimeType.subtype, 'jpeg');
});
test('should return `image/heic` if ends with `heic`', () {
const fileName = 'dummyFileName.heic';
final mimeType = fileName.mimeType;
expect(mimeType, isNotNull);
expect(mimeType!.type, 'image');
expect(mimeType.subtype, 'heic');
});
});
}
@@ -0,0 +1,43 @@
import 'package:stream_chat/src/core/util/serializer.dart';
import 'package:test/test.dart';
void main() {
group('Serializer', () {
test('moveKeysToMapInPlace', () {
final serializer = Serializer.moveToExtraDataFromRoot(
{
'test': 'test',
'name': 'Sahil',
'age': 22,
'country': 'India',
},
['test'],
);
expect(serializer, {
'test': 'test',
'extra_data': {
'name': 'Sahil',
'age': 22,
'country': 'India',
}
});
});
test('moveKeysToMapInPlace', () {
final serializer = Serializer.moveFromExtraDataToRoot({
'test': 'test',
'extra_data': {
'name': 'Sahil',
'age': 22,
'country': 'India',
}
});
expect(serializer, {
'test': 'test',
'name': 'Sahil',
'age': 22,
'country': 'India',
});
});
});
}
@@ -0,0 +1,15 @@
import 'package:stream_chat/src/core/util/utils.dart';
import 'package:test/test.dart';
void main() {
test('should generate a `randomId` of length 33', () {
final id = randomId(size: 33);
expect(id.length, 33);
});
test('should `generateHash` for the passed objects', () {
final objects = ['Sahil', 23, 'Flutter Engineer', 'India'];
final hash = generateHash(objects);
expect(hash, 'WyJTYWhpbCIsMjMsIkZsdXR0ZXIgRW5naW5lZXIiLCJJbmRpYSJd');
});
}