Moderate misbehaving users
Requires App Context
To work with stored user metadata, you must enable App Context for your app's keyset in the Admin Portal.
Unity Chat SDK provides moderation mechanisms for:
- Admins to mute misbehaving users on channels.
- Admins to ban misbehaving users from accessing channels.
For example, an admin can mute or ban the users from a given channel using Access Manager.
Mute or ban users
As an admin, you can mute a specific user on a channel or ban them from accessing that channel using SetRestriction()
and SetRestrictions()
methods.
All of them give the same output. The only difference is that you call a given method on the Chat
, User
, or the Channel
object. Depending on the object, these methods take a different set of input parameters.
When an admin mutes or bans a user on a channel, a moderation
event is created (of type muted
or banned
). You can listen to these events and, for example, remove user's membership on that channel.
Also, when an admin mutes or bans a user, an additional moderation membership is created for that user. This membership copies the ID of the channel and adds the PUBNUB_INTERNAL_MODERATION_
prefix to it, even though no new channel gets created for that purpose.
The additional membership is created only for the moderation purposes - when fetching all channel memberships for a given user with the GetMemberships()
method, you won't see the moderation membership as Unity Chat SDK filters it out automatically with App Context Filtering Language.
When you lift restrictions on the user (unmute or unban them), the moderation membership is removed and a moderation
event of type lifted
is created.
To learn if a user is muted on a given channel or banned, use the Unity Chat SDK methods to check moderation restrictions.
Requires Secret Key authentication
Mute and ban restrictions for the client devices should be set by servers initializing Unity Chat SDK with a Secret Key (available on the Admin Portal on your app's keyset).
The SecretKey
should only be used within a secure server and never exposed to client devices. If the SecretKey
is ever compromised, it can be an extreme security risk to your application. If you suspect your SecretKey
has been compromised, you can generate a new SecretKey
for the existing PubNub keyset on the Admin Portal.
Method signature
These methods take the following parameters:
-
SetRestriction()
(on theChat
object)chat.SetRestriction(
string userId,
string channelId,
Restriction restriction
) -
SetRestriction()
(on theUser
object)user.SetRestriction(
string channelId,
Restriction restriction
) -
SetRestrictions()
(on theChannel
object)channel.SetRestrictions(
string userId,
Restriction restriction
)
Input
Parameter | Type | Required for Chat | Required for User | Required for Channel | Default | Description |
---|---|---|---|---|---|---|
userId | string | Yes | No | No | n/a | 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 | string | Yes | No | No | n/a | ID of the channel on/from which the user should be muted or banned. |
restriction | Restriction | No | No | No | n/a | Moderation restrictions and reasoning behind them. |
→ ban | boolean | No | No | No | n/a | 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 | boolean | No | No | No | n/a | 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 | string | No | No | No | n/a | Reason why you want to ban or mute the user. |
Output
An awaitable Task
.
Basic usage
Mute
Mute support_agent_15
on the support
channel.
-
SetRestriction()
(on theChat
object)await chat.SetRestriction(
userId: "support_agent_15",
channelId: "support",
restriction: new Restriction()
{
Ban = false,
Mute = true,
Reason = string.Empty
}
); -
SetRestriction()
(on theUser
object)if (chat.TryGetUser("support_agent_15", out var user))
{
await user.SetRestriction(
channelId: "support",
restriction: new Restriction()
{
Ban = false,
Mute = true,
Reason = string.Empty
}
)
} -
SetRestrictions()
(on theChannel
object)if (chat.TryGetChannel("support", out var channel))
{
await channel.SetRestrictions(
userId: "support_agent_15",
restriction: new Restriction()
{
Ban = false,
Mute = true,
Reason = string.Empty
}
)
}
Ban
Ban support_agent_15
from the support
channel.
-
SetRestriction()
(on theChat
object)if (chat.TryGetUser("support_agent_15", out var user))
{
await user.SetRestriction(
channelId: "support",
restriction: new Restriction()
{
Ban = false,
Mute = true,
Reason = string.Empty
}
)
} -
SetRestriction()
(on theUser
object)if (chat.TryGetUser("support_agent_15", out var user))
{
await user.SetRestriction(
channelId: support,
banUser: true,
muteUser: false,
reason: null
);
} -
SetRestrictions()
(on theChannel
object)if (chat.TryGetChannel("support", out var channel))
{
await channel.SetRestrictions(
userId: "support_agent_15",
restriction: new Restriction()
{
Ban = false,
Mute = true,
Reason = string.Empty
}
)
}
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 GetChannelRestriction()
and GetUserRestriction()
methods.
Method signature
These methods take the following parameters:
-
GetChannelRestrictions()
user.GetChannelRestrictions(Channel channel)
-
GetUserRestrictions()
channel.GetUserRestrictions(User user)
Input
Parameter | Type | Required in GetChannelRestrictions() | Required in GetUserRestrictions() | Default | Description |
---|---|---|---|---|---|
channel | Channel | Yes | No | n/a | Channel on/from which the user can be muted or banned. |
user | User | No | Yes | n/a | User that can be muted or banned. |
Output
These methods return an awaitable Task<Restriction>
object.
public struct Restriction
{
public bool Ban;
public bool Mute;
public string Reason;
}
Basic usage
Check if the user support_agent_15
has any restrictions set on the support
channel.
-
GetChannelRestrictions()
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
if (!chat.TryGetUser("support_agent_15", out var user))
{
Console.WriteLine("Couldn't find user!");
return;
};
// check user restrictions
var restriction = await user.GetChannelRestrictions(channel) -
GetUserRestrictions()
if (!chat.TryGetChannel("support", out var channel))
{
Console.WriteLine("Couldn't find channel!");
return;
};
if (!chat.TryGetUser("support_agent_15", out var restrictedUser))
{
Console.WriteLine("Couldn't find user!");
return;
};
await channel.GetUserRestrictions(restrictedUser)
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:
user.GetChannelsRestrictions(
string sort = "",
int limit = 0,
Page page = null
)