diff --git a/packages/stream_chat/lib/src/core/models/own_user.dart b/packages/stream_chat/lib/src/core/models/own_user.dart index 7ceffbfa..679abb47 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.dart @@ -3,6 +3,7 @@ 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/user.dart'; import 'package:stream_chat/src/core/util/serializer.dart'; +import 'package:stream_chat/stream_chat.dart'; part 'own_user.g.dart'; @@ -25,6 +26,7 @@ class OwnUser extends User { bool online = false, Map extraData = const {}, bool banned = false, + List teams = const [], }) : super( id: id, role: role, @@ -34,6 +36,7 @@ class OwnUser extends User { online: online, extraData: extraData, banned: banned, + teams: teams, ); /// Create a new instance from a json @@ -50,8 +53,69 @@ class OwnUser extends User { online: user.online, banned: user.banned, extraData: user.extraData, + teams: user.teams, ); + /// Creates a copy of [OwnUser] with specified attributes overridden. + @override + OwnUser copyWith({ + String? id, + String? role, + DateTime? createdAt, + DateTime? updatedAt, + DateTime? lastActive, + bool? online, + Map? extraData, + bool? banned, + List? teams, + List? channelMutes, + List? devices, + List? mutes, + int? totalUnreadCount, + int? unreadChannels, + }) => + OwnUser( + id: id ?? this.id, + banned: banned ?? this.banned, + role: role ?? this.role, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + lastActive: lastActive ?? this.lastActive, + online: online ?? this.online, + extraData: extraData ?? this.extraData, + teams: teams ?? this.teams, + channelMutes: channelMutes ?? this.channelMutes, + devices: devices ?? this.devices, + mutes: mutes ?? this.mutes, + totalUnreadCount: totalUnreadCount ?? this.totalUnreadCount, + unreadChannels: unreadChannels ?? this.unreadChannels, + ); + + /// Returns a new [OwnUser] that is a combination of this ownUser + /// and the given [other] ownUser. + OwnUser merge(OwnUser? other) { + if (other == null) { + return this; + } + + return copyWith( + 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, + updatedAt: other.updatedAt, + ); + } + /// List of user devices @JsonKey(includeIfNull: false, defaultValue: []) final List devices; diff --git a/packages/stream_chat/lib/src/core/models/user.dart b/packages/stream_chat/lib/src/core/models/user.dart index c2960af9..1360d14e 100644 --- a/packages/stream_chat/lib/src/core/models/user.dart +++ b/packages/stream_chat/lib/src/core/models/user.dart @@ -47,9 +47,10 @@ class User extends Equatable { /// User role @JsonKey( - includeIfNull: false, - toJson: Serializer.readOnly, - defaultValue: []) + includeIfNull: false, + toJson: Serializer.readOnly, + defaultValue: [], + ) final List teams; /// Date of user creation diff --git a/packages/stream_chat/test/src/core/models/own_user_test.dart b/packages/stream_chat/test/src/core/models/own_user_test.dart index edaf022e..7aef859d 100644 --- a/packages/stream_chat/test/src/core/models/own_user_test.dart +++ b/packages/stream_chat/test/src/core/models/own_user_test.dart @@ -41,5 +41,43 @@ void main() { expect(ownUser.banned, user.banned); expect(ownUser.extraData, user.extraData); }); + + test('copyWith', () { + final user = OwnUser.fromJson(jsonFixture('own_user.json')); + var newUser = user.copyWith(); + + expect(newUser.id, user.id); + expect(newUser.role, user.role); + expect(newUser.name, user.name); + + newUser = user.copyWith( + id: 'test', + role: 'test', + extraData: { + 'name': 'test', + }, + ); + + expect(newUser.id, 'test'); + expect(newUser.role, 'test'); + expect(newUser.name, 'test'); + }); + + test('merge', () { + final user = OwnUser.fromJson(jsonFixture('own_user.json')); + final newUser = user.merge(OwnUser( + id: 'test', + role: 'test', + extraData: const { + 'name': 'test', + }, + banned: true, + )); + + expect(newUser.id, 'test'); + expect(newUser.role, 'test'); + expect(newUser.name, 'test'); + expect(newUser.banned, true); + }); }); }