diff --git a/src/channel.ts b/src/channel.ts index 365c404f4..13c7ff769 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -57,6 +57,7 @@ import { PollVoteData, SendMessageOptions, AscDesc, + PartialUpdateMemberAPIResponse, } from './types'; import { Role } from './permissions'; import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from './constants'; @@ -308,12 +309,12 @@ export class Channel>} Updated member */ - async partialUpdateMember(user_id: string, updates: PartialUpdateMember) { + async partialUpdateMember(user_id: string, updates: PartialUpdateMember) { if (!user_id) { throw Error('Please specify the user id'); } - return await this.getClient().patch>( + return await this.getClient().patch>( this._channelURL() + `/member/${encodeURIComponent(user_id)}`, updates, ); @@ -643,6 +644,50 @@ export class Channel>} The server response + * + * example: + * await channel.pin(); + * + * example server side: + * await channel.pin({user_id: userId}); + * + */ + async pin(opts: { user_id?: string } = {}) { + const cli = this.getClient(); + const uid = opts.user_id || cli.userID; + if (!uid) { + throw Error('A user_id is required for pinning a channel'); + } + const resp = await this.partialUpdateMember(uid, { set: { pinned: true } }); + return resp.channel_member; + } + + /** + * unpin - unpins the current channel + * @param {{ user_id?: string }} opts user_id if called server side + * @return {Promise>} The server response + * + * example: + * await channel.unpin(); + * + * example server side: + * await channel.unpin({user_id: userId}); + * + */ + async unpin(opts: { user_id?: string } = {}) { + const cli = this.getClient(); + const uid = opts.user_id || cli.userID; + if (!uid) { + throw Error('A user_id is required for unpinning a channel'); + } + const resp = await this.partialUpdateMember(uid, { set: { pinned: false } }); + return resp.channel_member; + } + /** * muteStatus - returns the mute status for the current channel * @return {{ muted: boolean; createdAt: Date | null; expiresAt: Date | null }} { muted: true | false, createdAt: Date | null, expiresAt: Date | null} diff --git a/src/types.ts b/src/types.ts index 29e9ea51d..982a7e594 100644 --- a/src/types.ts +++ b/src/types.ts @@ -331,6 +331,11 @@ export type ChannelMemberAPIResponse[]; }; +export type ChannelMemberUpdates = { + channel_role?: Role; + pinned?: boolean; +}; + export type ChannelMemberResponse = { ban_expires?: string; banned?: boolean; @@ -341,6 +346,7 @@ export type ChannelMemberResponse = APIResponse & { + channel_member: ChannelMemberResponse; +}; + export type CheckPushResponse = APIResponse & { device_errors?: { [deviceID: string]: { @@ -2491,9 +2503,9 @@ export type PartialUpdateChannel>; }; -export type PartialUpdateMember = { - set?: Partial>; - unset?: Array>; +export type PartialUpdateMember = { + set?: ChannelMemberUpdates; + unset?: Array; }; export type PartialUserUpdate = {