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): PNFuture<PNPublishResult>
Input
| Parameter | Description |
|---|---|
reason *Type: StringDefault: n/a | Reason for reporting/flagging a given message. |
Output
| Type | Description |
|---|---|
PNFuture<PNPublishResult> | Returned object with a value of any type. |
Sample code
Report the last message on the support channel as offensive.
1// retrieve the "support" channel
2pubnub.getChannel("support").async { channelResult ->
3 channelResult.onSuccess { channel ->
4 // get the last message from the channel’s history
5 channel.getHistory(count = 1).async { historyResult ->
6 historyResult.onSuccess { historyResponse ->
7 val message = historyResponse.messages.firstOrNull()
8 if (message != null) {
9 // report the last message as offensive
10 message.report("Offensive Content").async { reportResult ->
11 reportResult.onSuccess { pnPublishResult: PNPublishResult ->
12 println("Reported message successfully: ${pnPublishResult.timetoken}")
13 }.onFailure { throwable ->
14 println("Failed to report the message.")
15 throwable.printStackTrace()
show all 30 linesGet 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(
2 startTimetoken: Long?,
3 endTimetoken: Long?,
4 count: Int = 100,
5): PNFuture<GetEventsHistoryResult>
Input
| Parameter | Description |
|---|---|
startTimetokenType: LongDefault: 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: LongDefault: 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: IntDefault: 100 | The number of reported message events to fetch from the history. |
Output
| Type | Description |
|---|---|
PNFuture<GetEventsHistoryResult> | Returned object containing these fields: events and isMore. |
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// fefine timetokens for the message history period
2val startTimetoken: Long = 1725100800000L // July 1, 2024, 00:00:00 UTC
3val endTimetoken: Long = 1726780799000L // July 21, 2024, 23:59:59 UTC
4
5// fetch historical messages reported on the `support` channel
6pubnub.getChannel("support").async { channelResult ->
7 channelResult.onSuccess { channel ->
8 // fetch historical messages reported within the specified time frame
9 channel.getMessageReportsHistory(
10 startTimetoken = startTimetoken,
11 endTimetoken = endTimetoken,
12 count = 25
13 ).async { historyResult ->
14 historyResult.onSuccess { getEventsHistoryResult ->
15 println("Fetched reported messages successfully.")
show all 28 linesListen to report events
streamMessageReports() monitors report events for moderation dashboards.
Events documentation
To read more about the events of type report, refer to the Chat events documentation.
Method signature
This method has the following parameters:
1channel.streamMessageReports(callback: (event: Event<EventContent.Report>) -> Unit): AutoCloseable
Input
| Parameter | Description |
|---|---|
callback *Type: n/a Default: n/a | Callback function passed as a parameter. It defines the custom behavior to be executed when detecting new message report events. |
→ event *Type: Event<EventContent.Report>Default: n/a | An instance of an event where the content is specifically related to a report. |
Output
| Type | Description |
|---|---|
AutoCloseable | Interface that lets you stop receiving report-related updates (report events) by invoking the close() method. |
Sample code
Print a notification for an offensive message reported on the support channel.
1val autoCloseable: AutoCloseable = channel.streamMessageReports { event: Event<EventContent.Report> ->
2 // check if the event is for the 'support' channel
3 if (event.channelId == "support") {
4 // access the report details from the event's payload
5 val reportPayload = event.payload
6 val reportReason = reportPayload.reason
7
8 // print the notification
9 if (reportReason.equals("offensive", ignoreCase = true)) {
10 println("Notification: An offensive message was reported on the 'support' channel by user ${event.userId}. Reason: $reportReason")
11 }
12 }
13}
14
15// later, you may want to stop listening to the events
show all 16 lines