Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHAT-1513] partial message update #576

Merged
merged 11 commits into from
May 18, 2021
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