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.

📘

The user can have multiple privacy lists, but only one can be active.

Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.

Before you begin

  1. Register a QuickBlox account. 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 our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
  4. Connect to the Chat server. See our 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.

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

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

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.

📘

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

Activate privacy list

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

try {
    privacyListsManager.applyPrivacyList("My_Privacy_List");
} catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException exception) {

}
try {
    privacyListsManager.applyPrivacyList("My_Privacy_List")
} catch (exceptione: SmackException.NotConnectedException) {

} catch (exception: SmackException.NoResponseException) {
    
} catch (exception: XMPPException) {
    
}

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

}
// 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) {
    
}

Retrieve privacy lists

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

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

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

}

Retrieve privacy list by name

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

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

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

}

Remove privacy list

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

QBPrivacyListsManager privacyListsManager = QBChatService.getInstance().getPrivacyListsManager();

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

}
val privacyListsManager = QBChatService.getInstance().privacyListsManager

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

} catch (exception: SmackException.NoResponseException) {

} catch (exception: XMPPException) {

}

🚧

Before deleting the privacy list, you should decline it.

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.

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

What’s Next