From b1e4e134cadc175bf9660fe9fbffd98478d1597f Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 19 May 2022 14:34:04 +0530 Subject: [PATCH] feat(llc): add support for channel.membership Signed-off-by: xsahil03x --- .../stream_chat/lib/src/client/channel.dart | 12 ++++ .../core/models/attachment_file.freezed.dart | 59 +++++-------------- .../lib/src/core/models/channel_model.dart | 10 ++++ .../lib/src/core/models/channel_model.g.dart | 4 ++ 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index f5d87404..2a9e26a0 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -171,6 +171,18 @@ class Channel { return state!.channelStateStream.map((cs) => cs.channel?.config); } + /// Relationship of the current user to this channel. + Member? get membership { + _checkInitialized(); + return state!._channelState.channel?.membership; + } + + /// Relationship of the current user to this channel as a stream. + Stream get membershipStream { + _checkInitialized(); + return state!.channelStateStream.map((cs) => cs.channel?.membership); + } + /// Channel user creator. User? get createdBy { _checkInitialized(); diff --git a/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart b/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart index 94f15db4..9140ec5e 100644 --- a/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart +++ b/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart @@ -12,7 +12,7 @@ part of 'attachment_file.dart'; T _$identity(T value) => value; final _privateConstructorUsedError = UnsupportedError( - 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); UploadState _$UploadStateFromJson(Map json) { switch (json['runtimeType']) { @@ -31,39 +31,6 @@ UploadState _$UploadStateFromJson(Map json) { } } -/// @nodoc -class _$UploadStateTearOff { - const _$UploadStateTearOff(); - - Preparing preparing() { - return const Preparing(); - } - - InProgress inProgress({required int uploaded, required int total}) { - return InProgress( - uploaded: uploaded, - total: total, - ); - } - - Success success() { - return const Success(); - } - - Failed failed({required String error}) { - return Failed( - error: error, - ); - } - - UploadState fromJson(Map json) { - return UploadState.fromJson(json); - } -} - -/// @nodoc -const $UploadState = _$UploadStateTearOff(); - /// @nodoc mixin _$UploadState { @optionalTypeArgs @@ -156,7 +123,7 @@ class __$$PreparingCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res> /// @nodoc @JsonSerializable() class _$Preparing implements Preparing { - const _$Preparing({String? $type}) : $type = $type ?? 'preparing'; + const _$Preparing({final String? $type}) : $type = $type ?? 'preparing'; factory _$Preparing.fromJson(Map json) => _$$PreparingFromJson(json); @@ -175,6 +142,7 @@ class _$Preparing implements Preparing { (other.runtimeType == runtimeType && other is _$Preparing); } + @JsonKey(ignore: true) @override int get hashCode => runtimeType.hashCode; @@ -304,7 +272,7 @@ class __$$InProgressCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res> @JsonSerializable() class _$InProgress implements InProgress { const _$InProgress( - {required this.uploaded, required this.total, String? $type}) + {required this.uploaded, required this.total, final String? $type}) : $type = $type ?? 'inProgress'; factory _$InProgress.fromJson(Map json) => @@ -332,6 +300,7 @@ class _$InProgress implements InProgress { const DeepCollectionEquality().equals(other.total, total)); } + @JsonKey(ignore: true) @override int get hashCode => Object.hash( runtimeType, @@ -424,14 +393,14 @@ class _$InProgress implements InProgress { } abstract class InProgress implements UploadState { - const factory InProgress({required int uploaded, required int total}) = - _$InProgress; + const factory InProgress( + {required final int uploaded, required final int total}) = _$InProgress; factory InProgress.fromJson(Map json) = _$InProgress.fromJson; - int get uploaded; - int get total; + int get uploaded => throw _privateConstructorUsedError; + int get total => throw _privateConstructorUsedError; @JsonKey(ignore: true) _$$InProgressCopyWith<_$InProgress> get copyWith => throw _privateConstructorUsedError; @@ -456,7 +425,7 @@ class __$$SuccessCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res> /// @nodoc @JsonSerializable() class _$Success implements Success { - const _$Success({String? $type}) : $type = $type ?? 'success'; + const _$Success({final String? $type}) : $type = $type ?? 'success'; factory _$Success.fromJson(Map json) => _$$SuccessFromJson(json); @@ -475,6 +444,7 @@ class _$Success implements Success { (other.runtimeType == runtimeType && other is _$Success); } + @JsonKey(ignore: true) @override int get hashCode => runtimeType.hashCode; @@ -596,7 +566,7 @@ class __$$FailedCopyWithImpl<$Res> extends _$UploadStateCopyWithImpl<$Res> /// @nodoc @JsonSerializable() class _$Failed implements Failed { - const _$Failed({required this.error, String? $type}) + const _$Failed({required this.error, final String? $type}) : $type = $type ?? 'failed'; factory _$Failed.fromJson(Map json) => @@ -621,6 +591,7 @@ class _$Failed implements Failed { const DeepCollectionEquality().equals(other.error, error)); } + @JsonKey(ignore: true) @override int get hashCode => Object.hash(runtimeType, const DeepCollectionEquality().hash(error)); @@ -711,11 +682,11 @@ class _$Failed implements Failed { } abstract class Failed implements UploadState { - const factory Failed({required String error}) = _$Failed; + const factory Failed({required final String error}) = _$Failed; factory Failed.fromJson(Map json) = _$Failed.fromJson; - String get error; + String get error => throw _privateConstructorUsedError; @JsonKey(ignore: true) _$$FailedCopyWith<_$Failed> get copyWith => throw _privateConstructorUsedError; diff --git a/packages/stream_chat/lib/src/core/models/channel_model.dart b/packages/stream_chat/lib/src/core/models/channel_model.dart index c3e83435..e2f7303e 100644 --- a/packages/stream_chat/lib/src/core/models/channel_model.dart +++ b/packages/stream_chat/lib/src/core/models/channel_model.dart @@ -1,5 +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/models/member.dart'; import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/util/serializer.dart'; @@ -25,6 +26,7 @@ class ChannelModel { this.extraData = const {}, this.team, this.cooldown = 0, + this.membership, }) : assert( (cid != null && cid.contains(':')) || (id != null && type != null), 'provide either a cid or an id and type', @@ -100,6 +102,10 @@ class ChannelModel { @JsonKey(includeIfNull: false, toJson: Serializer.readOnly) final String? team; + /// Relationship of the current user to this channel. + @JsonKey(includeIfNull: false) + final Member? membership; + /// Known top level fields. /// Useful for [Serializer] methods. static const topLevelFields = [ @@ -117,6 +123,7 @@ class ChannelModel { 'member_count', 'team', 'cooldown', + 'membership', ]; /// Shortcut for channel name @@ -145,6 +152,7 @@ class ChannelModel { Map? extraData, String? team, int? cooldown, + Member? membership, }) => ChannelModel( id: id ?? this.id, @@ -162,6 +170,7 @@ class ChannelModel { extraData: extraData ?? this.extraData, team: team ?? this.team, cooldown: cooldown ?? this.cooldown, + membership: membership ?? this.membership, ); /// Returns a new [ChannelModel] that is a combination of this channelModel @@ -184,6 +193,7 @@ class ChannelModel { extraData: {...extraData, ...other.extraData}, team: other.team, cooldown: other.cooldown, + membership: other.membership, ); } } diff --git a/packages/stream_chat/lib/src/core/models/channel_model.g.dart b/packages/stream_chat/lib/src/core/models/channel_model.g.dart index 9bd8f062..910c3aea 100644 --- a/packages/stream_chat/lib/src/core/models/channel_model.g.dart +++ b/packages/stream_chat/lib/src/core/models/channel_model.g.dart @@ -36,6 +36,9 @@ ChannelModel _$ChannelModelFromJson(Map json) => ChannelModel( extraData: json['extra_data'] as Map? ?? const {}, team: json['team'] as String?, cooldown: json['cooldown'] as int? ?? 0, + membership: json['membership'] == null + ? null + : Member.fromJson(json['membership'] as Map), ); Map _$ChannelModelToJson(ChannelModel instance) { @@ -63,5 +66,6 @@ Map _$ChannelModelToJson(ChannelModel instance) { val['cooldown'] = instance.cooldown; val['extra_data'] = instance.extraData; writeNotNull('team', readonly(instance.team)); + writeNotNull('membership', instance.membership?.toJson()); return val; }