On this page

Moderate misbehaving users as a chat administrator

note

Enable App Context in the Admin Portal to work with user metadata.

Administrators are chat users with SDK instances initialized with SecretKey. Admin moderation capabilities:

  • Mute users on channels
  • Ban users from accessing channels

Use Access Manager to enforce restrictions. See also moderation for regular users.

Mute or ban users as an administrator

Mute or ban users with SetRestriction() or SetRestrictions() on the Chat, User, or Channel object. All three produce the same output with different input parameters.

How it works:

  • Muting/banning creates a moderation event (muted or banned)
  • A moderation membership is created with PUBNUB_INTERNAL_MODERATION_ prefix (filtered from GetMemberships() results)
  • Lifting restrictions removes the moderation membership and creates a lifted event

Listen to moderation events to trigger actions like removing memberships. Check restrictions to verify user status.

Secret Key required

Initialize with Secret Key (from Admin Portal) for admin operations. Never expose SecretKey to clients. If compromised, generate a new one in the Admin Portal.

Method signature

These methods take the following parameters:

  • SetRestriction() (on the Chat object)

    1chat.SetRestriction(
    2 string userId,
    3 string channelId,
    4 Restriction restriction
    5)
  • SetRestriction() (on the User object)

    1user.SetRestriction(
    2 string channelId,
    3 Restriction restriction
    4)
  • SetRestrictions() (on the Channel object)

    1channel.SetRestrictions(
    2 string userId,
    3 Restriction restriction
    4)

Input

ParameterRequired for ChatRequired for UserRequired for ChannelDescription
userId
Type: string
Default:
n/a
Yes
No
Yes
Unique User ID that becomes your app's current user. It's a string of up to 92 characters that identifies a single client (end user, device, or server) that connects to PubNub. Based on User ID, PubNub calculates pricing for your apps' usage. User ID should be persisted and remain unchanged. If you don't set userId, you won't be able to connect to PubNub. In this method, userId stands for the user that you want to mute or ban.
channelId
Type: string
Default:
n/a
Yes
Yes
No
ID of the channel on/from which the user should be muted or banned.
restriction
Type: Restriction
Default:
n/a
Yes
Yes
Yes
Moderation restrictions and reasoning behind them.
 → ban
Type: boolean
Default:
n/a
No
No
No
Value that represents the user's moderation restrictions. Set to true to ban the user from the channel or to false to unban them.
 → mute
Type: boolean
Default:
n/a
No
No
No
Value that represents the user's moderation restrictions. Set to true to mute the user on the channel or to false to unmute them.
 → reason
Type: string
Default:
n/a
No
No
No
Reason why you want to ban or mute the user.

Output

An awaitable Task<ChatOperationResult>.

Sample code

Mute

Mute support_agent_15 on the support channel.

  • SetRestriction() (on the Chat object)

    1
    
  • SetRestriction() (on the User object)

    1
    
  • SetRestrictions() (on the Channel object)

    1
    

Ban

Ban support_agent_15 from the support channel.

  • SetRestriction() (on the Chat object)

    1
    
  • SetRestriction() (on the User object)

    1
    
  • SetRestrictions() (on the Channel object)

    1
    

Check restrictions

One user on one channel

Check if there are any mute or ban restrictions set for a user on one channel using the GetChannelRestrictions() and GetUserRestrictions() methods.

Method signature

These methods take the following parameters:

  • GetChannelRestrictions()

    1user.GetChannelRestrictions(Channel channel)
  • GetUserRestrictions()

    1channel.GetUserRestrictions(User user)
Input
ParameterRequired in GetChannelRestrictions()Required in GetUserRestrictions()Description
channel
Type: Channel
Default:
n/a
Yes
No
Channel on/from which the user can be muted or banned.
user
Type: User
Default:
n/a
No
Yes
User that can be muted or banned.
Output

These methods return an awaitable Task<ChatOperationResult<Restriction>> object.

1 public class Restriction
2 {
3 public bool Ban;
4 public bool Mute;
5 public string Reason;
6 }

Sample code

Check if the user support_agent_15 has any restrictions set on the support channel.

  • GetChannelRestrictions()

    1
    
  • GetUserRestrictions()

    1
    

One user on all channels

Check if there are any mute or ban restrictions set for a user on all channels they are a member of using the GetChannelsRestrictions() method.

Method signature

This method takes the following parameters:

1user.GetChannelsRestrictions(
2 string sort = "",
3 int limit = 0,
4 PNPageObject page = null
5)
Input
* required
ParameterDescription
sort
Type: string
Default:
empty string
Key-value pair of a property to sort by, and a sort direction. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. By default, the items are sorted by the last updated date.
limit
Type: int
Default:
0
Number of objects to return in response.
page
Type: PNPageObject
Default:
null
Object used for pagination to define which previous or next result page you want to fetch.
Output
TypeDescription
Task<ChatOperationResult<ChannelsRestrictionsWrapper>>
An awaitable Task with an object containing the filtered, sorted, and paginated list of user restrictions.

Sample code

List all mute and ban restrictions set for the user support_agent_15.

1

All users on one channel

Check if there are any mute or ban restrictions set for members of a given channel using the GetUsersRestrictions() method.

Method signature

This method takes the following parameters:

1channel.GetUsersRestrictions(
2 string sort = "",
3 int limit = 0,
4 PNPageObject page = null
5)
Input
* required
ParameterDescription
sort
Type: string
Default:
empty string
Key-value pair of a property to sort by, and a sort direction. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. By default, the items are sorted by the last updated date.
limit
Type: int
Default:
0
Number of objects to return in response.
page
Type: PNPageObject
Default:
null
Object used for pagination to define which previous or next result page you want to fetch.
Output
TypeDescription
Task<ChatOperationResult<UsersRestrictionsWrapper>>
An awaitable Task with an object containing the filtered, sorted, and paginated list of restricted users.

Sample code

List all mute and ban restrictions set for the support channel.

1

Secure moderation

Client-side UI restrictions (hiding channels, disabling input) can be bypassed. Secure with server-side logic using Access Manager, plus optional client-side feedback.

note

See Moderation as user for client-side moderation options.

Server-side restrictions

Use Access Manager with Chat SDK methods to grant/revoke permissions based on muting/banning restrictions.

Access Manager permissions

With SyncMutedUsers enabled, grant these permissions (replace $currentUserId with actual user ID):

  • read on PN_PRV.$currentUserId.mute1 channel
  • update, delete, get on PN_PRV.$currentUserId.mute1 user

Recommended workflow:

  1. Admin sets restrictions via dashboard
  2. Get moderation restrictions for users
  3. Call Access Manager API to generate/revoke tokens

Implementation steps:

  1. Enable Access Manager.

    Navigate to your app's keyset in the Admin Portal and turn on the ACCESS MANAGER option.

  2. Initialize Unity Chat SDK with AuthKey.

    On the frontend of your app, initialize the Unity Chat SDK with the authentication key (AuthKey) on your clients. Use it for all requests made to PubNub APIs to authenticate users in your application and grant them access to PubNub resources (other users' metadata and channels).

  3. Secure backend initialization.

    On the backend, initialize the Unity SDK with the secret key (SecretKey) on your servers to secure your PubNub instance.

    Secret key

    SecretKey is a secret shared between your application's server and PubNub and it's used to administer Access Manager permissions for your client applications by signing and verifying the authenticity of messages and requests. Remember to never expose the SecretKey to client devices.

  4. Get user permissions.

    Retrieve detailed user restrictions and convert these details into a simplified permission format where each channel is marked with whether the user can read, write, or access it, based on such restrictions as bans or mutes using the Get user details and Check restrictions methods.

  5. Generate authorization token.

    Using the Unity SDK, generate and assign an access token reflecting the user's permissions.

    The token contains information about which channels the user can access and how (read/write), and it's configured with a specific validity period. This token serves as a key for users to interact with the application according to their permissions.

    Operation-to-permission mapping

    Read the Permissions document for a complete list of available operations that users can do with PubNub resources in apps created with the Unity Chat SDK.

    Set short TTLs

    You can mute or ban a user for an indefinite amount of time, but you can unmute/unban them at any time. To make sure that the permissions set with Access Manager reflect the most up-to-date muting/banning restrictions on the client-side, it's recommended to set short-lived tokens (TTLs) for grant calls (valid for seconds and minutes rather than hours or days). Alternatively, if new muting/banning restrictions are set on the frontend side of your app, you can revoke Access Manager permissions on the backend using the chat.sdk.RevokeToken() method.

  6. Listen for moderation events.

    Set up a listener to listen for the moderation event type ("banned," "muted," or "lifted") generated when UI restrictions are added or removed.

  7. Act on moderation events.

    Update permissions in response to moderation events and generate new tokens if necessary.

Client-side restrictions

With server-side permissions enforced, add optional client-side UI feedback.

note

See Client-side restrictions for regular user moderation.

Listen to moderation events

StreamModerationEvents() sends notifications when users are muted/banned or removes memberships on ban. Use OnModerationEvent to handle updates.

Events documentation

To read more about the events of type Moderation, refer to the Chat events documentation.

Method naming

Earlier versions used SetListeningForModerationEvents() to enable streaming. This method has been superseded by StreamModerationEvents(), though it remains available for backward compatibility.

Method signature

These methods take the following parameters:

  • StreamModerationEvents()

    1user.StreamModerationEvents(bool stream)
  • OnModerationEvent - Event signature

    1// event on the User entity
    2public event Action<ChatEvent> OnModerationEvent;
    3// needs a corresponding event handler
    4void EventHandler(ChatEvent moderationEvent)

Input

ParameterRequired in StreamModerationEvents()Required in OnModerationEventDescription
stream
Type: bool
Default:
n/a
Yes
n/a
Whether to start (true) or stop (false) listening to moderation events for the user. Events are received from the user's channel (automatically prefixed with PUBNUB_INTERNAL_MODERATION.).
moderationEvent
Type: ChatEvent
Default:
n/a
No
Yes
The moderation event containing information about the moderation action.

Output

These methods don't return a value. Moderation event updates are delivered through the OnModerationEvent event handler.

Sample code

Send a Moderation event to the muted user.

1

Last updated on