From 41595f7859fb63f57a96bd9272f4d000c5c94909 Mon Sep 17 00:00:00 2001 From: Antti Kupila Date: Fri, 18 Oct 2024 12:15:23 +0300 Subject: [PATCH 1/5] split out PartialUpdateMember request from response --- src/channel.ts | 2 +- src/types.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/channel.ts b/src/channel.ts index 2117816fe..172be6de3 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -308,7 +308,7 @@ 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'); } diff --git a/src/types.ts b/src/types.ts index 152f35afc..b0ca92ca8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -331,6 +331,10 @@ export type ChannelMemberAPIResponse[]; }; +export type ChannelMemberUpdates = { + channel_role?: Role; +}; + export type ChannelMemberResponse = { ban_expires?: string; banned?: boolean; @@ -2484,9 +2488,9 @@ export type PartialUpdateChannel>; }; -export type PartialUpdateMember = { - set?: Partial>; - unset?: Array>; +export type PartialUpdateMember = { + set?: ChannelMemberUpdates; + unset?: Array; }; export type PartialUserUpdate = { From 0411b8a6f15a24333a24623cb120a4b24715de63 Mon Sep 17 00:00:00 2001 From: Antti Kupila Date: Fri, 18 Oct 2024 12:16:19 +0300 Subject: [PATCH 2/5] feat: add channel pin --- src/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/types.ts b/src/types.ts index b0ca92ca8..8c1443409 100644 --- a/src/types.ts +++ b/src/types.ts @@ -333,6 +333,7 @@ export type ChannelMemberAPIResponse = { @@ -351,6 +352,7 @@ export type ChannelMemberResponse; user_id?: string; + pinned_at?: string; }; export type CheckPushResponse = APIResponse & { From 1d7bdb36cad1ca8948877ba1385b74bdf7c2a162 Mon Sep 17 00:00:00 2001 From: Antti Kupila Date: Fri, 18 Oct 2024 15:39:13 +0300 Subject: [PATCH 3/5] fix response containing channel_member key --- src/channel.ts | 3 ++- src/types.ts | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/channel.ts b/src/channel.ts index 172be6de3..fcb5164e9 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'; @@ -313,7 +314,7 @@ export class Channel>( + return await this.getClient().patch>( this._channelURL() + `/member/${encodeURIComponent(user_id)}`, updates, ); diff --git a/src/types.ts b/src/types.ts index 8c1443409..22d6236e1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -355,6 +355,12 @@ export type ChannelMemberResponse = APIResponse & { + channel_member: ChannelMemberResponse; +}; + export type CheckPushResponse = APIResponse & { device_errors?: { [deviceID: string]: { From 1c9acb98a5eaf4351015c01f87e54dade7b5e4fd Mon Sep 17 00:00:00 2001 From: Antti Kupila Date: Mon, 21 Oct 2024 11:38:51 +0300 Subject: [PATCH 4/5] add pin/unpin methods to Channel --- src/channel.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/channel.ts b/src/channel.ts index fcb5164e9..a7478d53c 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -644,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} From eb4a18081dbb8fa0a6f5fc48ff4c2e00721cca9b Mon Sep 17 00:00:00 2001 From: Antti Kupila Date: Mon, 21 Oct 2024 15:17:34 +0300 Subject: [PATCH 5/5] types: sort fields alphabetically --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 22d6236e1..af077af85 100644 --- a/src/types.ts +++ b/src/types.ts @@ -346,13 +346,13 @@ export type ChannelMemberResponse; user_id?: string; - pinned_at?: string; }; export type PartialUpdateMemberAPIResponse<