diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index dc0a7176f..3106daf58 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +✅ Added + +- Added teams role support for users. + ## 9.8.0 ✅ Added 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 78fbdf664..05c000188 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.dart @@ -31,6 +31,7 @@ class OwnUser extends User { super.banExpires, super.teams, super.language, + super.teamsRole, }); /// Create a new instance from json. @@ -53,6 +54,7 @@ class OwnUser extends User { extraData: user.extraData, teams: user.teams, language: user.language, + teamsRole: user.teamsRole, ); /// Creates a copy of [OwnUser] with specified attributes overridden. @@ -78,6 +80,7 @@ class OwnUser extends User { int? unreadChannels, int? unreadThreads, String? language, + Map? teamsRole, }) => OwnUser( id: id ?? this.id, @@ -103,6 +106,7 @@ class OwnUser extends User { unreadThreads: unreadThreads ?? this.unreadThreads, blockedUserIds: blockedUserIds ?? this.blockedUserIds, language: language ?? this.language, + teamsRole: teamsRole ?? this.teamsRole, ); /// Returns a new [OwnUser] that is a combination of this ownUser @@ -130,6 +134,7 @@ class OwnUser extends User { blockedUserIds: other.blockedUserIds, updatedAt: other.updatedAt, language: other.language, + teamsRole: other.teamsRole, ); } diff --git a/packages/stream_chat/lib/src/core/models/own_user.g.dart b/packages/stream_chat/lib/src/core/models/own_user.g.dart index 6c073f3b2..c7074f7bc 100644 --- a/packages/stream_chat/lib/src/core/models/own_user.g.dart +++ b/packages/stream_chat/lib/src/core/models/own_user.g.dart @@ -47,4 +47,7 @@ OwnUser _$OwnUserFromJson(Map json) => OwnUser( (json['teams'] as List?)?.map((e) => e as String).toList() ?? const [], language: json['language'] as String?, + teamsRole: (json['teams_role'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); diff --git a/packages/stream_chat/lib/src/core/models/user.dart b/packages/stream_chat/lib/src/core/models/user.dart index 68fa91004..3e5b9b03c 100644 --- a/packages/stream_chat/lib/src/core/models/user.dart +++ b/packages/stream_chat/lib/src/core/models/user.dart @@ -45,6 +45,7 @@ class User extends Equatable implements ComparableFieldProvider { this.banExpires, this.teams = const [], this.language, + this.teamsRole, }) : // For backwards compatibility, set 'name', 'image' in [extraData]. extraData = { @@ -71,6 +72,7 @@ class User extends Equatable implements ComparableFieldProvider { 'ban_expires', 'teams', 'language', + 'teams_role', ]; /// User id. @@ -130,6 +132,12 @@ class User extends Equatable implements ComparableFieldProvider { @JsonKey(includeIfNull: false) final String? language; + /// The roles for the user in the teams. + /// + /// eg: `{'teamId': 'role', 'teamId2': 'role2'}` + @JsonKey(includeIfNull: false) + final Map< /*Team*/ String, /*Role*/ String>? teamsRole; + /// Map of custom user extraData. final Map extraData; @@ -157,6 +165,7 @@ class User extends Equatable implements ComparableFieldProvider { DateTime? banExpires, List? teams, String? language, + Map? teamsRole, }) => User( id: id ?? this.id, @@ -175,6 +184,7 @@ class User extends Equatable implements ComparableFieldProvider { banExpires: banExpires ?? this.banExpires, teams: teams ?? this.teams, language: language ?? this.language, + teamsRole: teamsRole ?? this.teamsRole, ); @override @@ -188,6 +198,7 @@ class User extends Equatable implements ComparableFieldProvider { banExpires, teams, language, + teamsRole, ]; @override diff --git a/packages/stream_chat/lib/src/core/models/user.g.dart b/packages/stream_chat/lib/src/core/models/user.g.dart index 68c227d43..60b7185d0 100644 --- a/packages/stream_chat/lib/src/core/models/user.g.dart +++ b/packages/stream_chat/lib/src/core/models/user.g.dart @@ -28,10 +28,14 @@ User _$UserFromJson(Map json) => User( (json['teams'] as List?)?.map((e) => e as String).toList() ?? const [], language: json['language'] as String?, + teamsRole: (json['teams_role'] as Map?)?.map( + (k, e) => MapEntry(k, e as String), + ), ); Map _$UserToJson(User instance) => { 'id': instance.id, if (instance.language case final value?) 'language': value, + if (instance.teamsRole case final value?) 'teams_role': value, 'extra_data': instance.extraData, }; diff --git a/packages/stream_chat_persistence/CHANGELOG.md b/packages/stream_chat_persistence/CHANGELOG.md index e4b21113e..73a403976 100644 --- a/packages/stream_chat_persistence/CHANGELOG.md +++ b/packages/stream_chat_persistence/CHANGELOG.md @@ -1,3 +1,7 @@ +## Upcoming + +- Added support for `User.teamsRole` field. + ## 9.8.0 - Added `pinnedAt` and `archivedAt` fields on `Member`. diff --git a/packages/stream_chat_persistence/lib/src/db/drift_chat_database.g.dart b/packages/stream_chat_persistence/lib/src/db/drift_chat_database.g.dart index 19ce6ffe8..837ad32e9 100644 --- a/packages/stream_chat_persistence/lib/src/db/drift_chat_database.g.dart +++ b/packages/stream_chat_persistence/lib/src/db/drift_chat_database.g.dart @@ -6357,6 +6357,12 @@ class $UsersTable extends Users with TableInfo<$UsersTable, UserEntity> { GeneratedColumn.constraintIsAlways('CHECK ("banned" IN (0, 1))'), defaultValue: const Constant(false)); @override + late final GeneratedColumnWithTypeConverter?, String> + teamsRole = GeneratedColumn('teams_role', aliasedName, true, + type: DriftSqlType.string, requiredDuringInsert: false) + .withConverter?>( + $UsersTable.$converterteamsRolen); + @override late final GeneratedColumnWithTypeConverter, String> extraData = GeneratedColumn('extra_data', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true) @@ -6371,6 +6377,7 @@ class $UsersTable extends Users with TableInfo<$UsersTable, UserEntity> { lastActive, online, banned, + teamsRole, extraData ]; @override @@ -6443,6 +6450,9 @@ class $UsersTable extends Users with TableInfo<$UsersTable, UserEntity> { .read(DriftSqlType.bool, data['${effectivePrefix}online'])!, banned: attachedDatabase.typeMapping .read(DriftSqlType.bool, data['${effectivePrefix}banned'])!, + teamsRole: $UsersTable.$converterteamsRolen.fromSql(attachedDatabase + .typeMapping + .read(DriftSqlType.string, data['${effectivePrefix}teams_role'])), extraData: $UsersTable.$converterextraData.fromSql(attachedDatabase .typeMapping .read(DriftSqlType.string, data['${effectivePrefix}extra_data'])!), @@ -6454,6 +6464,10 @@ class $UsersTable extends Users with TableInfo<$UsersTable, UserEntity> { return $UsersTable(attachedDatabase, alias); } + static TypeConverter, String> $converterteamsRole = + MapConverter(); + static TypeConverter?, String?> $converterteamsRolen = + NullAwareTypeConverter.wrap($converterteamsRole); static TypeConverter, String> $converterextraData = MapConverter(); } @@ -6483,6 +6497,11 @@ class UserEntity extends DataClass implements Insertable { /// True if user is banned from the chat final bool banned; + /// The roles for the user in the teams. + /// + /// eg: `{'teamId': 'role', 'teamId2': 'role2'}` + final Map? teamsRole; + /// Map of custom user extraData final Map extraData; const UserEntity( @@ -6494,6 +6513,7 @@ class UserEntity extends DataClass implements Insertable { this.lastActive, required this.online, required this.banned, + this.teamsRole, required this.extraData}); @override Map toColumns(bool nullToAbsent) { @@ -6516,6 +6536,10 @@ class UserEntity extends DataClass implements Insertable { } map['online'] = Variable(online); map['banned'] = Variable(banned); + if (!nullToAbsent || teamsRole != null) { + map['teams_role'] = + Variable($UsersTable.$converterteamsRolen.toSql(teamsRole)); + } { map['extra_data'] = Variable($UsersTable.$converterextraData.toSql(extraData)); @@ -6535,6 +6559,7 @@ class UserEntity extends DataClass implements Insertable { lastActive: serializer.fromJson(json['lastActive']), online: serializer.fromJson(json['online']), banned: serializer.fromJson(json['banned']), + teamsRole: serializer.fromJson?>(json['teamsRole']), extraData: serializer.fromJson>(json['extraData']), ); } @@ -6550,6 +6575,7 @@ class UserEntity extends DataClass implements Insertable { 'lastActive': serializer.toJson(lastActive), 'online': serializer.toJson(online), 'banned': serializer.toJson(banned), + 'teamsRole': serializer.toJson?>(teamsRole), 'extraData': serializer.toJson>(extraData), }; } @@ -6563,6 +6589,7 @@ class UserEntity extends DataClass implements Insertable { Value lastActive = const Value.absent(), bool? online, bool? banned, + Value?> teamsRole = const Value.absent(), Map? extraData}) => UserEntity( id: id ?? this.id, @@ -6573,6 +6600,7 @@ class UserEntity extends DataClass implements Insertable { lastActive: lastActive.present ? lastActive.value : this.lastActive, online: online ?? this.online, banned: banned ?? this.banned, + teamsRole: teamsRole.present ? teamsRole.value : this.teamsRole, extraData: extraData ?? this.extraData, ); UserEntity copyWithCompanion(UsersCompanion data) { @@ -6586,6 +6614,7 @@ class UserEntity extends DataClass implements Insertable { data.lastActive.present ? data.lastActive.value : this.lastActive, online: data.online.present ? data.online.value : this.online, banned: data.banned.present ? data.banned.value : this.banned, + teamsRole: data.teamsRole.present ? data.teamsRole.value : this.teamsRole, extraData: data.extraData.present ? data.extraData.value : this.extraData, ); } @@ -6601,6 +6630,7 @@ class UserEntity extends DataClass implements Insertable { ..write('lastActive: $lastActive, ') ..write('online: $online, ') ..write('banned: $banned, ') + ..write('teamsRole: $teamsRole, ') ..write('extraData: $extraData') ..write(')')) .toString(); @@ -6608,7 +6638,7 @@ class UserEntity extends DataClass implements Insertable { @override int get hashCode => Object.hash(id, role, language, createdAt, updatedAt, - lastActive, online, banned, extraData); + lastActive, online, banned, teamsRole, extraData); @override bool operator ==(Object other) => identical(this, other) || @@ -6621,6 +6651,7 @@ class UserEntity extends DataClass implements Insertable { other.lastActive == this.lastActive && other.online == this.online && other.banned == this.banned && + other.teamsRole == this.teamsRole && other.extraData == this.extraData); } @@ -6633,6 +6664,7 @@ class UsersCompanion extends UpdateCompanion { final Value lastActive; final Value online; final Value banned; + final Value?> teamsRole; final Value> extraData; final Value rowid; const UsersCompanion({ @@ -6644,6 +6676,7 @@ class UsersCompanion extends UpdateCompanion { this.lastActive = const Value.absent(), this.online = const Value.absent(), this.banned = const Value.absent(), + this.teamsRole = const Value.absent(), this.extraData = const Value.absent(), this.rowid = const Value.absent(), }); @@ -6656,6 +6689,7 @@ class UsersCompanion extends UpdateCompanion { this.lastActive = const Value.absent(), this.online = const Value.absent(), this.banned = const Value.absent(), + this.teamsRole = const Value.absent(), required Map extraData, this.rowid = const Value.absent(), }) : id = Value(id), @@ -6669,6 +6703,7 @@ class UsersCompanion extends UpdateCompanion { Expression? lastActive, Expression? online, Expression? banned, + Expression? teamsRole, Expression? extraData, Expression? rowid, }) { @@ -6681,6 +6716,7 @@ class UsersCompanion extends UpdateCompanion { if (lastActive != null) 'last_active': lastActive, if (online != null) 'online': online, if (banned != null) 'banned': banned, + if (teamsRole != null) 'teams_role': teamsRole, if (extraData != null) 'extra_data': extraData, if (rowid != null) 'rowid': rowid, }); @@ -6695,6 +6731,7 @@ class UsersCompanion extends UpdateCompanion { Value? lastActive, Value? online, Value? banned, + Value?>? teamsRole, Value>? extraData, Value? rowid}) { return UsersCompanion( @@ -6706,6 +6743,7 @@ class UsersCompanion extends UpdateCompanion { lastActive: lastActive ?? this.lastActive, online: online ?? this.online, banned: banned ?? this.banned, + teamsRole: teamsRole ?? this.teamsRole, extraData: extraData ?? this.extraData, rowid: rowid ?? this.rowid, ); @@ -6738,6 +6776,10 @@ class UsersCompanion extends UpdateCompanion { if (banned.present) { map['banned'] = Variable(banned.value); } + if (teamsRole.present) { + map['teams_role'] = Variable( + $UsersTable.$converterteamsRolen.toSql(teamsRole.value)); + } if (extraData.present) { map['extra_data'] = Variable( $UsersTable.$converterextraData.toSql(extraData.value)); @@ -6759,6 +6801,7 @@ class UsersCompanion extends UpdateCompanion { ..write('lastActive: $lastActive, ') ..write('online: $online, ') ..write('banned: $banned, ') + ..write('teamsRole: $teamsRole, ') ..write('extraData: $extraData, ') ..write('rowid: $rowid') ..write(')')) @@ -12574,6 +12617,7 @@ typedef $$UsersTableCreateCompanionBuilder = UsersCompanion Function({ Value lastActive, Value online, Value banned, + Value?> teamsRole, required Map extraData, Value rowid, }); @@ -12586,6 +12630,7 @@ typedef $$UsersTableUpdateCompanionBuilder = UsersCompanion Function({ Value lastActive, Value online, Value banned, + Value?> teamsRole, Value> extraData, Value rowid, }); @@ -12623,6 +12668,12 @@ class $$UsersTableFilterComposer ColumnFilters get banned => $composableBuilder( column: $table.banned, builder: (column) => ColumnFilters(column)); + ColumnWithTypeConverterFilters?, Map, + String> + get teamsRole => $composableBuilder( + column: $table.teamsRole, + builder: (column) => ColumnWithTypeConverterFilters(column)); + ColumnWithTypeConverterFilters, Map, String> get extraData => $composableBuilder( @@ -12663,6 +12714,9 @@ class $$UsersTableOrderingComposer ColumnOrderings get banned => $composableBuilder( column: $table.banned, builder: (column) => ColumnOrderings(column)); + ColumnOrderings get teamsRole => $composableBuilder( + column: $table.teamsRole, builder: (column) => ColumnOrderings(column)); + ColumnOrderings get extraData => $composableBuilder( column: $table.extraData, builder: (column) => ColumnOrderings(column)); } @@ -12700,6 +12754,10 @@ class $$UsersTableAnnotationComposer GeneratedColumn get banned => $composableBuilder(column: $table.banned, builder: (column) => column); + GeneratedColumnWithTypeConverter?, String> + get teamsRole => $composableBuilder( + column: $table.teamsRole, builder: (column) => column); + GeneratedColumnWithTypeConverter, String> get extraData => $composableBuilder( column: $table.extraData, builder: (column) => column); @@ -12736,6 +12794,7 @@ class $$UsersTableTableManager extends RootTableManager< Value lastActive = const Value.absent(), Value online = const Value.absent(), Value banned = const Value.absent(), + Value?> teamsRole = const Value.absent(), Value> extraData = const Value.absent(), Value rowid = const Value.absent(), }) => @@ -12748,6 +12807,7 @@ class $$UsersTableTableManager extends RootTableManager< lastActive: lastActive, online: online, banned: banned, + teamsRole: teamsRole, extraData: extraData, rowid: rowid, ), @@ -12760,6 +12820,7 @@ class $$UsersTableTableManager extends RootTableManager< Value lastActive = const Value.absent(), Value online = const Value.absent(), Value banned = const Value.absent(), + Value?> teamsRole = const Value.absent(), required Map extraData, Value rowid = const Value.absent(), }) => @@ -12772,6 +12833,7 @@ class $$UsersTableTableManager extends RootTableManager< lastActive: lastActive, online: online, banned: banned, + teamsRole: teamsRole, extraData: extraData, rowid: rowid, ), diff --git a/packages/stream_chat_persistence/lib/src/entity/users.dart b/packages/stream_chat_persistence/lib/src/entity/users.dart index 4245c02e6..bb72790bb 100644 --- a/packages/stream_chat_persistence/lib/src/entity/users.dart +++ b/packages/stream_chat_persistence/lib/src/entity/users.dart @@ -29,6 +29,11 @@ class Users extends Table { /// True if user is banned from the chat BoolColumn get banned => boolean().withDefault(const Constant(false))(); + /// The roles for the user in the teams. + /// + /// eg: `{'teamId': 'role', 'teamId2': 'role2'}` + TextColumn get teamsRole => text().nullable().map(MapConverter())(); + /// Map of custom user extraData TextColumn get extraData => text().map(MapConverter())(); diff --git a/packages/stream_chat_persistence/lib/src/mapper/user_mapper.dart b/packages/stream_chat_persistence/lib/src/mapper/user_mapper.dart index f6409c391..2d391e27d 100644 --- a/packages/stream_chat_persistence/lib/src/mapper/user_mapper.dart +++ b/packages/stream_chat_persistence/lib/src/mapper/user_mapper.dart @@ -14,6 +14,7 @@ extension UserEntityX on UserEntity { extraData: extraData, banned: banned, createdAt: createdAt, + teamsRole: teamsRole, ); } @@ -29,6 +30,7 @@ extension UserX on User { lastActive: lastActive, online: online, banned: banned, + teamsRole: teamsRole, extraData: extraData, ); } diff --git a/packages/stream_chat_persistence/test/src/dao/user_dao_test.dart b/packages/stream_chat_persistence/test/src/dao/user_dao_test.dart index f5a40dbc6..ce7f4a477 100644 --- a/packages/stream_chat_persistence/test/src/dao/user_dao_test.dart +++ b/packages/stream_chat_persistence/test/src/dao/user_dao_test.dart @@ -28,6 +28,7 @@ void main() { lastActive: DateTime.now(), online: math.Random().nextBool(), banned: math.Random().nextBool(), + teamsRole: const {'teamId': 'role', 'teamId2': 'role2'}, ), ); await userDao.updateUsers(users); @@ -47,6 +48,7 @@ void main() { updatedAt: DateTime.now(), online: math.Random().nextBool(), banned: math.Random().nextBool(), + teamsRole: const {'teamId': 'role', 'teamId2': 'role2'}, ); await userDao.updateUsers([copyUser, newUser]); diff --git a/packages/stream_chat_persistence/test/src/mapper/user_mapper_test.dart b/packages/stream_chat_persistence/test/src/mapper/user_mapper_test.dart index b41e9d597..42b1d20d1 100644 --- a/packages/stream_chat_persistence/test/src/mapper/user_mapper_test.dart +++ b/packages/stream_chat_persistence/test/src/mapper/user_mapper_test.dart @@ -18,6 +18,7 @@ void main() { lastActive: DateTime.now(), online: math.Random().nextBool(), banned: math.Random().nextBool(), + teamsRole: const {'teamId': 'role', 'teamId2': 'role2'}, extraData: {'test_extra_data': 'extraData'}, ); final user = entity.toUser(); @@ -30,6 +31,7 @@ void main() { expect(user.lastActive, isSameDateAs(entity.lastActive)); expect(user.online, entity.online); expect(user.banned, entity.banned); + expect(user.teamsRole, entity.teamsRole); expect(user.extraData, entity.extraData); }); @@ -43,6 +45,7 @@ void main() { lastActive: DateTime.now(), online: math.Random().nextBool(), banned: math.Random().nextBool(), + teamsRole: const {'teamId': 'role', 'teamId2': 'role2'}, extraData: const {'test_extra_data': 'extraData'}, ); final entity = user.toEntity(); @@ -55,6 +58,7 @@ void main() { expect(entity.lastActive, isSameDateAs(user.lastActive)); expect(entity.online, user.online); expect(entity.banned, user.banned); + expect(entity.teamsRole, user.teamsRole); expect(entity.extraData, user.extraData); }); }