8000 feat: add blockedUsers state to StreamChat class by MartinCupela · Pull Request #1691 · GetStream/stream-chat-js · GitHub
[go: up one dir, main page]

Skip to content
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
36 changes: 31 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ type MessageComposerSetupFunction = ({
composer: MessageComposer;
}) => void | MessageComposerTearDownFunction;

export type BlockedUsersState = { userIds: string[] };

export type MessageComposerSetupState = {
/**
* Each `MessageComposer` runs this function each time its signature changes or
Expand Down Expand Up @@ -322,6 +324,7 @@ export class StreamChat {
moderation: Moderation;
mutedChannels: ChannelMute[];
mutedUsers: Mute[];
blockedUsers: StateStore<BlockedUsersState>;
node: boolean;
options: StreamChatOptions;
secret?: string;
Expand Down Expand Up @@ -382,6 +385,7 @@ export class StreamChat {
// a list of channels to hide ws events from
this.mutedChannels = [];
this.mutedUsers = [];
this.blockedUsers = new StateStore<BlockedUsersState>({ userIds: [] });

this.moderation = new Moderation(this);

Expand Down Expand Up @@ -1547,6 +1551,7 @@ export class StreamChat {
client.state.updateUser(event.me);
client.mutedChannels = event.me.channel_mutes;
client.mutedUsers = event.me.mutes;
client.blockedUsers.partialNext({ userIds: event.me.blocked_user_ids ?? [] });
}

if (event.channel && event.type === 'notification.message_new') {
Expand Down Expand Up @@ -2637,23 +2642,44 @@ export class StreamChat {
});
}
async blockUser(blockedUserID: string, user_id?: string) {
return await this.post<BlockUserAPIResponse>(this.baseURL + '/users/block', {
const result = await this.post<BlockUserAPIResponse>(this.baseURL + '/users/block', {
blocked_user_id: blockedUserID,
...(user_id ? { user_id } : {}),
});
if (this._cacheEnabled()) {
this.blockedUsers.next(({ userIds }) => ({
userIds: userIds.concat(blockedUserID),
}));
}
return result;
}

async getBlockedUsers(user_id?: string) {
return await this.get<GetBlockedUsersAPIResponse>(this.baseURL + '/users/block', {
...(user_id ? { user_id } : {}),
});
const result = await this.get<GetBlockedUsersAPIResponse>(
this.baseURL + '/users/block',
{
...(user_id ? { user_id } : {}),
},
);
if (this._cacheEnabled()) {
this.blockedUsers.partialNext({
userIds: result.blocks.map(({ blocked_user_id }) => blocked_user_id),
});
}
return result;
}

async unBlockUser(blockedUserID: string, userID?: string) {
return await this.post<APIResponse>(this.baseURL + '/users/unblock', {
const result = await this.post<APIResponse>(this.baseURL + '/users/unblock', {
blocked_user_id: blockedUserID,
...(userID ? { user_id: userID } : {}),
});
if (this._cacheEnabled()) {
this.blockedUsers.next(({ userIds }) => ({
userIds: userIds.filter((id) => id !== blockedUserID),
}));
}
return result;
}

/** getSharedLocations
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ export type BlockUserAPIResponse = APIResponse & {
export type GetBlockedUsersAPIResponse = APIResponse & {
blocks: BlockedUserDetails[];
};

export type BlockedUserDetails = APIResponse & {
blocked_user: UserResponse;
blocked_user_id: string;
Expand Down
Loading
0