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, you must enable Message Persistence 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:

message.report(reason: String): PNFuture<PNPublishResult>

Input

ParameterTypeRequiredDefaultDescription
reasonStringYesn/aReason for reporting/flagging a given message.

Output

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

Basic usage

Report the last message on the support channel as offensive.

// retrieve the "support" channel
pubnub.getChannel("support").async { channelResult ->
channelResult.onSuccess { channel ->
// get the last message from the channel’s history
channel.getHistory(count = 1).async { historyResult ->
historyResult.onSuccess { historyResponse ->
val message = historyResponse.messages.firstOrNull()
if (message != null) {
// report the last message as offensive
message.report("Offensive Content").async { reportResult ->
reportResult.onSuccess { pnPublishResult: PNPublishResult ->
println("Reported message successfully: ${pnPublishResult.timetoken}")
}.onFailure { throwable ->
println("Failed to report the message.")
throwable.printStackTrace()
show all 30 lines

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 (INTERNAL_MODERATION_PREFIX) 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:

channel.getMessageReportsHistory(
startTimetoken: Long?,
endTimetoken: Long?,
count: Int = 100,
): PNFuture<GetEventsHistoryResult>

Input

ParameterTypeRequiredDefaultDescription
startTimetokenLongNon/aThe start timetoken for fetching the history of reported messages, which allows specifying the point in time where the history retrieval should begin.
endTimetokenLongNon/aThe end time token for fetching the history of reported messages, which allows specifying the point in time where the history retrieval should end.
countIntNo100The number of reported message events to fetch from the history.

Output

TypeDescription
PNFuture<GetEventsHistoryResult>Returned object containing these fields: events and isMore.

Basic usage

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.

// fefine timetokens for the message history period
val startTimetoken: Long = 1725100800000L // July 1, 2024, 00:00:00 UTC
val endTimetoken: Long = 1726780799000L // July 21, 2024, 23:59:59 UTC

// fetch historical messages reported on the `support` channel
pubnub.getChannel("support").async { channelResult ->
channelResult.onSuccess { channel ->
// fetch historical messages reported within the specified time frame
channel.getMessageReportsHistory(
startTimetoken = startTimetoken,
endTimetoken = endTimetoken,
count = 25
).async { historyResult ->
historyResult.onSuccess { getEventsHistoryResult ->
println("Fetched reported messages successfully.")
show all 28 lines

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:

channel.streamMessageReports(callback: (event: Event<EventContent.Report>) -> Unit): AutoCloseable
Input
ParameterTypeRequiredDefaultDescription
callbackn/aYesn/aCallback function passed as a parameter. It defines the custom behavior to be executed when detecting new message report events.
 → eventEvent<EventContent.Report>Yesn/aAn instance of an event where the content is specifically related to a report.
Output
TypeDescription
AutoCloseableInterface that lets you stop receiving report-related updates (report events) by invoking the close() method.

Basic usage

Print a notification for an offensive message reported on the support channel.

val autoCloseable: AutoCloseable = channel.streamMessageReports { event: Event<EventContent.Report> ->
// check if the event is for the 'support' channel
if (event.channelId == "support") {
// access the report details from the event's payload
val reportPayload = event.payload
val reportReason = reportPayload.reason

// print the notification
if (reportReason.equals("offensive", ignoreCase = true)) {
println("Notification: An offensive message was reported on the 'support' channel by user ${event.userId}. Reason: $reportReason")
}
}
}

// later, you may want to stop listening to the events
show all 16 lines
Last updated on