Link Search Menu Expand Document

Comment Add

/api/v2/feed/comment/add

Post

posts

profiles

groups

notifications

jwt


Purpose

To allow a user to comment on a post that he/she is allowed to comment on.

Body Content [input]

{
    uid: string,
    postID: string,
    comment: string,
    visibility: string,
    reply_to: string
}

Returns if valid [output]

{
    commentID: string,
    error: 0
}

What the API does

  • Finds the profile corresponding to the uid passed and fetches a list of all values inside of the following and blocked fields.
  • Finds the record from the posts collection having a postID corresponding to the one passed [404].
  • Ensures that the record found does not have a value for its visibility field set to none [403].
  • If the record does not have a visibility of none, followers or public, fetch the members field of the AccessGroup corresponding to the value of the visibility field and ensures that uid is among the values of that members field [403].
  • If the record has a value for its groupID field, find the group corresponding to that groupID and check if uid is a value among that group’s members field with a status of accepted [403].
  • Ensures that the record’s uid field does not equal to a value among those of the fetched blocked field above [403].
  • Ensures that the profile that has a uid corresponding to that of the record does not contain a value corresponding to the uid passed in its blocked field [403].
  • Else, if the record does not have a value for its groupID field, and has a value of followers for its visibility field check if uid is among the values of the following field or if it is equal to the uid of the post itself [403].
  • Checks that the post has a value of true for its allow_comments field [403].
  • Checks that there is a comment with a value of reply_to for its commentID field inside of that post’s comments field if reply_to is not empty [404].
  • Loops through each of the comment of the comments field of that post if reply_to is not empty and:
    • Create a new data structure illegalComments for the commentIDs of comments that the user is not allowed to reply to.
    • Append the value of the commentID field of a comment if:
      • That comment if has a visibility set to none.
      • uid is not among the values of the members field obtained by fetching the members field of the AccessGroup corresponding to the value of the visibility field if the comment does not have a visibility of none, followers or public.
      • The value of its uid field is among those of the fetched blocked field above.
      • The blocked field of the profile that has a uid corresponding to that of the comment has the uid passed among its values.
      • uid is not among the values of the followers field of the profile corresponding to the value of the uid field of that comment if the comment has a visibility of followers and is not equal to the uid of that comment either.
  • Checks that the value of the reply_to field passed is not among those of the illegalComments data structure [403].
  • Validates the value of the visibility field passed to make sure that its value is either public, followers, none or one among the accessIDs found in the access_groups field in the record corresponding to that of the uid passed from the profiles collection [400].
  • Generates a unique commentID and a timestamp for the current date/time.
  • Creates and saves a new object inside of that post’s comments field with the values of the uid, comment, reply_to and visibility fields passed, along with the value of the generated timestamp for the created_on and modified_on fields, and uses the generated commentID as well as an empty array for the reacts field.
  • Generates a new notificationID and a timestamp corresponding to the current date and time, and creates a new object in the notifications collection with the following data:
    {
      uid: post.uid,
      notificationID: notificationID,
      content: `uid.${uid} commented on your post`,
      profileID: uid,
      type: "comment_new",
      created_on: timestamp,
      read: false,
      redirect: `/posts/${postID}`
    }
    
  • Generates a new notificationID and a timestamp corresponding to the current date and time, and creates a new object in the notifications collection with the following data if reply_to corresponds to something other than an empty string:
    {
      uid: comments[reply_to].uid,
      notificationID: notificationID,
      content: `uid.${uid} replied to your comment`,
      profileID: uid,
      type: "comment_reply",
      created_on: timestamp,
      read: false,
      redirect: `/posts/${postID}`
    }