progress on slow mode

This commit is contained in:
groovinchip
2021-07-26 11:42:24 -04:00
parent 848aa550f7
commit 9fb60e93c1
9 changed files with 178 additions and 16 deletions
@@ -130,6 +130,9 @@ class Channel {
return state?.channelStateStream.map((cs) => cs.channel?.cooldown);
}
///
DateTime? cooldownStartedAt;
/// Channel creation date
DateTime? get createdAt {
_checkInitialized();
@@ -429,6 +432,9 @@ class Channel {
skipPush: skipPush,
);
state!.addMessage(response.message);
if (cooldown! > 0) {
cooldownStartedAt = DateTime.now();
}
return response;
} catch (e) {
if (e is StreamChatNetworkError && e.isRetriable) {
@@ -827,6 +833,20 @@ class Channel {
return _client.updateChannelPartial(id!, type, set: set, unset: unset);
}
/// Enable slow mode
Future<UpdateChannelResponse> enableSlowMode({
required int cooldownInterval,
}) async {
_checkInitialized();
return _client.enableSlowdown(id!, type, cooldownInterval);
}
/// Disable slow mode
Future<UpdateChannelResponse> disableSlowMode() async {
_checkInitialized();
return _client.disableSlowdown(id!, type);
}
/// Delete this channel. Messages are permanently removed.
Future<EmptyResponse> delete() async {
_checkInitialized();
@@ -1244,6 +1244,28 @@ class StreamChatClient {
language,
);
/// Enables slow mode
Future<UpdateChannelResponse> enableSlowdown(
String channelId,
String channelType,
int cooldown,
) async =>
_chatApi.channel.enableSlowdown(
channelId,
channelType,
cooldown,
);
/// Disables slow mode
Future<UpdateChannelResponse> disableSlowdown(
String channelId,
String channelType,
) async =>
_chatApi.channel.disableSlowdown(
channelId,
channelType,
);
/// Pins provided message
/// [timeoutOrExpirationDate] can either be a [DateTime] or a value in seconds
/// to be added to [DateTime.now]
@@ -123,6 +123,35 @@ class ChannelApi {
return PartialUpdateChannelResponse.fromJson(response.data);
}
/// Enable slowdown
Future<UpdateChannelResponse> enableSlowdown(
String channelId,
String channelType,
int cooldown,
) async {
final response = await _client.post(
_getChannelUrl(channelId, channelType),
data: {
'cooldown': cooldown,
},
);
return UpdateChannelResponse.fromJson(response.data);
}
/// Disable slowdown
Future<UpdateChannelResponse> disableSlowdown(
String channelId,
String channelType,
) async {
final response = await _client.post(
_getChannelUrl(channelId, channelType),
data: {
'cooldown': 0,
},
);
return UpdateChannelResponse.fromJson(response.data);
}
/// Accept invitation to the channel
Future<AcceptInviteResponse> acceptChannelInvite(
String channelId,
@@ -83,7 +83,7 @@ class ChannelModel {
final int memberCount;
/// The number of seconds in a cooldown
@JsonKey(includeIfNull: false)
@JsonKey(includeIfNull: false, defaultValue: 0)
final int cooldown;
/// Map of custom channel extraData
@@ -33,6 +33,7 @@ ChannelModel _$ChannelModelFromJson(Map<String, dynamic> json) {
memberCount: json['member_count'] as int? ?? 0,
extraData: json['extra_data'] as Map<String, dynamic>? ?? {},
team: json['team'] as String?,
cooldown: json['cooldown'] as int? ?? 0,
);
}
@@ -57,6 +58,7 @@ Map<String, dynamic> _$ChannelModelToJson(ChannelModel instance) {
writeNotNull('updated_at', readonly(instance.updatedAt));
writeNotNull('deleted_at', readonly(instance.deletedAt));
writeNotNull('member_count', readonly(instance.memberCount));
val['cooldown'] = instance.cooldown;
val['extra_data'] = instance.extraData;
writeNotNull('team', readonly(instance.team));
return val;
@@ -36,5 +36,8 @@ OwnUser _$OwnUserFromJson(Map<String, dynamic> json) {
online: json['online'] as bool? ?? false,
extraData: json['extra_data'] as Map<String, dynamic>? ?? {},
banned: json['banned'] as bool? ?? false,
teams:
(json['teams'] as List<dynamic>?)?.map((e) => e as String).toList() ??
[],
);
}