Comments
Here's an overview of how you can start integrating comments into your applications
Comments are a way to create comments for a particular piece of content without needing to join a channel or manage channel memberships.
Amity Social SDK provides support for common text in comments.
All comment methods are contained in a CommentRepository class. Before calling any comment methods, you must ensure to import it.
import { CommentRepository } from '@amityco/js-sdk';

Comment Description

Name
Data Type
Description
Attributes
commentId
String
ID of the comment
userId
String
ID of the user who posted the comment
parentID
String
ID of a parent comment
rootId
String
ID of a root comment
refrenceId
String
ID of a reference
referenceType
String
Type of a reference
Enum: [post, content]
dataType
String
Type of the comment
Example: text
data
Object
Body of the comment
metadata
Object
Additional properties to support custom fields
childrenNumber
Number
Number of children comments
flagCount
Integer
Number of users who has read the comment
hashFlag
Object
Flag for checking internally if this comment is reported or not
reactions
Object
Mapping of reaction with reactionCounter
Example: OrderedMap { "like": 1 }
reactionsCount
Integer
Number of all reactions for the comment
myReactions
Array.<String>
List of my reactions to the comment
Example: "like"
isDeleted
Boolean
Flag that determines if comment is deleted
Default: false
editedAt
String($date-time)
Date/time when comment was edited
createdAt
String($date-time)
Date/time when comment was created
updatedAt
String($date-time)
Date/time when comment was updated or deleted
children
String
ID of the children comment
segmentNumber
Integer
required
Object
mentionees
Array.<String>
User IDs of the mentioned users
Example:
[
{ type: ‘user’, userIds: [‘user-id-1’, ‘user-id-2’]
}
]

Comment reference type

A comment's referenceType can be:
  • post
  • content
A post type comment is a comment that is intended for a regular post. A content type comment on the other hand is intended for a content type post. A content type comment will automatically create a content post if the referenceId does not exist yet.

Sending Comments

All comment sending methods are designed to be robust enough to work under any network conditions. When you send any comment, that comment will be automatically placed in a queue in case of unstable network conditions. Once the SDK reconnects to the server, it will automatically resend all queued comments.
Additionally, sent comments are always returned in comment queries, even before they have been delivered to the server. It is done to provide the user with a fluid commenting behavior: when a user sends a comment, that comment would appear in the comment list right away, instead of waiting until it has been confirmed by the server.

Content ID

Unlike messages, comments are directly tied to a specific referenceId that is managed by the client. The referenceId has to be unique but can be any string.

Create Comments

In creating a standard text comment, you need to specify the following parameters:
  • referenceType - values can be post or content
  • referenceId - id of the post/content that you want to add the comment
  • text - comment body
Refer to Comment reference type for a more detailed explanation of the referenceType parameter.
A comment should not exceed 20,000 characters in length.
For a successful creation of the comment, the reply will be the comment model. Otherwise, it will return an error.

Get Comment

You can use the commentForId() method in order to get a single comment. You need to pass the commentId of the requested comment as the parameter.
The method returns a liveobject instance of a comment model. It will throw an error if the passed commentId is not valid.

Comment Query

To query for a list of comments in a post:
This method will return a LiveCollection of comments in the specified referenceId.
Refer to Comment reference type for a more detailed explanation of the referenceType parameter.

Threaded Comments

A comment can be a root for a thread. To query the children of a comment thread, you can add the parentId parameter in a comment query, along with the filterByParentId flag.
When creating a comment, we can also pass the parentId to make it appear under the parent.
Refer to Comment reference type for a more detailed explanation of the referenceType parameter.

Flag & Unflag Comments

Before you can flag or unflag a comment, the related comment must be fetched from the server first using either of these methods:

Flag

You can report or flag a comment as inappropriate using the flag method. The required parameter is the comment ID, which is a string.
To flag a comment, call the following method:

Unflag

You can unflag or revoke the report for a comment using the unflag method. The required parameter is the comment ID, which is a string.
To unflag a comment, call the following method:

Check for isFlaggedByMe

The isFlaggedByMe method checks if a comment has been flagged by the current user. The required parameter is the comment ID, which is a string.

Edit Comments

You can only perform edit and delete operations on your own comments. Once the operation is complete, the comment's editedAtDate will be set to the current time. This allows you to provide UI to the user to inform the user of specific messages that have been edited, if needed. An optional completion block can be provided to notify you of operation success.

Add Comment to a Post

You can add a comment to a post with the following:
To know which post a comment belongs to, you can use the referenceType parameter.
comment.referenceType === postId
If a comment is a reply to another comment, the parentId is undefined.
Refer to Comment reference type for a more detailed explanation of the referenceType parameter.

Delete Comments

There are two kinds of comments deletion:
  1. 1.
    Soft delete
    The comment is only marked as deleted. This means that the comment still exists in the database but the isDeleted flag is set to true.
  2. 2.
    Hard delete
    The comment data, along with its reactions and replies, are removed from the database. Thus, all data related to the comment will be lost and irretrievable. Hard deleting children comments will not delete the parent comment.
    Hard delete is applicable for posts and comments only. You cannot hard delete Users and Communities.
    Hard deleting posts, as well as comments, is only supported via SDK. UIKit and Console support may be considered in the future.
Only the owner of the comment or an admin can update and/or delete a comment.
CommentRepository provides a convenient method to delete comments. In deleting a comment, you need to pass the commentId and a boolean parameter. Set the boolean parameter to true for hard delete and false for soft delete.
const comment = await CommentRepository.deleteComment('commentId', true);
If you will not specify the boolean parameter, it will be set to false by default which is just equivalent to a soft delete.
The deleteComment method returns a promise acknowledging the server's successful response. Use await to receive the result. It will return true if the deletion is successful. Otherwise, it will return false.
{
"success": true
}
Export as PDF
Copy link
Outline
Comment Description
Comment reference type
Sending Comments
Content ID
Create Comments
Get Comment
Comment Query
Threaded Comments
Flag & Unflag Comments
Flag
Unflag
Check for isFlaggedByMe
Edit Comments
Add Comment to a Post
Delete Comments