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 channel pinning and archiving #1380

Closed
wants to merge 6 commits into from
Closed
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
93 changes: 91 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,94 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
});
}

/**
* archive - archives the current channel
* @param {{ user_id?: string }} opts user_id if called server side
* @return {Promise<ChannelMemberResponse<StreamChatGenerics>>} The server response
*
* example:
* await channel.archives();
*
* example server side:
* await channel.archive({user_id: userId});
*
*/
async archive(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 archiving a channel');
}
const resp = await this.partialUpdateMember(uid, { set: { archived: true } });
return resp.channel_member;
}

/**
* unarchive - unarchives 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 unarchive(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 unarchiving a channel');
}
const resp = await this.partialUpdateMember(uid, { set: { archived: false } });
return resp.channel_member;
}

/**
* 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
28 changes: 23 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,14 @@ export type ChannelMemberAPIResponse<StreamChatGenerics extends ExtendableGeneri
members: ChannelMemberResponse<StreamChatGenerics>[];
};

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

export type ChannelMemberResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
archived_at?: string;
ban_expires?: string;
banned?: boolean;
channel_role?: Role;
Expand All @@ -341,6 +348,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 +357,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 @@ -963,10 +977,10 @@ export type CreateChannelOptions<StreamChatGenerics extends ExtendableGenerics =
reminders?: boolean;
replies?: boolean;
search?: boolean;
skip_last_msg_update_for_system_msgs?: boolean;
typing_events?: boolean;
uploads?: boolean;
url_enrichment?: boolean;
skip_last_msg_update_for_system_msgs?: boolean;
};

export type CreateCommandOptions<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
Expand Down Expand Up @@ -1487,6 +1501,9 @@ export type ChannelFilters<StreamChatGenerics extends ExtendableGenerics = Defau
userType: StreamChatGenerics['userType'];
}>[Key]
>;
} & {
archived?: boolean;
pinned?: boolean;
}
>;

Expand Down Expand Up @@ -1794,7 +1811,8 @@ export type ReactionSortBase<StreamChatGenerics extends ExtendableGenerics = Def

export type ChannelSort<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> =
| ChannelSortBase<StreamChatGenerics>
| Array<ChannelSortBase<StreamChatGenerics>>;
| Array<ChannelSortBase<StreamChatGenerics>>
| { pinned_at: AscDesc };

export type ChannelSortBase<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = Sort<
StreamChatGenerics['channelType']
Expand Down Expand Up @@ -2485,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