Skip to content

Commit

Permalink
docs: add Blocking Users guide (#2542)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arnautov-anton authored Oct 23, 2024
1 parent 18f1b09 commit 83a24d2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
105 changes: 105 additions & 0 deletions docusaurus/docs/React/guides/blocking-users.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
id: blocking-users
title: Blocking Users
---

## Introduction

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.

## Stream Chat

The Stream Chat SDK provides a way for blocking and unblocking users, as well as listing all of the blocked users.

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.

In this cookbook, we will see how to implement this feature in your chat application, using the Stream Chat SDK.

## Low Level Client support

The low-level client provides the following methods related to user blocking.

### Blocking a user

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.

```tsx
import { StreamChat } from 'stream-chat';
const chatClient = StreamChat.getInstance('<API_KEY>');

const blockUser = async (userId: string) => {
try {
await chatClient.blockUser(userId);
} catch (error) {
console.log('Error blocking user:', error);
}
};
```

### Unblocking a user

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.

```tsx
import { StreamChat } from 'stream-chat';
const chatClient = StreamChat.getInstance('<API_KEY>');

const unBlockUser = async (userId: string) => {
try {
await chatClient.unBlockUser(userId);
} catch (error) {
console.log('Error UnBlocking user:', error);
}
};
```

### Listing Blocked Users

To list all the blocked users, you can use the `getBlockedUsers` method of the client instance.

```tsx
const chatClient = StreamChat.getInstance('<API_KEY>');

const getBlockedUsers = async () => {
try {
const users = await chatClient.getBlockedUsers();
setBlockedUsers(users.blocks);
} catch (error) {
console.log('Error getting blocked users:', error);
}
};
```

:::note
All of these actions can be triggered _after_ the client connection (`client.connectUser`) is established.
:::

## Message Actions

You can use the logic above to create your own custom message actions that will involve user blocking.

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).

```tsx
import { Channel, messageActions } from 'stream-chat-react-native';

const CustomMessageActionList = () => {
const { client } = useChatContext();
const { message } = useMessageContext();

return (
<>
<button
className='str-chat__message-actions-list-item-button'
onClick={() => client.blockUser(message.user_id!)}
>
Block <strong>{message.user?.name ?? message.user?.id}</strong>
</button>

{/** ...other action buttons... */}
</>
);
};

<Channel CustomMessageActionList={CustomMessageActionList}>...</Channel>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const CustomMessageActionList = () => {
return (
<>
<button
className='str-chat__message-actions-list-item str-chat__message-actions-list-item-button'
className='str-chat__message-actions-list-item-button'
onClick={(event) => {
window.alert(`Yell action clicked on message: ${message.id}!`);
}}
Expand Down
3 changes: 2 additions & 1 deletion docusaurus/sidebars-react.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@
"guides/sdk-state-management",
"guides/date-time-formatting",
"guides/custom-threads-view",
"guides/dialog-management"
"guides/dialog-management",
"guides/blocking-users"
]
},
{
Expand Down

0 comments on commit 83a24d2

Please sign in to comment.