> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quickblox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Privacy List

> Learn how to create privacy lists and implement user-to-user blocks.

Privacy list API allows enabling or disabling communication with other users in a chat. You can create, modify, delete privacy lists or define a default list.

<Note>
  The user can have multiple privacy lists, but only one can be active.
</Note>

Visit [Key Concepts](/docs/key-concepts) page to learn the most important QuickBlox concepts.

## Before you begin

1. Register a [QuickBlox account](https://admin.quickblox.com/signin). This is a matter of a few minutes and you will be able to use this account to build your apps.
2. Configure QuickBlox SDK for your app. Check out [Setup](/sdks/android-setup) page for more details.
3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/android-authentication) page to learn how to do it.
4. Connect to the Chat server. See [Connection](/sdks/android-chat-connection) page to learn how to do it.

## Create privacy list

A privacy list must have at least one element in order to be created. If no elements are specified, then the list with a given name will be deleted.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBPrivacyListsManager privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();

    QBPrivacyList privacyList = new QBPrivacyList();
    privacyList.setName("My_Privacy_List");

    ArrayList<QBPrivacyListItem> items = new ArrayList<>();

    QBPrivacyListItem item = new QBPrivacyListItem();
    item.setAllow(false);
    item.setType(QBPrivacyListItem.Type.USER_ID);
    item.setValueForType(String.valueOf(user.getId()));

    items.add(item);

    privacyList.setItems(items);

    try {
        privacyListsManager.createPrivacyList(privacyList);
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val privacyListsManager = QBChatService.getInstance().privacyListsManager

    val privacyList = QBPrivacyList()
    privacyList.name = "My_Privacy_List"

    val items = ArrayList<QBPrivacyListItem>()

    val item = QBPrivacyListItem()
    item.isAllow = false
    item.type = QBPrivacyListItem.Type.USER_ID
    item.valueForType = user.id.toString()

    items.add(item)

    privacyList.items = items

    try {
        privacyListsManager.createPrivacyList(privacyList)
    } catch (exception: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

The `QBPrivacyListItem` class takes 4 arguments:

* **type** - use `USER_ID` to block a user in a 1-1 chat or `GROUP_USER_ID` to block in a group chat.
* **valueForType** - ID of a user to apply an action.
* **allow** - can be `true` or `false`.
* **mutualBlock** - can be `true` or `false` to block user's message in both directions or not.

<Note>
  In order to be used the privacy list should be not only set but also activated (set as default).
</Note>

## Activate privacy list

In order to activate rules from a privacy list, you should set it as default.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    try {
        privacyListsManager.applyPrivacyList("My_Privacy_List");
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    try {
        privacyListsManager.applyPrivacyList("My_Privacy_List")
    } catch (exceptione: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

## Update privacy list

There are some rules you should follow to update a privacy list:

* Include **all** of the desired items (not a "delta").
* If you want to update or set a new privacy list instead of the current one, you should decline the current default list first.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    // deactivate active list
    try {
        privacyListsManager.declinePrivacyList();
    } catch (SmackException | XMPPException.XMPPErrorException exception) {

    }

    // create new list
    // ...

    // activate again active list
    try {
        privacyListsManager.applyPrivacyList("New_Privacy_List");
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    // deactivate active list
    try {
        privacyListsManager.declinePrivacyList()
    } catch (exception: SmackException) {

    } catch (exception: XMPPException.XMPPErrorException) {
    }

    // create new list
    // ...

    // activate again active list
    try {
        privacyListsManager.applyPrivacyList("New_Privacy_List")
    } catch (exception: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

## Retrieve privacy lists

To get a list of **all** your privacy lists names, use the following request.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBPrivacyListsManager privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();

    // get all privacy lists
    List<QBPrivacyList> lists = new ArrayList<>();

    try {
        lists = privacyListsManager.getPrivacyLists();
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val privacyListsManager = QBChatService.getInstance().privacyListsManager

    // get all privacy lists
    var lists: List<QBPrivacyList> = ArrayList()

    try {
        lists = privacyListsManager.privacyLists
    } catch (exception: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

## Retrieve privacy list by name

To get the privacy list by name, you should use the following method.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBPrivacyListsManager privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();

    // get privacy list by name
    QBPrivacyList privacyList = new QBPrivacyList();
    try {
        privacyList = privacyListsManager.getPrivacyList("New_Privacy_List");
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val privacyListsManager = QBChatService.getInstance().privacyListsManager

    // get privacy list by name
    var privacyList = QBPrivacyList()
    try {
        privacyList = privacyListsManager.getPrivacyList("New_Privacy_List")
    } catch (exception: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

## Remove privacy list

To delete a list, you can call a method below or you can edit a list and set items to `nil`.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBPrivacyListsManager privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();

    try {
        privacyListsManager.declinePrivacyList();
        privacyListsManager.deletePrivacyList("New_Privacy_List");
    } catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

    }
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val privacyListsManager = QBChatService.getInstance().privacyListsManager

    try {
        privacyListsManager.declinePrivacyList()
        privacyListsManager.deletePrivacyList("New_Privacy_List")
    } catch (exception: SmackException.NotConnectedException) {

    } catch (exception: SmackException.NoResponseException) {

    } catch (exception: XMPPException) {

    }
    ```
  </Tab>
</Tabs>

<Warning>
  Before deleting the privacy list, you should decline it.
</Warning>

## Blocked user attempts to communicate with user

A user can be blocked in **1-1 dialog** and **group dialog**. In this case, the blocked user receives an error when trying to send a message in a **1-1 dialog** and receives nothing when trying to send a message in **group dialog**.

<Tabs>
  <Tab title="Java">
    ```Java theme={null}
    QBChatMessage chatMessage = new QBChatMessage();
    chatMessage.setBody("I am blocked. I am trying to send message to you");

    try {
        privateDialog.sendMessage(chatMessage);
    } catch (SmackException.NotConnectedException exception) {
        exception.printStackTrace();
    }

    privateDialog.addMessageListener(new QBChatDialogMessageListener() {
        @Override
        public void processMessage(String dialogId, QBChatMessage chatMessage, Integer integer) {
            // when we received a message from dialog
        }

        @Override
        public void processError(String dialogId, QBChatException exception, QBChatMessage chatMessage, Integer senderId) {
            // when we received an error from dialog
            exception.printStackTrace();
        }
    });
    ```
  </Tab>

  <Tab title="Kotlin">
    ```Kotlin theme={null}
    val chatMessage = QBChatMessage()
    chatMessage.setBody("I am blocked. I am trying to send message to you")

    try {
        privateDialog.sendMessage(chatMessage)
    } catch (exception: SmackException.NotConnectedException) {
        exception.printStackTrace()
    }

    privateDialog.addMessageListener(object : QBChatDialogMessageListener() {
        override fun processMessage(dialogId: String?, chatMessage: QBChatMessage?, integer: Int?) {
        // when we received a message from dialog
        }

        override fun processError(dialogId: String?, exception: QBChatException?, chatMessage: QBChatMessage?, senderId: Int?) {
            // when we received an error from dialog
            exception?.printStackTrace()
        }
    })
    ```
  </Tab>
</Tabs>
