Skip to content

Commit 83a24d2

Browse files
docs: add Blocking Users guide (#2542)
This guide is a copy of a [RN guide](https://github.com/GetStream/stream-chat-react-native/blob/develop/docusaurus/docs/reactnative/guides/blocking-users.mdx) with some adjustments to the code examples.
1 parent 18f1b09 commit 83a24d2

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
id: blocking-users
3+
title: Blocking Users
4+
---
5+
6+
## Introduction
7+
8+
Blocking users is an essential feature in a chat application because it enhances user safety and experience. It allows individuals to protect themselves from harassment, spam, and unwanted interactions. By giving users control over their interactions, it helps maintain privacy, reduces the risk of cyber-bullying, and promotes a respectful community atmosphere.
9+
10+
## Stream Chat
11+
12+
The Stream Chat SDK provides a way for blocking and unblocking users, as well as listing all of the blocked users.
13+
14+
When you block a user, you won't receive any direct messages from that user anymore. However, if you share a group with other participants, you will still receive messages from the blocked user.
15+
16+
In this cookbook, we will see how to implement this feature in your chat application, using the Stream Chat SDK.
17+
18+
## Low Level Client support
19+
20+
The low-level client provides the following methods related to user blocking.
21+
22+
### Blocking a user
23+
24+
In order to block a user, you need to use the `blockUser` method of the client instance. This method takes the user id of the user you wish to block.
25+
26+
```tsx
27+
import { StreamChat } from 'stream-chat';
28+
const chatClient = StreamChat.getInstance('<API_KEY>');
29+
30+
const blockUser = async (userId: string) => {
31+
try {
32+
await chatClient.blockUser(userId);
33+
} catch (error) {
34+
console.log('Error blocking user:', error);
35+
}
36+
};
37+
```
38+
39+
### Unblocking a user
40+
41+
Similarly, to unblock a blocked user, you need to use the `unBlockUser` method of the client instance. This method takes the user id of the user you wish to unblock.
42+
43+
```tsx
44+
import { StreamChat } from 'stream-chat';
45+
const chatClient = StreamChat.getInstance('<API_KEY>');
46+
47+
const unBlockUser = async (userId: string) => {
48+
try {
49+
await chatClient.unBlockUser(userId);
50+
} catch (error) {
51+
console.log('Error UnBlocking user:', error);
52+
}
53+
};
54+
```
55+
56+
### Listing Blocked Users
57+
58+
To list all the blocked users, you can use the `getBlockedUsers` method of the client instance.
59+
60+
```tsx
61+
const chatClient = StreamChat.getInstance('<API_KEY>');
62+
63+
const getBlockedUsers = async () => {
64+
try {
65+
const users = await chatClient.getBlockedUsers();
66+
setBlockedUsers(users.blocks);
67+
} catch (error) {
68+
console.log('Error getting blocked users:', error);
69+
}
70+
};
71+
```
72+
73+
:::note
74+
All of these actions can be triggered _after_ the client connection (`client.connectUser`) is established.
75+
:::
76+
77+
## Message Actions
78+
79+
You can use the logic above to create your own custom message actions that will involve user blocking.
80+
81+
This can be done by using the `CustomMessageActionsList` prop of the `Channel` component. You can follow the guide [here](../guides/theming/actions/message-actions.mdx#using-custommessageactionlist-component).
82+
83+
```tsx
84+
import { Channel, messageActions } from 'stream-chat-react-native';
85+
86+
const CustomMessageActionList = () => {
87+
const { client } = useChatContext();
88+
const { message } = useMessageContext();
89+
90+
return (
91+
<>
92+
<button
93+
className='str-chat__message-actions-list-item-button'
94+
onClick={() => client.blockUser(message.user_id!)}
95+
>
96+
Block <strong>{message.user?.name ?? message.user?.id}</strong>
97+
</button>
98+
99+
{/** ...other action buttons... */}
100+
</>
101+
);
102+
};
103+
104+
<Channel CustomMessageActionList={CustomMessageActionList}>...</Channel>;
105+
```

docusaurus/docs/React/guides/theming/actions/message-actions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const CustomMessageActionList = () => {
5757
return (
5858
<>
5959
<button
60-
className='str-chat__message-actions-list-item str-chat__message-actions-list-item-button'
60+
className='str-chat__message-actions-list-item-button'
6161
onClick={(event) => {
6262
window.alert(`Yell action clicked on message: ${message.id}!`);
6363
}}

docusaurus/sidebars-react.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@
150150
"guides/sdk-state-management",
151151
"guides/date-time-formatting",
152152
"guides/custom-threads-view",
153-
"guides/dialog-management"
153+
"guides/dialog-management",
154+
"guides/blocking-users"
154155
]
155156
},
156157
{

0 commit comments

Comments
 (0)