Skip to content

Commit

Permalink
Fixed operatorIds bugs when updating channel
Browse files Browse the repository at this point in the history
  • Loading branch information
intoxicated committed Nov 4, 2021
1 parent 7539831 commit cdd3544
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 73 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [3.1.6] - Nov 3, 2021
* Added `RestrictedUser` for callback mute/ban feature
* Fixed session related issue
* Fixed group channel updates not to apply unset `operator_ids` in `GroupChannelParams`

## [3.1.5] - Sep 30, 2021
* Fixed link in README
* Improved stabilities
Expand Down
5 changes: 5 additions & 0 deletions lib/constant/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,8 @@ const memberListOrderEnumMap = <MemberListOrder, String>{
MemberListOrder.operatorThenMemberNicknameAlphabetical:
'operator_then_member_alphabetical',
};

enum RestrictionType {
muted,
banned,
}
11 changes: 10 additions & 1 deletion lib/core/models/member.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:sendbird_sdk/constant/enums.dart';
import 'package:sendbird_sdk/core/models/restricted_user.dart';
import 'package:sendbird_sdk/core/models/user.dart';
import 'package:sendbird_sdk/utils/json_from_parser.dart';

Expand Down Expand Up @@ -31,6 +32,10 @@ class Member extends User {
@JsonKey(defaultValue: Role.none, unknownEnumValue: Role.none)
Role role;

/// Restriction information
@JsonKey(ignore: true)
RestrictionInfo? restrictionInfo;

Member({
this.state = MemberState.none,
this.isBlockedByMe = false,
Expand Down Expand Up @@ -64,7 +69,11 @@ class Member extends User {

// json serialization

factory Member.fromJson(Map<String, dynamic> json) => _$MemberFromJson(json);
factory Member.fromJson(Map<String, dynamic> json) {
final member = _$MemberFromJson(json);
member.restrictionInfo = RestrictionInfo.fromJson(json);
return member;
}

@override
Map<String, dynamic> toJson() => _$MemberToJson(this);
Expand Down
33 changes: 30 additions & 3 deletions lib/core/models/responses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:sendbird_sdk/core/channel/base/base_channel.dart';
import 'package:sendbird_sdk/core/channel/group/group_channel.dart';
import 'package:sendbird_sdk/core/channel/open/open_channel.dart';
import 'package:sendbird_sdk/core/message/base_message.dart';
import 'package:sendbird_sdk/core/models/member.dart';
import 'package:sendbird_sdk/core/models/restricted_user.dart';
import 'package:sendbird_sdk/core/models/user.dart';
import 'package:sendbird_sdk/services/db/cache_service.dart';

Expand Down Expand Up @@ -158,16 +160,17 @@ class OperatorListQueryResponse extends BaseResponse {
}

@JsonSerializable(createToJson: false)
class UserListQueryResponse extends BaseResponse {
class UserListQueryResponse<T extends User> extends BaseResponse {
@JsonKey(defaultValue: [])
List<User> users;
@UserConverter()
List<T> users;

String? next;

UserListQueryResponse({this.users = const [], this.next});

factory UserListQueryResponse.fromJson(Map<String, dynamic> json) =>
_$UserListQueryResponseFromJson(json);
_$UserListQueryResponseFromJson<T>(json);
}

@JsonSerializable(createToJson: false)
Expand Down Expand Up @@ -204,6 +207,30 @@ class ChannelConverter<T> implements JsonConverter<T, Object> {
}
}

class UserConverter<T> implements JsonConverter<T, Object> {
const UserConverter();

@override
T fromJson(Object json) {
if (json is Map<String, dynamic>) {
if (json.containsKey('end_at')) {
return RestrictedUser.fromJson(json) as T;
} else if (json.containsKey('muted_end_at') ||
json.containsKey('is_muted')) {
return Member.fromJson(json) as T;
} else {
return User.fromJson(json) as T;
}
}
return json as T;
}

@override
Object toJson(T object) {
return object as Object;
}
}

@JsonSerializable(createToJson: false)
class MessageSearchQueryResponse extends BaseResponse {
@JsonKey(defaultValue: [])
Expand Down
6 changes: 3 additions & 3 deletions lib/core/models/responses.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions lib/core/models/restricted_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:sendbird_sdk/constant/enums.dart';
import 'package:sendbird_sdk/core/models/user.dart';
import 'package:sendbird_sdk/utils/json_from_parser.dart';

part 'restricted_user.g.dart';

/// An object represents a sender of a message.
@JsonSerializable()
class RestrictedUser extends User {
/// Information about restriction
RestrictionInfo? get restrictionInfo => _info;
RestrictionInfo? _info;

RestrictedUser({
required String userId,
required String nickname,
String? profileUrl,
UserConnectionStatus connectionStatus = UserConnectionStatus.notAvailable,
int? lastSeenAt,
List<String>? preferredLanguages,
String? friendDiscoveryKey,
String? friendName,
List<String>? discoveryKeys,
Map<String, String> metaData = const {},
bool requireAuth = false,
}) : super(
userId: userId,
nickname: nickname,
profileUrl: profileUrl,
connectionStatus: connectionStatus,
lastSeenAt: lastSeenAt,
preferredLanguages: preferredLanguages,
friendDiscoveryKey: friendDiscoveryKey,
friendName: friendName,
discoveryKeys: discoveryKeys,
metaData: metaData,
requireAuth: requireAuth,
);

factory RestrictedUser.fromJson(Map<String, dynamic> json) {
final user = _$RestrictedUserFromJson(json);
user._info = RestrictionInfo.fromJson(json);
return user;
}

@override
Map<String, dynamic> toJson() => _$RestrictedUserToJson(this);
}

@JsonSerializable()
class RestrictionInfo {
// description of restriction
final String? description;

// timestamp when restriction will end
final int? endAt;

// timestamp when restriction started
final int? startAt;

// restriction type
final RestrictionType type;

RestrictionInfo({
this.description,
this.endAt,
this.startAt,
this.type = RestrictionType.muted,
});

factory RestrictionInfo.fromJson(Map<String, dynamic> json) {
json['end_at'] = json['end_at'] ?? json['muted_end_at'];
json['start_at'] = json['start_at'] ?? json['muted_start_at'];
json['description'] = json['description'] ?? json['muted_description'];
if (json['type'] == null) json['type'] = 'muted';
return _$RestrictionInfoFromJson(json);
}

Map<String, dynamic> toJson() => _$RestrictionInfoToJson(this);
}
97 changes: 97 additions & 0 deletions lib/core/models/restricted_user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions lib/events/session_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ part 'session_event.g.dart';
/// Represents session information
@JsonSerializable(createToJson: false)
class SessionEvent extends BaseEvent {
@JsonKey(defaultValue: 0)
final int? expiresAt;

final String? newKey;

final String? ekey;
Expand All @@ -17,7 +14,7 @@ class SessionEvent extends BaseEvent {

String get sessionKey => key ?? newKey ?? '';

SessionEvent({this.expiresAt, this.newKey, this.ekey, this.key});
SessionEvent({this.newKey, this.ekey, this.key});

factory SessionEvent.fromJson(Map<String, dynamic> json) =>
_$SessionEventFromJson(json);
Expand Down
1 change: 0 additions & 1 deletion lib/events/session_event.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cdd3544

Please sign in to comment.