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.
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
| Parameter | Description |
|---|---|
reason *Type: stringDefault: n/a | Reason for reporting/flagging a given message. |
Output
| Type | Description |
|---|---|
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 message.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
| Parameter | Description |
|---|---|
startTimetokenType: stringDefault: 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. |
endTimetokenType: stringDefault: 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. |
countType: numberDefault: undefined | The number of reported message events to fetch from the history. |
Output
| Parameter | Description |
|---|---|
Promise<>Type: object | Returned object containing these fields: events and isMore. |
→ eventsType: Event<"report">[] | An array of events where each event is of the type "report". These events represent the reported messages in the specified channel. |
→ isMoreType: 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 linesListen to report events
streamMessageReports() monitors report events for moderation dashboards.
Events documentation
See Chat events for report event details.
Method signature
This method has the following parameters:
1channel.streamMessageReports(
2 callback: (event: Event<"report">) => void
3): () => void
Input
| Parameter | Description |
|---|---|
channelType: stringDefault: n/a | Channel to listen for new report events. |
typeType: stringDefault: n/a | Type of events. report is the type defined for all events emitted when an offensive message is flagged/reported. |
callback *Type: n/a Default: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed whenever a report event type is detected on the specified channel. |
Output
| Type | Description |
|---|---|
() => void | Function you can call to disconnect (unsubscribe) from the channel and stop receiving report events. |
Sample code
Print a notification for an offensive message reported on the support channel.
1const channel = await chat.getChannel("support")
2channel.streamMessageReports((event) => {
3 console.log("Report event: ", event)
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
| Parameter | Description |
|---|---|
reason *Type: stringDefault: n/a | Reason for reporting/flagging a given message. |
Output
| Type | Description |
|---|---|
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 message.DEPRECATED_report("This message contains profane words. Can you remove it?")