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
2chat.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
6chat.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
onMessageReported() monitors report events on a channel. Use this for moderation dashboards to track reported messages in real time.
Deprecated method
streamMessageReports() is deprecated. Use onMessageReported() instead, which provides a typed Report object.
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.onMessageReported(callback: (report: Report) -> Unit): AutoCloseable
Input
| Parameter | Description |
|---|---|
callback *Type: (report: Report) -> UnitDefault: n/a | Function invoked with a Report event whenever a message is reported on this channel. |
The Report object contains:
| Property | Description |
|---|---|
reasonType: String | The reason for reporting the message. |
textType: String? | The text of the reported message, if available. |
messageTimetokenType: Long? | The timetoken of the reported message. |
reportedMessageChannelIdType: String? | The channel ID where the reported message was sent. |
reportedUserIdType: String? | The user ID of the user who sent the reported message. |
autoModerationIdType: String? | The auto-moderation ID associated with the report. |
Output
| Type | Description |
|---|---|
AutoCloseable | Interface that lets you stop receiving report events by invoking the close() method. |
Sample code
Print a notification for an offensive message reported on the support channel.
1val channel: Channel
2// ...
3
4val subscription = channel.onMessageReported { report ->
5 println("Message reported: reason=${report.reason}, by user=${report.reportedUserId}")
6}
7
8// stop listening:
9// subscription.close()