ugc
The ugc built-in module is available only inside a PubNub Function. It calls an Auto Moderation configuration, identified by configId, to moderate a message.
1const ugc = require('ugc');
moderateMessage
1function moderateMessage(req: ModerateMessageRequest): Promise<ModerateMessageResponse>
ModerateMessageRequest
| Field | Type | Required | Description |
|---|---|---|---|
configId | String | Yes | UUID of the Auto Moderation configuration created in BizOps Workspace. |
message | JSON | Yes | Full publish body. Policies read the top-level text field by default. |
channel | String | Yes | Channel the message publishes to. |
userId | String | Yes | User ID (the uuid parameter) of the publisher. |
meta | JSON | No | Optional metadata as custom key-value pairs. Functions receive this as a stringified JSON. Parse it before use. |
ModerateMessageResponse
| Field | Type | Required | Description |
|---|---|---|---|
moderationId | String | Yes | Unique ID for auditing and correlation. |
flagged | Boolean | Yes | true if any moderation rule triggered. |
actions | String[] | Yes | Configuration-defined actions, for example ["block", "wordMasked", "report"]. |
categories | Record<string, CategoryResult> | Yes | Per-category results. |
transform | Object | No | Suggested changes to apply. May be empty. |
transform.message | JSON | No | Message with masks or edits applied. |
transform.meta | JSON | No | Meta augmented with moderation fields, for example a report identifier. |
CategoryResult
| Field | Type | Required | Description |
|---|---|---|---|
flagged | Boolean | Yes | Whether this category triggered. |
details | Object | No | Category-specific details. |
details.maskedWords | String[] | No | Present for word-list violations. |
1export default (request) => {
2 const ugc = require('ugc');
3 const metaFromClient = typeof request.params.meta === 'string' ? JSON.parse(request.params.meta) : request.params.meta;
4
5 return ugc.moderateMessage({
6 configId: '<YOUR_MODERATION_CONFIG_ID>',
7 message: request.message,
8 userId: request.params.uuid,
9 channel: request.channels[0],
10 meta: metaFromClient,
11 }).then((res) => {
12 if (res.flagged && res.actions.includes('block')) {
13 request.status = 403;
14 return request.abort('Flagged');
15 }
show all 22 linesValidation errors
| Field | Condition | Error message |
|---|---|---|
configId | Missing | configId must be provided |
message | Undefined or null | message must be provided |
channel | Missing or not a string | channel must be provided and must be a string |
userId | Missing or not a string | userId must be provided and must be a string |
Service and transport errors
| Scenario | Behavior | Notes |
|---|---|---|
| Moderation API returns a non-2xx response | Promise resolves with the parsed error body when available | Built-in retries, up to 3 total attempts |
| Network or transport failure | Promise rejects | Counts toward the XHR-per-execution limit |
Each channel supports one Before Publish or Fire Function per keyset. Consolidate moderation and other pre-publish logic into that single Function. Calling ugc.moderateMessage from it is the supported pattern for combining custom logic with an Auto Moderation configuration.
Related reference
- Modules and libraries. Every built-in module available inside a Function.
- API limits. Per-execution XHR request cap, which
moderateMessagecounts against.