On this page

Report offensive messages

Users can report offensive messages directly from your app. Reported messages publish to PUBNUB_INTERNAL_MODERATION_{channel_id} and emit report events.

Add custom logic using emitted events to handle reported messages (e.g., delete them).

Message Persistence

Enable Message Persistence in the Admin Portal.

icon

Under the hood

Flag/Report messages

report() flags a message for admin review. Reports publish to PUBNUB_INTERNAL_MODERATION_{channel_id} (e.g., reporting on support sends to PUBNUB_INTERNAL_MODERATION_support).

Method signature

This method takes the following parameters:

1message.report(reason: string): Promise<any>

Input

* required
ParameterDescription
reason *
Type: string
Default:
n/a
Reason for reporting/flagging a given message.

Output

TypeDescription
Promise<any>
Returned object with a value of any type.

Sample code

Report the last message on the support channel as offensive.

1// reference the "incident-management" channel
2const channel = await chat.getChannel("support")
3// get the last message on the channel
4const lastMessage = (await channel.getHistory({count: 1})).messages[0]
5// report the message to the admin and provide the reason
6await lastMessage.report("This message contains profane words. Can you remove it?")

Get historical reported messages

getMessageReportsHistory() fetches reported message events for a channel with optional time and count filters.

Method signature

This method takes the following parameters:

1channel.getMessageReportsHistory(params?: {
2 startTimetoken?: string;
3 endTimetoken?: string;
4 count?: number;
5}): Promise<{
6 events: Event<"report">[];
7 isMore: boolean;
8}>

Input

* required
ParameterDescription
startTimetoken
Type: string
Default:
n/a
The start timetoken for fetching the history of reported messages, which allows specifying the point in time where the history retrieval should begin.
endTimetoken
Type: string
Default:
n/a
The end time token for fetching the history of reported messages, which allows specifying the point in time where the history retrieval should end.
count
Type: number
Default:
undefined
The number of reported message events to fetch from the history.

Output

ParameterDescription
Promise<>
Type: object
Returned object containing these fields: events and isMore.
 → events
Type: Event<"report">[]
An array of events where each event is of the type "report". These events represent the reported messages in the specified channel.
 → isMore
Type: boolean
A boolean flag indicating whether there are more reported message events available apart from the ones fetched in the current operation.

Sample code

Fetch historical messages reported on the support channel between the 1725100800000 (July 1, 2024, 00:00:00 UTC) and 1726780799000 (July 21, 2024, 23:59:59 UTC) timetokens.

1// Example start and end timetokens
2const startTimetoken = "1725100800000"; // July 1, 2024, 00:00:00 UTC in milliseconds
3const endTimetoken = "1726780799000"; // July 21, 2024, 23:59:59 UTC in milliseconds
4
5// asynchronously fetch the channel object and then get the reported messages history
6async function fetchReportedMessagesHistory() {
7 try {
8 // reference the "chat" object and invoke the "getChannel()" method
9 const channel = await chat.getChannel("support");
10
11 // fetch the reported message history
12 const { events, isMore } = await channel.getMessageReportsHistory({
13 startTimetoken,
14 endTimetoken
15 });
show all 25 lines

Listen to report events

channel.onMessageReported() monitors report events for moderation dashboards.

Method signature

1channel.onMessageReported(callback: (report: MessageReport) => void): () => void

The MessageReport object contains:

PropertyDescription
reason
Type: string
Reason the message was reported.
text
Type: string?
Text content of the reported message, if available.
messageTimetoken
Type: string?
Timetoken of the reported message.
reportedMessageChannelId
Type: string?
ID of the channel where the reported message was posted.
reportedUserId
Type: string?
ID of the user who sent the reported message.
autoModerationId
Type: string?
ID assigned by auto-moderation, if applicable.

Sample code

1const channel = await chat.getChannel("support")
2const stopListening = channel.onMessageReported((report) => {
3 console.log(`Message reported: reason=${report.reason}, by user=${report.reportedUserId}`)
4})

Flag/Report messages (deprecated)

DEPRECATED_report() lets you flag and report an inappropriate message to the admin. All reported messages (events of type report) are sent to one dedicated INTERNAL_ADMIN_CHANNEL channel (one "bucket" for all reported messages).

Method signature

This method takes the following parameters:

1message.DEPRECATED_report(reason: string): Promise<any>

Input

* required
ParameterDescription
reason *
Type: string
Default:
n/a
Reason for reporting/flagging a given message.

Output

TypeDescription
Promise<any>
Returned object with a value of any type.

Sample code

Report the last message on the support channel as offensive.

1// reference the "incident-management" channel
2const channel = await chat.getChannel("support")
3// get the last message on the channel
4const lastMessage = (await channel.getHistory({count: 1})).messages[0]
5// report the message to the admin and provide the reason
6await lastMessage.DEPRECATED_report("This message contains profane words. Can you remove it?")
Last updated on