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 EkoCommentRepository before starting any activity with comments, such as editing an operation and/or flagging a comment.

There are 2 methods in EkoCommentRepository 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 a EkoCommentRepository class. Before being able to call any comment method, you must initialise a repository instance using the EkoClient instance you created earlier during setup:

let repository: EkoCommentRepository = EkoCommentRepository(client: client)

Create Comment

EkoCommentRepository provides one convenient method to create comment. You can provide referenceId, parentId and text while creating a comment. parentId is optional parameters and useful for replying a comment. The concept is similar to the EkoMessage you can reply a message object with a parentId. For now, only text data will be supported in the creation method.

var commentToken: EkoNotificationToken?  

...

let commentObject: EkoObject<EkoComment> = repository.createComment(withReferenceId: postId!, parentId: parentCommentId, text: text)

commentToken = commentObject.observe { commentObject, error in
  if let error = error {
    // Handle comment create error
    return
  }
  guard let comment = commentObject.object
  print("Comment created: \(comment.commentId)")
}

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

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

The parent parameter in createComment: is optional.

The referenceId parameter in createComment: is a mandatory and will be only support EkoPost identifier.

Comments Query

The EkoCommentRepository provides method to get the query. The channelsForFilter(_:) and commentsWithReferenceId(_:) 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).

var commentCollection: EkoCollection<EkoComment>?
var commentCollectionToken: EkoNotificationToken?

commentCollection = commentRepository.comments(withReferenceId: postId!, filterByParentId: true, parentId: parentCommentId, orderBy: .descending, includeDeleted: false)

commentCollectionToken = commentCollection?.observe { [weak self] _, _, _ in
    // Update datasource
}

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.

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

let latestComment = commentRepo.getLatestComment(withReferenceId: "abc", referenceType: .post, includeReplies: true)
latestComment.observe({ (liveObject, error) in 
    // Do something
})

Edit Comment

For any editing operation either update or delete a comment you can use EkoCommentEditor. EkoCommentEditor instantiate by using the selected commentId and the client. They have 2 methods which are editText:withCompletion: and deleteWithCompletion:

// The editor object
var editor: EkoCommentEditor? {
    return EkoCommentEditor(client: client, commentId: commentId)
}

// For editing the data
editor?.editText(text, completion: { (success, error) in
    // Do something with success
})

// For delete the comment
editor?.delete(completion: { (success, _) in
    // Do something with success
})

Flag Comment

For flagging operation either update or delete a comment you can use EkoCommentFlagger. EkoCommentFlagger instantiate by using the selected commentId and the client. They have 3 methods which are flagWithCompletion:, unflagWithCompletion: and isFlagByMeWithCompletion:

// The flagger object
var flagger: EkoCommentFlagger? {
    return EkoCommentFlagger(client: client, commentId: commentId)
}

// For flagging the comment
flagger?.flag(completion: { (success, _) in
    // Do something with success
})

// For unflag the comment
flagger?.unflag(completion: { (success, _) in
    // Do something with success
})

// For get the flag status
flagger?.isFlagByMe(completion: { (isFlagged) in
    // Do something with the flag status
})

Reactions

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

To add or remove reaction, please refer to documentation for EkoReactionRepository.

Last updated