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.

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.

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.

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);
  }
};

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.

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>;
© Getstream.io, Inc. All Rights Reserved.