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 typescript support for custom channel member fields #1389

Merged
merged 3 commits into from
Nov 13, 2024
Merged
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
34 changes: 22 additions & 12 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import {
InviteOptions,
MarkReadOptions,
MarkUnreadOptions,
MemberFilters,
MemberSort,
Message,
MessageFilters,
MessagePaginationOptions,
MessageResponse,
MessageSetType,
MuteChannelAPIResponse,
NewMemberPayload,
PartialUpdateChannel,
PartialUpdateChannelAPIResponse,
PartialUpdateMember,
Expand All @@ -51,7 +53,6 @@ import {
TruncateChannelAPIResponse,
TruncateOptions,
UpdateChannelAPIResponse,
UserFilters,
UserResponse,
QueryChannelAPIResponse,
PollVoteData,
Expand Down Expand Up @@ -265,15 +266,15 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
/**
* queryMembers - Query Members
*
* @param {UserFilters<StreamChatGenerics>} filterConditions object MongoDB style filters
* @param {MemberFilters<StreamChatGenerics>} filterConditions object MongoDB style filters
* @param {MemberSort<StreamChatGenerics>} [sort] Sort options, for instance [{created_at: -1}].
* When using multiple fields, make sure you use array of objects to guarantee field order, for instance [{name: -1}, {created_at: 1}]
* @param {{ limit?: number; offset?: number }} [options] Option object, {limit: 10, offset:10}
*
* @return {Promise<ChannelMemberAPIResponse<StreamChatGenerics>>} Query Members response
*/
async queryMembers(
filterConditions: UserFilters<StreamChatGenerics>,
filterConditions: MemberFilters<StreamChatGenerics>,
sort: MemberSort<StreamChatGenerics> = [],
options: QueryMembersOptions = {},
) {
Expand Down Expand Up @@ -309,7 +310,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
*
* @return {Promise<ChannelMemberResponse<StreamChatGenerics>>} Updated member
*/
async partialUpdateMember(user_id: string, updates: PartialUpdateMember) {
async partialUpdateMember(user_id: string, updates: PartialUpdateMember<StreamChatGenerics>) {
if (!user_id) {
throw Error('Please specify the user id');
}
Expand Down Expand Up @@ -517,13 +518,13 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
/**
* addMembers - add members to the channel
*
* @param {{user_id: string, channel_role?: Role}[]} members An array of members to add to the channel
* @param {string[] | Array<NewMemberPayload<StreamChatGenerics>>} members An array of members to add to the channel
* @param {Message<StreamChatGenerics>} [message] Optional message object for channel members notification
* @param {ChannelUpdateOptions} [options] Option object, configuration to control the behavior while updating
* @return {Promise<UpdateChannelAPIResponse<StreamChatGenerics>>} The server response
*/
async addMembers(
members: string[] | { user_id: string; channel_role?: Role }[],
members: string[] | Array<NewMemberPayload<StreamChatGenerics>>,
message?: Message<StreamChatGenerics>,
options: ChannelUpdateOptions = {},
) {
Expand Down Expand Up @@ -561,13 +562,13 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
/**
* inviteMembers - invite members to the channel
*
* @param {{user_id: string, channel_role?: Role}[]} members An array of members to invite to the channel
* @param {string[] | Array<NewMemberPayload<StreamChatGenerics>>} members An array of members to invite to the channel
* @param {Message<StreamChatGenerics>} [message] Optional message object for channel members notification
* @param {ChannelUpdateOptions} [options] Option object, configuration to control the behavior while updating
* @return {Promise<UpdateChannelAPIResponse<StreamChatGenerics>>} The server response
*/
async inviteMembers(
members: { user_id: string; channel_role?: Role }[] | string[],
members: string[] | Array<NewMemberPayload<StreamChatGenerics>>,
message?: Message<StreamChatGenerics>,
options: ChannelUpdateOptions = {},
) {
Expand Down Expand Up @@ -704,7 +705,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
const cli = this.getClient();
const uid = opts.user_id || cli.userID;
if (!uid) {
throw Error('A user_id is required for pinning a channel');
throw new Error('A user_id is required for pinning a channel');
}
const resp = await this.partialUpdateMember(uid, { set: { pinned: true } });
return resp.channel_member;
Expand All @@ -726,7 +727,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
const cli = this.getClient();
const uid = opts.user_id || cli.userID;
if (!uid) {
throw Error('A user_id is required for unpinning a channel');
throw new Error('A user_id is required for unpinning a channel');
}
const resp = await this.partialUpdateMember(uid, { set: { pinned: false } });
return resp.channel_member;
Expand Down Expand Up @@ -1515,12 +1516,21 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
case 'member.added':
case 'member.updated':
if (event.member?.user_id) {
channelState.members[event.member.user_id] = event.member;
channelState.members = {
...channelState.members,
[event.member.user_id]: event.member,
};
}
break;
case 'member.removed':
if (event.user?.id) {
delete channelState.members[event.user.id];
const newMembers = {
...channelState.members,
};

delete newMembers[event.user.id];

channelState.members = newMembers;
}
break;
case 'notification.mark_unread': {
Expand Down
Loading
Loading