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

feat: add support for pinning channels #1378

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -308,12 +309,12 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
*
* @return {Promise<ChannelMemberResponse<StreamChatGenerics>>} Updated member
*/
async partialUpdateMember(user_id: string, updates: PartialUpdateMember<StreamChatGenerics>) {
async partialUpdateMember(user_id: string, updates: PartialUpdateMember) {
if (!user_id) {
throw Error('Please specify the user id');
}

return await this.getClient().patch<ChannelMemberResponse<StreamChatGenerics>>(
return await this.getClient().patch<PartialUpdateMemberAPIResponse<StreamChatGenerics>>(
this._channelURL() + `/member/${encodeURIComponent(user_id)}`,
updates,
);
Expand Down Expand Up @@ -643,6 +644,50 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
});
}

/**
* pin - pins the current channel
* @param {{ user_id?: string }} opts user_id if called server side
* @return {Promise<ChannelMemberResponse<StreamChatGenerics>>} 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<ChannelMemberResponse<StreamChatGenerics>>} 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}
Expand Down
18 changes: 15 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ export type ChannelMemberAPIResponse<StreamChatGenerics extends ExtendableGeneri
members: ChannelMemberResponse<StreamChatGenerics>[];
};

export type ChannelMemberUpdates = {
channel_role?: Role;
pinned?: boolean;
};

export type ChannelMemberResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
ban_expires?: string;
banned?: boolean;
Expand All @@ -341,6 +346,7 @@ export type ChannelMemberResponse<StreamChatGenerics extends ExtendableGenerics
invited?: boolean;
is_moderator?: boolean;
notifications_muted?: boolean;
pinned_at?: string;
role?: string;
shadow_banned?: boolean;
status?: string;
Expand All @@ -349,6 +355,12 @@ export type ChannelMemberResponse<StreamChatGenerics extends ExtendableGenerics
user_id?: string;
};

export type PartialUpdateMemberAPIResponse<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = APIResponse & {
channel_member: ChannelMemberResponse<StreamChatGenerics>;
};

export type CheckPushResponse = APIResponse & {
device_errors?: {
[deviceID: string]: {
Expand Down Expand Up @@ -2491,9 +2503,9 @@ export type PartialUpdateChannel<StreamChatGenerics extends ExtendableGenerics =
unset?: Array<keyof ChannelResponse<StreamChatGenerics>>;
};

export type PartialUpdateMember<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
set?: Partial<ChannelMemberResponse<StreamChatGenerics>>;
unset?: Array<keyof ChannelMemberResponse<StreamChatGenerics>>;
export type PartialUpdateMember = {
set?: ChannelMemberUpdates;
unset?: Array<keyof ChannelMemberUpdates>;
};

export type PartialUserUpdate<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
Expand Down
Loading