Report offensive messages
Chat SDK provides a mechanism for users to report offensive content in messages directly from their applications.
In each case, a user must provide a reason for flagging a given message. As a result of flagging, a reported message gets published on the dedicated administrative channel (with the ID of PUBNUB_INTERNAL_MODERATION_{channel_id}) and an event of the report type gets created.
As a developer, you can add custom logic that uses the emitted events and defines what an admin can later do with such reported messages. For example, an admin can delete an inappropriate message.
Message Persistence
To work with stored message data, make sure you have Message Persistence enabled for your app's keyset in the Admin Portal.
Flag/Report messages
report() lets you flag and report an inappropriate message to the admin.
All messages (events of type report) reported on a given channel are sent to a PUBNUB_INTERNAL_MODERATION_{channel_id} channel which is a child channel for the main one where a reported message was published. For example, an event on a message reported on the support channel will be sent to the PUBNUB_INTERNAL_MODERATION_support channel.
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()
Get historical reported messages
getMessageReportsHistory() fetches a list of reported message events for a specified channel within optional time and count constraints, returning the events and a boolean indicating if there are more events to fetch.
This method prefixes the internal channel ID with a predefined string (PUBNUB_INTERNAL_MODERATION_) and calls the getEventsHistory method of the chat instance, passing along the constructed channel ID and any additional parameters.
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: eventsandisMore. | 
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.")
Listen to report events
As an admin of your chat app, you can monitor all events emitted when someone reports an offensive message using the streamMessageReports() method.
You can use this method to create moderation dashboard alerts.
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 ( reportevents) by invoking theclose()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