Conflicts:
	packages/stream_chat/lib/src/client/channel.dart
	packages/stream_chat_flutter/example/lib/main.dart
	packages/stream_chat_flutter/lib/src/message_list_view.dart
This commit is contained in:
Deven Joshi
2021-08-16 14:05:18 +05:30
98 changed files with 5094 additions and 2424 deletions
@@ -84,7 +84,7 @@ class ChannelApi {
/// Mark all channels for this user as read
Future<EmptyResponse> markAllRead() async {
final response = await _client.post('channels/read');
final response = await _client.post('/channels/read');
return EmptyResponse.fromJson(response.data);
}
@@ -7,11 +7,12 @@ import 'package:stream_chat/stream_chat.dart';
part 'own_user.g.dart';
/// The class that defines the own user model
/// This object can be found in [Event]
/// The class that defines the own user model.
///
/// This object can be found in [Event].
@JsonSerializable(createToJson: false)
class OwnUser extends User {
/// Constructor used for json serialization
/// Constructor used for json serialization.
OwnUser({
this.devices = const [],
this.mutes = const [],
@@ -20,6 +21,8 @@ class OwnUser extends User {
this.channelMutes = const [],
required String id,
String? role,
String? name,
String? image,
DateTime? createdAt,
DateTime? updatedAt,
DateTime? lastActive,
@@ -31,6 +34,8 @@ class OwnUser extends User {
}) : super(
id: id,
role: role,
name: name,
image: image,
createdAt: createdAt,
updatedAt: updatedAt,
lastActive: lastActive,
@@ -41,14 +46,16 @@ class OwnUser extends User {
language: language,
);
/// Create a new instance from a json
/// Create a new instance from json.
factory OwnUser.fromJson(Map<String, dynamic> json) => _$OwnUserFromJson(
Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// Create a new instance from [User] object
/// Create a new instance from [User] object.
factory OwnUser.fromUser(User user) => OwnUser(
id: user.id,
role: user.role,
name: user.name,
image: user.image,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
lastActive: user.lastActive,
@@ -64,6 +71,8 @@ class OwnUser extends User {
OwnUser copyWith({
String? id,
String? role,
String? name,
String? image,
DateTime? createdAt,
DateTime? updatedAt,
DateTime? lastActive,
@@ -80,8 +89,12 @@ class OwnUser extends User {
}) =>
OwnUser(
id: id ?? this.id,
banned: banned ?? this.banned,
role: role ?? this.role,
/* if null, it will be retrieved from extraData['name']*/
name: name,
/* if null, it will be retrieved from extraData['image']*/
image: image,
banned: banned ?? this.banned,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
lastActive: lastActive ?? this.lastActive,
@@ -101,16 +114,18 @@ class OwnUser extends User {
OwnUser merge(OwnUser? other) {
if (other == null) return this;
return copyWith(
id: other.id,
role: other.role,
name: other.name,
image: other.image,
banned: other.banned,
channelMutes: other.channelMutes,
createdAt: other.createdAt,
devices: other.devices,
extraData: other.extraData,
id: other.id,
lastActive: other.lastActive,
mutes: other.mutes,
online: other.online,
role: other.role,
teams: other.teams,
totalUnreadCount: other.totalUnreadCount,
unreadChannels: other.unreadChannels,
@@ -119,27 +134,28 @@ class OwnUser extends User {
);
}
/// List of user devices
/// List of user devices.
@JsonKey(includeIfNull: false, defaultValue: <Device>[])
final List<Device> devices;
/// List of users muted by the user
/// List of users muted by the user.
@JsonKey(includeIfNull: false, defaultValue: <Mute>[])
final List<Mute> mutes;
/// List of users muted by the user
/// List of users muted by the user.
@JsonKey(includeIfNull: false, defaultValue: <Mute>[])
final List<Mute> channelMutes;
/// Total unread messages by the user
/// Total unread messages by the user.
@JsonKey(includeIfNull: false, defaultValue: 0)
final int totalUnreadCount;
/// Total unread channels by the user
/// Total unread channels by the user.
@JsonKey(includeIfNull: false)
final int? unreadChannels;
/// Known top level fields.
///
/// Useful for [Serializer] methods.
static final topLevelFields = [
'devices',
@@ -4,29 +4,60 @@ import 'package:stream_chat/src/core/util/serializer.dart';
part 'user.g.dart';
/// The class that defines the user model
/// Class that defines a Stream Chat User.
@JsonSerializable()
class User extends Equatable {
/// Constructor used for json serialization
/// Creates a new user.
///
/// {@template name}
/// If an [name] is provided it will be set on [extraData] with a `key`
/// of 'name'.
///
/// For example:
/// ```dart
/// final user = User(id: 'id', name: 'Sahil Kumar');
/// print(user.name == user.extraData['name']); // true
/// ```
/// {@endtemplate}
///
/// {@template image}
/// If an [image] is provided it will be set on [extraData] with a `key`
/// of 'image'.
///
/// For example:
/// ```dart
/// final user = User(id: 'id', image: 'https://getstream.io/image.png');
/// print(user.image == user.extraData['image']); // true
/// ```
/// {@endtemplate}
User({
required this.id,
this.role,
String? name,
String? image,
DateTime? createdAt,
DateTime? updatedAt,
this.lastActive,
Map<String, Object?> extraData = const {},
this.online = false,
this.extraData = const {},
this.banned = false,
this.teams = const [],
this.language,
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
updatedAt = updatedAt ?? DateTime.now(),
/*For backwards compatibility, set 'name', 'image' in [extraData].*/
extraData = {
...extraData,
if (name != null) 'name': name,
if (image != null) 'image': image,
};
/// Create a new instance from a json
/// Create a new instance from json.
factory User.fromJson(Map<String, dynamic> json) =>
_$UserFromJson(Serializer.moveToExtraDataFromRoot(json, topLevelFields));
/// Known top level fields.
///
/// Useful for [Serializer] methods.
static const topLevelFields = [
'id',
@@ -40,57 +71,13 @@ class User extends Equatable {
'language',
];
/// User id
/// User id.
final String id;
/// User role
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? role;
/// User role
@JsonKey(
includeIfNull: false,
toJson: Serializer.readOnly,
defaultValue: <String>[],
)
final List<String> teams;
/// Date of user creation
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt;
/// Date of last user update
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt;
/// Date of last user connection
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? lastActive;
/// True if user is online
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool online;
/// True if user is banned from the chat
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool banned;
/// Map of custom user extraData
@JsonKey(
includeIfNull: false,
defaultValue: {},
)
final Map<String, Object?> extraData;
/// The language this user prefers.
/// Shortcut for user name.
///
/// Defaults to 'en'.
@JsonKey(includeIfNull: false)
final String? language;
/// Shortcut for user name
/// {@macro name}
@JsonKey(ignore: true)
String get name {
if (extraData.containsKey('name')) {
final name = extraData['name']! as String;
@@ -99,11 +86,62 @@ class User extends Equatable {
return id;
}
/// List of users to list of userIds
/// Shortcut for user image.
///
/// {@macro image}
@JsonKey(ignore: true)
String? get image => extraData['image'] as String?;
/// User role.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final String? role;
/// User teams
@JsonKey(
includeIfNull: false,
toJson: Serializer.readOnly,
defaultValue: <String>[],
)
final List<String> teams;
/// Date of user creation.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime createdAt;
/// Date of last user update.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime updatedAt;
/// Date of last user connection.
@JsonKey(includeIfNull: false, toJson: Serializer.readOnly)
final DateTime? lastActive;
/// True if user is online.
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool online;
/// True if user is banned from the chat.
@JsonKey(
includeIfNull: false, toJson: Serializer.readOnly, defaultValue: false)
final bool banned;
/// Map of custom user extraData.
@JsonKey(
includeIfNull: false,
defaultValue: {},
)
final Map<String, Object?> extraData;
/// The language this user prefers.
@JsonKey(includeIfNull: false)
final String? language;
/// List of users to list of userIds.
static List<String>? toIds(List<User>? users) =>
users?.map((u) => u.id).toList();
/// Serialize to json
/// Serialize to json.
Map<String, dynamic> toJson() => Serializer.moveFromExtraDataToRoot(
_$UserToJson(this),
);
@@ -112,6 +150,8 @@ class User extends Equatable {
User copyWith({
String? id,
String? role,
String? name,
String? image,
DateTime? createdAt,
DateTime? updatedAt,
DateTime? lastActive,
@@ -124,6 +164,10 @@ class User extends Equatable {
User(
id: id ?? this.id,
role: role ?? this.role,
/* if null, it will be retrieved from extraData['name']*/
name: name,
/* if null, it will be retrieved from extraData['image']*/
image: image,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
lastActive: lastActive ?? this.lastActive,
@@ -135,5 +179,5 @@ class User extends Equatable {
);
@override
List<Object?> get props => [id];
List<Object?> get props => [id, role];
}
@@ -19,8 +19,8 @@ User _$UserFromJson(Map<String, dynamic> json) {
lastActive: json['last_active'] == null
? null
: DateTime.parse(json['last_active'] as String),
online: json['online'] as bool? ?? false,
extraData: json['extra_data'] as Map<String, dynamic>? ?? {},
online: json['online'] as bool? ?? false,
banned: json['banned'] as bool? ?? false,
teams:
(json['teams'] as List<dynamic>?)?.map((e) => e as String).toList() ??