Comments

Here's an overview of how you can get started integrating comments into your applications

Each comment is identified by a unique commentId, which is an immutable string. When creating a new comment, you do not need to specify your own commentId; you can leave it to Amity to generate one. We also provide optimistic updates on each comment. You will need to send a referenceId otherwise known as a postId , in the latest version. You need to use AmityCommentRepository before starting any activity with comments, such as editing an operation and/or flagging a comment.

There are 2 methods in AmityCommentRepository to note, one makes a comment as well as getting a collection of comments. They all return a LiveObject with the complete comment model.

Comment management methods are contained in the AmityCommentRepository class. Before being able to call any comment method, you must initialize a repository instance using the AmityClient instance you created earlier during setup:

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

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

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

childrenComments

String

Children comments

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.

Create Comment

AmityCommentRepository provides one convenient method to create comment. You can provide referenceId, parentId and text while creating a comment. parentId is an optional parameter and useful for replying to a comment. The concept is similar to the AmityMessage you can reply to a message object with a parentId.

For now, only text data will be supported in the creation method.

Refer to Comment reference type for a more detailed explanation of the referenceType parameter.

The code above creates a comment and prints out the commentId once it has been successfully created. It first instantiates the AmityCommentRepository, a class that contains all comment-related methods. Then it calls createComment: to obtain the LiveObject and observe it in order to obtain the final comment model.

A comment should not exceed 20,000 characters in length.

The AmityNotificationToken returned by the observeOnceWithBlock: is saved in self.token, a strongly referenced property. This is needed in order to prevent the observe block to be released.

The parentId parameter in createComment: is optional.

The referenceId parameter in createComment: is mandatory and will only support AmityPost identifier.

Comments Query

The AmityCommentRepository provides method to get the query. The channelsForFilter(_:) and getCommentsWithReferenceId(_:) are the methods to return the LiveCollection of all the matching comments available. Like other LiveObjects, the collection return will help automatically update and notify you on any comment modifications (e.g. new comment, deleted comment, modified comment, flagged comment).

You can order the comments in descending order (i.e latest comment first). In this case, you will have to call previousPage() method to fetch more comments. Please look into the method documentation for more details.

Pagination

When querying comments, you cannot set limit, skip, after, first, before, and last parameters. By default, the number of items in each page is 20. To handle pagination to load more comments, refer to Pagination in Live Objects.

Get latest comment

You can also fetch the latest single comment using getLatestComment method. This method returns the Live Object which you can observe.

  1. Refer to Comment reference type for a more detailed explanation of the referenceType parameter.

  2. If a comment is a reply to another comment, the parentId parameter is undefined.

Edit Comment

For any editing operation - either update or delete a comment, you can use AmityCommentEditor. AmityCommentEditor instantiates by using the selected commentId and the client.

Delete Comment

There are two kinds of comments deletion:

  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. 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.

To delete a comment, use AmityCommentRepository. It provides a deleteComment: method. You need to pass the commentId and a boolean parameter. Set the boolean parameter to true for hard delete and false for soft delete.

Only the owner of the comment or an admin can update and/or delete a comment.

Flag Comment

For flagging operation either update or delete a comment you can use AmityCommentFlagger. AmityCommentFlagger instantiates by using the selected commentId and the client. They have three methods which are flag:, unflag: and isFlaggedByMe:

Reactions

AmityComment object includes information about the reactions for that post. Use myReactions property to get a list of your reactions to the post. Usereactions property to get a list of all reactions to the post and reactionsCount provides the total count of reactions on that post.

For more details about Reactions, refer to our Reactions documentation.

Last updated