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​

FieldTypeRequiredDescription
configIdStringYesUUID of the Auto Moderation configuration created in BizOps Workspace.
messageJSONYesFull publish body. Policies read the top-level text field by default.
channelStringYesChannel the message publishes to.
userIdStringYesUser ID (the uuid parameter) of the publisher.
metaJSONNoOptional metadata as custom key-value pairs. Functions receive this as a stringified JSON. Parse it before use.

ModerateMessageResponse​

FieldTypeRequiredDescription
moderationIdStringYesUnique ID for auditing and correlation.
flaggedBooleanYestrue if any moderation rule triggered.
actionsString[]YesConfiguration-defined actions, for example ["block", "wordMasked", "report"].
categoriesRecord<string, CategoryResult>YesPer-category results.
transformObjectNoSuggested changes to apply. May be empty.
transform.messageJSONNoMessage with masks or edits applied.
transform.metaJSONNoMeta augmented with moderation fields, for example a report identifier.

CategoryResult

FieldTypeRequiredDescription
flaggedBooleanYesWhether this category triggered.
detailsObjectNoCategory-specific details.
details.maskedWordsString[]NoPresent 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 lines

Validation errors​

FieldConditionError message
configIdMissingconfigId must be provided
messageUndefined or nullmessage must be provided
channelMissing or not a stringchannel must be provided and must be a string
userIdMissing or not a stringuserId must be provided and must be a string

Service and transport errors​

ScenarioBehaviorNotes
Moderation API returns a non-2xx responsePromise resolves with the parsed error body when availableBuilt-in retries, up to 3 total attempts
Network or transport failurePromise rejectsCounts 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.

  • Modules and libraries. Every built-in module available inside a Function.
  • API limits. Per-execution XHR request cap, which moderateMessage counts against.

Was this page useful?

Last updated on