add extension_test.dart, serializer_test.dart and utils_test.dart
This commit is contained in:
@@ -4,7 +4,7 @@ import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.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/util/serialization.dart';
|
||||
import 'package:stream_chat/src/core/util/serializer.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
part 'attachment.g.dart';
|
||||
@@ -49,11 +49,11 @@ class Attachment extends Equatable {
|
||||
/// Create a new instance from a json
|
||||
factory Attachment.fromJson(Map<String, dynamic> json) =>
|
||||
_$AttachmentFromJson(
|
||||
Serialization.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
Serializer.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
|
||||
/// Create a new instance from a db data
|
||||
factory Attachment.fromData(Map<String, dynamic> json) =>
|
||||
_$AttachmentFromJson(Serialization.moveToExtraDataFromRoot(
|
||||
_$AttachmentFromJson(Serializer.moveToExtraDataFromRoot(
|
||||
json, topLevelFields + dbSpecificTopLevelFields));
|
||||
|
||||
///The attachment type based on the URL resource. This can be: audio,
|
||||
@@ -122,7 +122,7 @@ class Attachment extends Equatable {
|
||||
final String id;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static const topLevelFields = [
|
||||
'type',
|
||||
'title_link',
|
||||
@@ -145,7 +145,7 @@ class Attachment extends Equatable {
|
||||
];
|
||||
|
||||
/// Known db specific top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static const dbSpecificTopLevelFields = [
|
||||
'id',
|
||||
'upload_state',
|
||||
@@ -154,12 +154,12 @@ class Attachment extends Equatable {
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() =>
|
||||
Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this))
|
||||
Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this))
|
||||
..removeWhere((key, value) => dbSpecificTopLevelFields.contains(key));
|
||||
|
||||
/// Serialize to db data
|
||||
Map<String, dynamic> toData() =>
|
||||
Serialization.moveFromExtraDataToRoot(_$AttachmentToJson(this));
|
||||
Serializer.moveFromExtraDataToRoot(_$AttachmentToJson(this));
|
||||
|
||||
Attachment copyWith({
|
||||
String? id,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:json_annotation/json_annotation.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';
|
||||
|
||||
part 'channel_model.g.dart';
|
||||
@@ -37,7 +37,7 @@ class ChannelModel {
|
||||
/// Create a new instance from a json
|
||||
factory ChannelModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$ChannelModelFromJson(
|
||||
Serialization.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
Serializer.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
|
||||
/// The id of this channel
|
||||
final String id;
|
||||
@@ -46,15 +46,15 @@ class ChannelModel {
|
||||
final String type;
|
||||
|
||||
/// The cid of this channel
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final String cid;
|
||||
|
||||
/// The channel configuration data
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final ChannelConfig config;
|
||||
|
||||
/// The user that created this channel
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final User? createdBy;
|
||||
|
||||
/// True if this channel is frozen
|
||||
@@ -62,24 +62,24 @@ class ChannelModel {
|
||||
final bool frozen;
|
||||
|
||||
/// The date of the last message
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime? lastMessageAt;
|
||||
|
||||
/// The date of channel creation
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime createdAt;
|
||||
|
||||
/// The date of the last channel update
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime updatedAt;
|
||||
|
||||
/// The date of channel deletion
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime? deletedAt;
|
||||
|
||||
/// The count of this channel members
|
||||
@JsonKey(
|
||||
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0)
|
||||
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0)
|
||||
final int memberCount;
|
||||
|
||||
/// Map of custom channel extraData
|
||||
@@ -90,11 +90,11 @@ class ChannelModel {
|
||||
final Map<String, Object?> extraData;
|
||||
|
||||
/// The team the channel belongs to
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final String? team;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static const topLevelFields = [
|
||||
'id',
|
||||
'type',
|
||||
@@ -115,7 +115,7 @@ class ChannelModel {
|
||||
extraData.containsKey('name') ? extraData['name']! as String : cid;
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$ChannelModelToJson(this),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:json_annotation/json_annotation.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/util/serialization.dart';
|
||||
import 'package:stream_chat/src/core/util/serializer.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
|
||||
part 'event.g.dart';
|
||||
@@ -33,7 +33,7 @@ class Event {
|
||||
|
||||
/// Create a new instance from a json
|
||||
factory Event.fromJson(Map<String, dynamic> json) =>
|
||||
_$EventFromJson(Serialization.moveToExtraDataFromRoot(
|
||||
_$EventFromJson(Serializer.moveToExtraDataFromRoot(
|
||||
json,
|
||||
topLevelFields,
|
||||
));
|
||||
@@ -96,7 +96,7 @@ class Event {
|
||||
final Map<String, Object?> extraData;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static final topLevelFields = [
|
||||
'type',
|
||||
'cid',
|
||||
@@ -118,7 +118,7 @@ class Event {
|
||||
];
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$EventToJson(this),
|
||||
);
|
||||
|
||||
@@ -198,7 +198,7 @@ class EventChannel extends ChannelModel {
|
||||
|
||||
/// Create a new instance from a json
|
||||
factory EventChannel.fromJson(Map<String, dynamic> json) =>
|
||||
_$EventChannelFromJson(Serialization.moveToExtraDataFromRoot(
|
||||
_$EventChannelFromJson(Serializer.moveToExtraDataFromRoot(
|
||||
json,
|
||||
topLevelFields,
|
||||
));
|
||||
@@ -207,7 +207,7 @@ class EventChannel extends ChannelModel {
|
||||
final List<Member>? members;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static final topLevelFields = [
|
||||
'members',
|
||||
...ChannelModel.topLevelFields,
|
||||
@@ -215,7 +215,7 @@ class EventChannel extends ChannelModel {
|
||||
|
||||
/// Serialize to json
|
||||
@override
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$EventChannelToJson(this),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart';
|
||||
import 'package:json_annotation/json_annotation.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/util/serialization.dart';
|
||||
import 'package:stream_chat/src/core/util/serializer.dart';
|
||||
import 'package:stream_chat/src/core/models/user.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
@@ -81,7 +81,7 @@ class Message extends Equatable {
|
||||
|
||||
/// Create a new instance from a json
|
||||
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 is added.
|
||||
@@ -97,7 +97,7 @@ class Message extends Equatable {
|
||||
/// The message type
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: 'regular',
|
||||
)
|
||||
final String type;
|
||||
@@ -118,37 +118,37 @@ class Message extends Equatable {
|
||||
final List<User> mentionedUsers;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// The ID of the parent message, if the message is a thread reply.
|
||||
final String? parentId;
|
||||
|
||||
/// A quoted reply message
|
||||
@JsonKey(toJson: Serialization.readOnly)
|
||||
@JsonKey(toJson: Serializer.readOnly)
|
||||
final Message? quotedMessage;
|
||||
|
||||
/// The ID of the quoted message, if the message is a quoted reply.
|
||||
final String? quotedMessageId;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// Check if this message needs to show in the channel.
|
||||
@@ -165,25 +165,25 @@ class Message extends Equatable {
|
||||
/// If true the message is shadowed
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: false,
|
||||
)
|
||||
final bool shadowed;
|
||||
|
||||
/// A used command name.
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final String? command;
|
||||
|
||||
/// Reserved field indicating when the message was created.
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime createdAt;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// User who sent the message
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final User? user;
|
||||
|
||||
/// If true the message is pinned
|
||||
@@ -191,7 +191,7 @@ class Message extends Equatable {
|
||||
final bool pinned;
|
||||
|
||||
/// Reserved field indicating when the message was pinned
|
||||
@JsonKey(toJson: Serialization.readOnly)
|
||||
@JsonKey(toJson: Serializer.readOnly)
|
||||
final DateTime? pinnedAt;
|
||||
|
||||
/// Reserved field indicating when the message will expire
|
||||
@@ -200,7 +200,7 @@ class Message extends Equatable {
|
||||
final DateTime? pinExpires;
|
||||
|
||||
/// Reserved field indicating who pinned the message
|
||||
@JsonKey(toJson: Serialization.readOnly)
|
||||
@JsonKey(toJson: Serializer.readOnly)
|
||||
final User? pinnedBy;
|
||||
|
||||
/// Message custom extraData
|
||||
@@ -220,11 +220,11 @@ class Message extends Equatable {
|
||||
bool get isEphemeral => type == 'ephemeral';
|
||||
|
||||
/// Reserved field indicating when the message was deleted.
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime? deletedAt;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static const topLevelFields = [
|
||||
'id',
|
||||
'text',
|
||||
@@ -257,7 +257,7 @@ class Message extends Equatable {
|
||||
];
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$MessageToJson(this),
|
||||
);
|
||||
|
||||
@@ -412,14 +412,14 @@ class TranslatedMessage extends Message {
|
||||
/// Create a new instance from a json
|
||||
factory TranslatedMessage.fromJson(Map<String, dynamic> json) =>
|
||||
_$TranslatedMessageFromJson(
|
||||
Serialization.moveToExtraDataFromRoot(json, topLevelFields),
|
||||
Serializer.moveToExtraDataFromRoot(json, topLevelFields),
|
||||
);
|
||||
|
||||
/// A Map of
|
||||
final Map<String, String>? i18n;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static final topLevelFields = [
|
||||
'i18n',
|
||||
...Message.topLevelFields,
|
||||
@@ -427,7 +427,7 @@ class TranslatedMessage extends Message {
|
||||
|
||||
/// Serialize to json
|
||||
@override
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$TranslatedMessageToJson(this),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:json_annotation/json_annotation.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';
|
||||
|
||||
part 'mute.g.dart';
|
||||
@@ -20,19 +20,19 @@ class Mute {
|
||||
factory Mute.fromJson(Map<String, dynamic> json) => _$MuteFromJson(json);
|
||||
|
||||
/// The user that performed the muting action
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final User user;
|
||||
|
||||
/// The target user
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final ChannelModel channel;
|
||||
|
||||
/// The date in which the use was muted
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime createdAt;
|
||||
|
||||
/// The date of the last update
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime updatedAt;
|
||||
|
||||
/// Serialize to json
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:json_annotation/json_annotation.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/util/serialization.dart';
|
||||
import 'package:stream_chat/src/core/util/serializer.dart';
|
||||
import 'package:stream_chat/src/core/models/user.dart';
|
||||
|
||||
part 'own_user.g.dart';
|
||||
@@ -38,7 +38,7 @@ class OwnUser extends User {
|
||||
|
||||
/// Create a new instance from a json
|
||||
factory OwnUser.fromJson(Map<String, dynamic> json) => _$OwnUserFromJson(
|
||||
Serialization.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
Serializer.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
|
||||
/// Create a new instance from [User] object
|
||||
factory OwnUser.fromUser(User user) => OwnUser(
|
||||
@@ -55,35 +55,35 @@ class OwnUser extends User {
|
||||
/// List of user devices
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: <Device>[])
|
||||
final List<Device> devices;
|
||||
|
||||
/// List of users muted by the user
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: <Mute>[])
|
||||
final List<Mute> mutes;
|
||||
|
||||
/// List of users muted by the user
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: <Mute>[])
|
||||
final List<Mute> channelMutes;
|
||||
|
||||
/// Total unread messages by the user
|
||||
@JsonKey(
|
||||
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: 0)
|
||||
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: 0)
|
||||
final int totalUnreadCount;
|
||||
|
||||
/// Total unread channels by the user
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final int? unreadChannels;
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static final topLevelFields = [
|
||||
'devices',
|
||||
'mutes',
|
||||
@@ -95,7 +95,7 @@ class OwnUser extends User {
|
||||
|
||||
/// Serialize to json
|
||||
@override
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$OwnUserToJson(this),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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';
|
||||
|
||||
part 'reaction.g.dart';
|
||||
@@ -21,7 +21,7 @@ class Reaction {
|
||||
|
||||
/// Create a new instance from a json
|
||||
factory Reaction.fromJson(Map<String, dynamic> json) =>
|
||||
_$ReactionFromJson(Serialization.moveToExtraDataFromRoot(
|
||||
_$ReactionFromJson(Serializer.moveToExtraDataFromRoot(
|
||||
json,
|
||||
topLevelFields,
|
||||
));
|
||||
@@ -33,11 +33,11 @@ class Reaction {
|
||||
final String type;
|
||||
|
||||
/// The date of the reaction
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime createdAt;
|
||||
|
||||
/// The user that sent the reaction
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final User? user;
|
||||
|
||||
/// The score of the reaction (ie. number of reactions sent)
|
||||
@@ -45,7 +45,7 @@ class Reaction {
|
||||
final int score;
|
||||
|
||||
/// The userId that sent the reaction
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final String? userId;
|
||||
|
||||
/// Reaction custom extraData
|
||||
@@ -66,7 +66,7 @@ class Reaction {
|
||||
];
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$ReactionToJson(this),
|
||||
);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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';
|
||||
|
||||
@@ -22,10 +22,10 @@ class User {
|
||||
|
||||
/// Create a new instance from a json
|
||||
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(
|
||||
Serialization.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
Serializer.moveToExtraDataFromRoot(json, topLevelFields));
|
||||
|
||||
/// Known top level fields.
|
||||
/// Useful for [Serialization] methods.
|
||||
/// Useful for [Serializer] methods.
|
||||
static const topLevelFields = [
|
||||
'id',
|
||||
'role',
|
||||
@@ -41,36 +41,36 @@ class User {
|
||||
final String id;
|
||||
|
||||
/// User role
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final String? role;
|
||||
|
||||
/// User role
|
||||
@JsonKey(
|
||||
includeIfNull: false,
|
||||
toJson: Serialization.readOnly,
|
||||
toJson: Serializer.readOnly,
|
||||
defaultValue: <String>[])
|
||||
final List<String> teams;
|
||||
|
||||
/// Date of user creation
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime createdAt;
|
||||
|
||||
/// Date of last user update
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime updatedAt;
|
||||
|
||||
/// Date of last user connection
|
||||
@JsonKey(includeIfNull: false, toJson: Serialization.readOnly)
|
||||
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
|
||||
final DateTime? lastActive;
|
||||
|
||||
/// True if user is online
|
||||
@JsonKey(
|
||||
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false)
|
||||
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
|
||||
final bool online;
|
||||
|
||||
/// True if user is banned from the chat
|
||||
@JsonKey(
|
||||
includeIfNull: false, toJson: Serialization.readOnly, defaultValue: false)
|
||||
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
|
||||
final bool banned;
|
||||
|
||||
/// Map of custom user extraData
|
||||
@@ -102,7 +102,7 @@ class User {
|
||||
other is User && runtimeType == other.runtimeType && id == other.id;
|
||||
|
||||
/// Serialize to json
|
||||
Map<String, dynamic> toJson() => Serialization.moveFromExtraDataToRoot(
|
||||
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
|
||||
_$UserToJson(this),
|
||||
);
|
||||
|
||||
|
||||
@@ -9,10 +9,13 @@ extension IterableX<T> on Iterable<T?> {
|
||||
}
|
||||
|
||||
/// 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
|
||||
Map<K, V> get nullProtected =>
|
||||
Map.from(this)..removeWhere((key, value) => key == null || value == null);
|
||||
Map<K, V> get nullProtected {
|
||||
final nullProtected = {...this}
|
||||
..removeWhere((key, value) => key == null || value == null);
|
||||
return nullProtected.cast();
|
||||
}
|
||||
}
|
||||
|
||||
/// Useful extension functions for [String]
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
Null readonly(_) => null;
|
||||
|
||||
/// Helper class for serialization to and from json
|
||||
class Serialization {
|
||||
class Serializer {
|
||||
/// Used to avoid to serialize properties to json
|
||||
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');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user