> ## 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, or delete privacy lists, define a default list.

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/js-setup) page for more details. 3. Create a user session to be able to use QuickBlox functionality. See [Authentication](/sdks/js-authentication) page to learn how to do it.
3. Connect to the Chat server. See [Connection](/sdks/js-chat-connection) page to learn how to do it.

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

## Create privacy list

A privacy list must have at least one element in order to be created. You can choose a type of blocked logic. There are 2 types:

* Block in one way. When you blocked a user, but you can send messages to them.
* Block in two ways. When you blocked a user, but you can't send messages to them.

```JavaScript JavaScript theme={null}
var users = [
  { user_id: 34, action: "deny" },
  { user_id: 48, action: "deny", mutualBlock: true }, // it means you can't write to user
  { user_id: 18, action: "allow" }
];
var list = { name: "myList", items: users };

try {
  QB.chat.privacylist.create(list, function(error) {});
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

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

```JavaScript JavaScript theme={null}
try {
  QB.chat.privacylist.setAsDefault("myList", function(error) {

  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

## Update privacy list

There is a rule you should follow to update a privacy list: if you want to update or set a new privacy list instead of the current one, you should decline the current default list first.

```JavaScript JavaScript theme={null}
try {
  QB.chat.privacylist.setAsDefault(null, function(error) {
    if (!error) {
      var users = [{ user_id: 34, action: "allow" }];
      var list = { name: "myList", items: users };

      QB.chat.privacylist.update(list, function(error) {
        if (!error) {
          QB.chat.privacylist.setAsDefault("myList", function(error) {});
        }
      });
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

## Retrieve privacy lists

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

```JavaScript JavaScript theme={null}
try {
  QB.chat.privacylist.getNames(function(error, result) {
    if (!error) {
      var names = result.names;
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

## Retrieve privacy list by name

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

```JavaScript JavaScript theme={null}
try {
  QB.chat.privacylist.getList("myList", function(error, result) {
    if (!error) {
      var name = result.name;
      var items = result.items;
    }
  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

## Remove privacy list

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

```JavaScript JavaScript theme={null}
try {
  QB.chat.privacylist.delete("myList", function(error) {

  });
} catch (e) {
  if (e.name === 'ChatNotConnectedError') {
    // not connected to chat
  }
}
```

## 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**.

```JavaScript JavaScript theme={null}
QB.chat.onMessageErrorListener = function (messageId, error){

}
```
