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,
completion: ((Swift.Result<Timetoken, Error>) -> Void)? = nil
)

Input

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

Output

TypeDescription
((Swift.Result<Timetoken, Error>) -> Void)Returned object with a value of any type.

Basic usage

Report the last message on the support channel as offensive.

// Reference the "support" channel

chat?.getChannel(channelId: "support") { channelResult in
switch channelResult {
case let .success(channel):
if let channel = channel {
// Get the last message from the channel’s history
channel.getHistory(count: 1) { historyResult in
switch historyResult {
case let .success(historyResponse):
if let message = historyResponse.messages.first {
// Report the last message as offensive
message.report(reason: "Offensive Content") { reportResult in
switch reportResult {
case .success(let timetoken):
show all 34 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: Timetoken? = nil,
endTimetoken: Timetoken? = nil,
count: Int = 25,
completion: ((Swift.Result<(events: [EventWrapper], isMore: Bool), Error>) -> Void)?
)

Input

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

Output

TypeDescription
((Swift.Result<(events: [EventWrapper], isMore: Bool), Error>) -> Void)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.

// Define timetokens for the message history period
let startTimetoken: Timetoken = 1725100800000 // July 1, 2024, 00:00:00 UTC
let endTimetoken: Timetoken = 1726780799000 // July 21, 2024, 23:59:59 UTC

chat?.getChannel(channelId: "support") { channelResult in
switch channelResult {
case let .success(channel):
if let channel = channel {
// Fetch historical messages reported within the specified time frame
channel.getMessageReportsHistory(
startTimetoken: startTimetoken,
endTimetoken: endTimetoken,
count: 25
) { historyResult in
switch historyResult {
show all 31 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: @escaping (any Event<EventContent.Report>) -> Void) -> 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.
 → event(any Event<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.

// Reference the "support" channel
chat?.getChannel(channelId: "support") { result in
switch result {
case let .success(channel):
if let channel = channel {
// Listen for report events on the 'support' channel
let autoCloseable = channel.streamMessageReports { event in
// Check if the event is for the 'support' channel
if event.channelId == "support" {
// Access the report details from the event's payload
let reportPayload = event.payload
let reportReason = reportPayload.reason
// Print the notification
if reportReason.lowercased() == "offensive" {
print("Notification: An offensive message was reported on the 'support' channel by user \(event.userId). Reason: \(reportReason)")
show all 26 lines
Last updated on