Skip to content

Commit

Permalink
feat: add support for archiving channels (#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
akupila authored Nov 1, 2024
1 parent 7e39944 commit 2e74619
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,50 @@ 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.unarchive();
*
* example server side:
* await channel.unarchive({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
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ export type ChannelMemberAPIResponse<StreamChatGenerics extends ExtendableGeneri
};

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 Down Expand Up @@ -1499,6 +1501,9 @@ export type ChannelFilters<StreamChatGenerics extends ExtendableGenerics = Defau
userType: StreamChatGenerics['userType'];
}>[Key]
>;
} & {
archived?: boolean;
pinned?: boolean;
}
>;

Expand Down Expand Up @@ -1812,7 +1817,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

0 comments on commit 2e74619

Please sign in to comment.