Skip to content

Commit

Permalink
[CHAT-1513] partial message update (#576)
Browse files Browse the repository at this point in the history
* implement partial message update

* fix docs

* wip on tests

* add todo list

* improve test coverage

* fix comments

* rename types

* exclude reserved fields

* fix updatable fields

Co-authored-by: Vishal Narkhede <[email protected]>
  • Loading branch information
thesyncim and vishalnarkhede authored May 18, 2021
1 parent b8bd102 commit 5f713f7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
MuteUserOptions,
MuteUserResponse,
OwnUserResponse,
PartialMessageUpdate,
PartialUserUpdate,
PermissionAPIResponse,
PermissionsAPIResponse,
Expand Down Expand Up @@ -2404,6 +2405,7 @@ export class StreamChat<
* @param {UpdatedMessage<AttachmentType,ChannelType,CommandType,MessageType,ReactionType,UserType>} message object
* @param {undefined|number|string|Date} timeoutOrExpirationDate expiration date or timeout. Use number type to set timeout in seconds, string or Date to set exact expiration date
*/
// todo use partialMessageUpdate and allow pin using server side auth
pinMessage(
message: UpdatedMessage<
AttachmentType,
Expand Down Expand Up @@ -2436,6 +2438,7 @@ export class StreamChat<
* unpinMessage - unpins provided message
* @param {UpdatedMessage<AttachmentType,ChannelType,CommandType,MessageType,ReactionType,UserType>} message object
*/
// todo use partialMessageUpdate and allow unpin using server side auth
unpinMessage(
message: UpdatedMessage<
AttachmentType,
Expand Down Expand Up @@ -2545,6 +2548,44 @@ export class StreamChat<
});
}

/**
* partialUpdateMessage - Update the given message id while retaining additional properties
*
* @param {string} id the message id
*
* @param {PartialUpdateMessage<MessageType>} partialMessageObject which should contain id and any of "set" or "unset" params;
* example: {id: "user1", set:{text: "hi"}, unset:["color"]}
* @param {string | { id: string }} [userId]
*
* @return {APIResponse & { message: MessageResponse<AttachmentType, ChannelType, CommandType, MessageType, ReactionType, UserType> }} Response that includes the updated message
*/
async partialUpdateMessage(
id: string,
partialMessageObject: PartialMessageUpdate<MessageType>,
userId?: string | { id: string },
) {
if (!id) {
throw Error('Please specify the message id when calling partialUpdateMessage');
}
let user = userId;
if (userId != null && isString(userId)) {
user = { id: userId };
}
return await this.put<
UpdateMessageAPIResponse<
AttachmentType,
ChannelType,
CommandType,
MessageType,
ReactionType,
UserType
>
>(this.baseURL + `/messages/${id}`, {
...partialMessageObject,
user,
});
}

async deleteMessage(messageID: string, hardDelete?: boolean) {
let params = {};
if (hardDelete) {
Expand Down
10 changes: 9 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,8 @@ export type EndpointName =
| 'ExportChannels'
| 'GetExportChannelsStatus'
| 'CheckSQS'
| 'GetRateLimits';
| 'GetRateLimits'
| 'MessageUpdatePartial';

export type ExportChannelRequest = {
id: string;
Expand Down Expand Up @@ -1709,6 +1710,13 @@ export type PartialUserUpdate<UserType = UnknownType> = {
unset?: Array<keyof UserResponse<UserType>>;
};

export type MessageUpdatableFields<MessageType = UnknownType> = Omit<MessageResponse<MessageType>, 'cid' | 'created_at' | 'updated_at' | 'deleted_at' | 'user' | 'user_id'>

export type PartialMessageUpdate<MessageType = UnknownType> = {
set?: Partial<MessageUpdatableFields<MessageType>>;
unset?: Array<keyof MessageUpdatableFields<MessageType>>;
};

export type PermissionAPIObject = {
custom?: boolean;
name?: string;
Expand Down

0 comments on commit 5f713f7

Please sign in to comment.