Likes API Documentation

Overview

The Likes API allows users to view and manage likes on a specific post. This includes retrieving users who have liked a post with pagination and search functionality. Real-time updates for likes are handled via WebSockets.

Note: Ensure that all API requests include a valid JSON Web Token (JWT) in the x-token header to authenticate users.

Endpoints

POST /api/likes/:postId

Retrieve the list of users who liked the post specified by :postId, with pagination and optional search.

Request Headers:

x-token: <JWT>

Request Body:

{
  "page": <number> (optional, defaults to 1),
  "limit": <number> (optional, defaults to 10),
  "searchQuery": <string> (optional, for filtering by user name)
}

Response:

{
  "totalLikes": <number>,
  "totalPages": <number>,
  "page": <number>,
  "likes": [
    {
      "_id": "userId",
      "profileImage": "imageUrl",
      "firstName": "John",
      "lastName": "Doe"
    }
  ]
}

Real-Time Updates

The Likes feature uses WebSockets to handle real-time updates for likes. This ensures that any changes to likes are instantly reflected on the UI for all connected clients.

Note: Ensure that all API requests include a valid JSON Web Token (JWT) in the x-token header to authenticate users.

Explanation: The WebSocket implementation for likes operates with the following events:

  • getPostLikes: Emits the current likes for a post identified by postId. The server fetches the post and sends an update to the client.
  • like: Handles a like action by a user. The server updates the post's likes array and emits the updated list of likes to all connected clients. If the user is liking the post for the first time and is not the post owner, a notification is sent to the post owner.

Warning: Notifications are currently supported only for likes on posts. Notifications for comments and other features will be available in a future update.