Merge pull request #448 from GetStream/hotfix/pin

fix: make timeoutOrExpirationDate optional
This commit is contained in:
Salvatore Giordano
2021-05-21 15:30:13 +02:00
committed by GitHub
4 changed files with 84 additions and 10 deletions
@@ -507,9 +507,9 @@ class Channel {
/// Pins provided message /// Pins provided message
Future<UpdateMessageResponse> pinMessage( Future<UpdateMessageResponse> pinMessage(
Message message, Message message, [
Object? timeoutOrExpirationDate, Object? timeoutOrExpirationDate,
) { ]) {
assert(() { assert(() {
if (timeoutOrExpirationDate is! DateTime && if (timeoutOrExpirationDate is! DateTime &&
timeoutOrExpirationDate != null && timeoutOrExpirationDate != null &&
@@ -517,7 +517,7 @@ class Channel {
throw ArgumentError('Invalid timeout or Expiration date'); throw ArgumentError('Invalid timeout or Expiration date');
} }
return true; return true;
}(), 'Check for invalid token or expiration date'); }(), 'Check whether time out is valid');
DateTime? pinExpires; DateTime? pinExpires;
if (timeoutOrExpirationDate is DateTime) { if (timeoutOrExpirationDate is DateTime) {
+14 -7
View File
@@ -1361,12 +1361,13 @@ class StreamChatClient {
/// [timeoutOrExpirationDate] can either be a [DateTime] or a value in seconds /// [timeoutOrExpirationDate] can either be a [DateTime] or a value in seconds
/// to be added to [DateTime.now] /// to be added to [DateTime.now]
Future<UpdateMessageResponse> pinMessage( Future<UpdateMessageResponse> pinMessage(
Message message, Message message, [
Object timeoutOrExpirationDate, Object? timeoutOrExpirationDate,
) { ]) {
assert(() { assert(() {
if (timeoutOrExpirationDate is! DateTime && if (timeoutOrExpirationDate is! DateTime &&
timeoutOrExpirationDate is! num) { timeoutOrExpirationDate is! num &&
timeoutOrExpirationDate != null) {
throw ArgumentError('Invalid timeout or Expiration date'); throw ArgumentError('Invalid timeout or Expiration date');
} }
return true; return true;
@@ -1383,13 +1384,19 @@ class StreamChatClient {
.toUtc(); .toUtc();
} }
return updateMessage( return updateMessage(
message.copyWith(pinned: true, pinExpires: pinExpires), message.copyWith(
pinned: true,
pinExpires: pinExpires,
),
); );
} }
/// Unpins provided message /// Unpins provided message
Future<UpdateMessageResponse> unpinMessage(Message message) => Future<UpdateMessageResponse> unpinMessage(Message message) => updateMessage(
updateMessage(message.copyWith(pinned: false)); message.copyWith(
pinned: false,
),
);
} }
/// The class that handles the state of the channel listening to the events /// The class that handles the state of the channel listening to the events
@@ -449,6 +449,51 @@ void main() {
.called(1); .called(1);
}); });
test('should be pinned successfully with null timeout', () async {
final mockDio = MockDio();
when(() => mockDio.options).thenReturn(BaseOptions());
when(() => mockDio.interceptors).thenReturn(Interceptors());
final client = StreamChatClient(
'api-key',
httpClient: mockDio,
tokenProvider: (_) async => '',
);
final channelClient = client.channel('messaging', id: 'testid');
final message = Message(text: 'Hello', id: 'test');
when(() => mockDio.post<String>(
any(),
data: any(named: 'data'),
)).thenAnswer((_) async => Response(
data: jsonEncode(ChannelState()),
statusCode: 200,
requestOptions: FakeRequestOptions(),
));
await channelClient.watch();
when(
() => mockDio.post<String>(
'/messages/${message.id}',
data: anything,
),
).thenAnswer(
(_) async => Response(
data: jsonEncode({'message': message}),
statusCode: 200,
requestOptions: FakeRequestOptions(),
),
);
await channelClient.pinMessage(message);
verify(() =>
mockDio.post<String>('/messages/${message.id}', data: anything))
.called(1);
});
test('should be unpinned successfully', () async { test('should be unpinned successfully', () async {
final mockDio = MockDio(); final mockDio = MockDio();
@@ -1112,6 +1112,28 @@ void main() {
data: {'message': anything})).called(1); data: {'message': anything})).called(1);
}); });
test('should complete successfully with a null value', () async {
final message = Message(text: 'Hello');
when(
() => mockDio.post<String>(
'/messages/${message.id}',
data: anything,
),
).thenAnswer(
(_) async => Response(
data: jsonEncode({'message': message}),
statusCode: 200,
requestOptions: FakeRequestOptions(),
),
);
await client.pinMessage(message);
verify(() => mockDio.post<String>('/messages/${message.id}',
data: {'message': anything})).called(1);
});
test('should unpin message successfully', () async { test('should unpin message successfully', () async {
final message = Message(text: 'Hello'); final message = Message(text: 'Hello');