feat: Added new API endpoint

This commit is contained in:
Deven Joshi
2021-06-02 15:55:39 +05:30
parent 71b7facc0c
commit 39d82c75ad
2 changed files with 57 additions and 19 deletions
+33 -8
View File
@@ -462,7 +462,7 @@ class Channel {
if (error is DioError && error.type != DioErrorType.response) { if (error is DioError && error.type != DioErrorType.response) {
state?.retryQueue?.add([message]); state?.retryQueue?.add([message]);
} else if (error is ApiError) { } else if (error is ApiError) {
if(currentMessage != null) { if (currentMessage != null) {
state?.addMessage(currentMessage); state?.addMessage(currentMessage);
} }
} }
@@ -470,6 +470,27 @@ class Channel {
} }
} }
/// Partially updates the [message] in this channel.
Future<UpdateMessageResponse> partiallyUpdateMessage(
Message message, Map data) async {
try {
final response = await _client.partiallyUpdateMessage(message.id, data);
final m = response.message.copyWith(
ownReactions: message.ownReactions,
);
state?.addMessage(m);
return response;
} catch (error) {
if (error is DioError && error.type != DioErrorType.response) {
state?.retryQueue?.add([message]);
}
rethrow;
}
}
/// Deletes the [message] from the channel. /// Deletes the [message] from the channel.
Future<EmptyResponse> deleteMessage(Message message) async { Future<EmptyResponse> deleteMessage(Message message) async {
// Directly deleting the local messages which are not yet sent to server // Directly deleting the local messages which are not yet sent to server
@@ -533,17 +554,21 @@ class Channel {
Duration(seconds: timeoutOrExpirationDate.toInt()), Duration(seconds: timeoutOrExpirationDate.toInt()),
); );
} }
return updateMessage( return partiallyUpdateMessage(message, {
message.copyWith( 'set': {
pinned: true, 'pinned': true,
pinExpires: pinExpires, if (pinExpires != null) 'pin_expires': pinExpires.toIso8601String(),
), }
); });
} }
/// Unpins provided message /// Unpins provided message
Future<UpdateMessageResponse> unpinMessage(Message message) => Future<UpdateMessageResponse> unpinMessage(Message message) =>
updateMessage(message.copyWith(pinned: false)); partiallyUpdateMessage(message, {
'set': {
'pinned': false,
}
});
/// Send a file to this channel /// Send a file to this channel
Future<SendFileResponse> sendFile( Future<SendFileResponse> sendFile(
+24 -11
View File
@@ -1347,6 +1347,18 @@ class StreamChatClient {
return decode(response.data, UpdateMessageResponse.fromJson); return decode(response.data, UpdateMessageResponse.fromJson);
} }
/// Partially update the given message
/// Use 'set' in map to set values
/// User 'unset' in map to unset values
Future<UpdateMessageResponse> partiallyUpdateMessage(
String id, Map data) async {
final response = await put(
'/messages/${id}',
data: data,
);
return decode(response.data, UpdateMessageResponse.fromJson);
}
/// Deletes the given message /// Deletes the given message
Future<EmptyResponse> deleteMessage(Message message) async { Future<EmptyResponse> deleteMessage(Message message) async {
final response = await delete('/messages/${message.id}'); final response = await delete('/messages/${message.id}');
@@ -1385,20 +1397,21 @@ class StreamChatClient {
) )
.toUtc(); .toUtc();
} }
return updateMessage( return partiallyUpdateMessage(message.id, {
message.copyWith( 'set': {
pinned: true, 'pinned': true,
pinExpires: pinExpires, if (pinExpires != null) 'pin_expires': pinExpires.toIso8601String(),
), }
); });
} }
/// Unpins provided message /// Unpins provided message
Future<UpdateMessageResponse> unpinMessage(Message message) => updateMessage( Future<UpdateMessageResponse> unpinMessage(Message message) =>
message.copyWith( partiallyUpdateMessage(message.id, {
pinned: false, 'set': {
), '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