Skip to content

Commit 2370fd8

Browse files
authored
feat: add blockedUsers state to StreamChat class (#1691)
1 parent 91f6758 commit 2370fd8

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/client.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ type MessageComposerSetupFunction = ({
273273
composer: MessageComposer;
274274
}) => void | MessageComposerTearDownFunction;
275275

276+
export type BlockedUsersState = { userIds: string[] };
277+
276278
export type MessageComposerSetupState = {
277279
/**
278280
* Each `MessageComposer` runs this function each time its signature changes or
@@ -322,6 +324,7 @@ export class StreamChat {
322324
moderation: Moderation;
323325
mutedChannels: ChannelMute[];
324326
mutedUsers: Mute[];
327+
blockedUsers: StateStore<BlockedUsersState>;
325328
node: boolean;
326329
options: StreamChatOptions;
327330
secret?: string;
@@ -382,6 +385,7 @@ export class StreamChat {
382385
// a list of channels to hide ws events from
383386
this.mutedChannels = [];
384387
this.mutedUsers = [];
388+
this.blockedUsers = new StateStore<BlockedUsersState>({ userIds: [] });
385389

386390
this.moderation = new Moderation(this);
387391

@@ -1547,6 +1551,7 @@ export class StreamChat {
15471551
client.state.updateUser(event.me);
15481552
client.mutedChannels = event.me.channel_mutes;
15491553
client.mutedUsers = event.me.mutes;
1554+
client.blockedUsers.partialNext({ userIds: event.me.blocked_user_ids ?? [] });
15501555
}
15511556

15521557
if (event.channel && event.type === 'notification.message_new') {
@@ -2637,23 +2642,44 @@ export class StreamChat {
26372642
});
26382643
}
26392644
async blockUser(blockedUserID: string, user_id?: string) {
2640-
return await this.post<BlockUserAPIResponse>(this.baseURL + '/users/block', {
2645+
const result = await this.post<BlockUserAPIResponse>(this.baseURL + '/users/block', {
26412646
blocked_user_id: blockedUserID,
26422647
...(user_id ? { user_id } : {}),
26432648
});
2649+
if (this._cacheEnabled()) {
2650+
this.blockedUsers.next(({ userIds }) => ({
2651+
userIds: userIds.concat(blockedUserID),
2652+
}));
2653+
}
2654+
return result;
26442655
}
26452656

26462657
async getBlockedUsers(user_id?: string) {
2647-
return await this.get<GetBlockedUsersAPIResponse>(this.baseURL + '/users/block', {
2648-
...(user_id ? { user_id } : {}),
2649-
});
2658+
const result = await this.get<GetBlockedUsersAPIResponse>(
2659+
this.baseURL + '/users/block',
2660+
{
2661+
...(user_id ? { user_id } : {}),
2662+
},
2663+
);
2664+
if (this._cacheEnabled()) {
2665+
this.blockedUsers.partialNext({
2666+
userIds: result.blocks.map(({ blocked_user_id }) => blocked_user_id),
2667+
});
2668+
}
2669+
return result;
26502670
}
26512671

26522672
async unBlockUser(blockedUserID: string, userID?: string) {
2653-
return await this.post<APIResponse>(this.baseURL + '/users/unblock', {
2673+
const result = await this.post<APIResponse>(this.baseURL + '/users/unblock', {
26542674
blocked_user_id: blockedUserID,
26552675
...(userID ? { user_id: userID } : {}),
26562676
});
2677+
if (this._cacheEnabled()) {
2678+
this.blockedUsers.next(({ userIds }) => ({
2679+
userIds: userIds.filter((id) => id !== blockedUserID),
2680+
}));
2681+
}
2682+
return result;
26572683
}
26582684

26592685
/** getSharedLocations

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ export type BlockUserAPIResponse = APIResponse & {
819819
export type GetBlockedUsersAPIResponse = APIResponse & {
820820
blocks: BlockedUserDetails[];
821821
};
822+
822823
export type BlockedUserDetails = APIResponse & {
823824
blocked_user: UserResponse;
824825
blocked_user_id: string;

0 commit comments

Comments
 (0)