Quick Reference for AI Agents & DevelopersRequired: Moderation enabled in Dashboard → Moderation → Advanced Settings
Overview
Flagging messages allows users to report inappropriate content to moderators or administrators. When a message is flagged, it appears in the CometChat Dashboard under Moderation > Flagged Messages for review.For a complete understanding of how flagged messages are reviewed and managed, see the Flagged Messages documentation.
Prerequisites
Before using the flag message feature:- Moderation must be enabled for your app in the CometChat Dashboard
- Flag reasons should be configured under Moderation > Advanced Settings
How It Works
Get Flag Reasons
Before flagging a message, retrieve the list of available flag reasons configured in your Dashboard:- JavaScript
- TypeScript
Response
Response
On Success —
getFlagReasons() returns an array of flag reason objects:Flag Reason Array (per item):| Parameter | Type | Description | Sample Value |
|---|---|---|---|
id | string | Unique identifier for the flag reason | "spam" |
name | string | Display name of the flag reason | "Spam" |
description | string | Detailed description of the reason | "Repeated, promotional, or irrelevant content" |
createdAt | number | Unix timestamp when reason was created | 1761627675 |
updatedAt | number | Unix timestamp when reason was last updated | 1761627675 |
default | boolean | Whether this is a default reason | false |
Flag a Message
To flag a message, use theflagMessage() method with the message ID and a payload containing the reason:
- JavaScript
- TypeScript
Response
Response
On Success —
flagMessage() returns a success response:Response Object:| Parameter | Type | Description | Sample Value |
|---|---|---|---|
success | boolean | Indicates if the flag operation succeeded | true |
message | string | Confirmation message with flagged message ID | "Message 25300 has been flagged successfully." |
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| messageId | string | Yes | The ID of the message to flag |
| payload.reasonId | string | Yes | ID of the flag reason (from getFlagReasons()) |
| payload.remark | string | No | Additional context or explanation from the user |
Complete Example
Here’s a complete implementation showing how to build a report message flow:Best Practices
Best Practices
- Cache flag reasons: Call
getFlagReasons()once at app initialization or when the report dialog is first opened, then reuse the cached list. Avoid fetching reasons on every report action. - Require a reason: Always require users to select a reason before submitting a flag. This improves the quality of moderation data and helps moderators prioritize reviews.
- Provide a remark field: Allow users to add optional context via the
remarkparameter. Additional details help moderators make faster, more informed decisions. - Confirm before submitting: Show a confirmation dialog before calling
flagMessage()to prevent accidental reports. - Show feedback after flagging: Display a success message or toast after a message is flagged so the user knows their report was submitted.
Troubleshooting
Troubleshooting
getFlagReasons()returns an empty array: Ensure that flag reasons are configured in the CometChat Dashboard under Moderation > Advanced Settings. Moderation must also be enabled for your app.flagMessage()fails with an error: Verify that thereasonIdmatches one of the IDs returned bygetFlagReasons(). Also confirm that themessageIdis valid and belongs to a conversation the logged-in user has access to.- Flagged messages not appearing in Dashboard: Check that moderation is enabled in your Dashboard settings. Flagged messages appear under Moderation > Flagged Messages.
- Permission errors: The logged-in user must be a participant in the conversation containing the message they want to flag. Users cannot flag messages from conversations they are not part of.