Like, Comment & Share

post module
Endpoints: POST /:publicId/like · POST /:publicId/reply · POST /:publicId/repost · POST /:publicId/quote
4
Tương tác
like · reply · repost · quote
Redis
Like storage
SET + counter + queue flush
1 cấp
Nested comment
reply → reply (1 level)
2
Kiểu chia sẻ
repost (nguyên bài) · quote (thêm lời)

User Perspective


Like
Nhấn nút ❤️ trên bài viết → tim đổi màu, số like tăng lên tức thì. Nhấn lại → unlike. Tất cả xảy ra gần như ngay lập tức nhờ Redis.
Comment
Nhấn "Trả lời" → mở giao diện viết reply → nhập nội dung (hỗ trợ text + media + @mention) → gửi. Reply xuất hiện dưới bài gốc. Có thể reply lại 1 reply (nested 1 cấp).
Repost
Nhấn nút "Chia sẻ" → chọn "Repost" → bài gốc xuất hiện trên trang cá nhân người dùng, giữ nguyên nội dung.
Quote
Nhấn "Chia sẻ" → chọn "Trích dẫn" → viết thêm lời bình → đăng. Bài viết mới chứa bài gốc bên trong kèm bình luận của mình.

Like Flow (Redis-based)


POST /:publicId/like isLiked? true (like) Redis: SADD post:{id}:likes nếu added=1 → INCR likeCount LPUSH → LIKED_ADD_QUEUE false (unlike) Redis: SREM post:{id}:likes nếu removed=1 → DECR likeCount LPUSH → LIKED_REMOVE_QUEUE Redis: SCARD → trả về likeCount Worker flush queue → sync MySQL định kỳ redis operation async queue

Like/Unlike dùng Redis SET để đảm bảo idempotent. Worker background sẽ flush queue về MySQL định kỳ.

API Detail


# API / Tính năng Mô tả nghiệp vụ Trạng thái
01
POST /:publicId/like
Like / Unlike
Toggle like: isLiked=true → thêm vào Redis SET + tăng counter, push queue. isLiked=false → xóa khỏi SET + giảm counter, push queue khác. Đảm bảo idempotent (like 2 lần = 1 like). done
02
POST /:publicId/reply
Comment (Reply)
Tạo bài mới loại REPLY gắn với parentPublicId. Hỗ trợ đầy đủ text + media + @mention. Transaction + attachPostMeta. Nested 1 cấp (reply → reply). done
03
POST /:publicId/repost
Chia sẻ nguyên bài
Tạo bài loại REPOST link tới bài gốc bằng originPublicId + originPostId. Không cần transaction vì không có meta phụ. done
04
POST /:publicId/quote
Trích dẫn + bình luận
Tạo bài loại QUOTE. Giống reply nhưng link tới bài gốc thay vì parent. Có transaction, mention, topic. User viết thêm nội dung kèm bài gốc. done
05
GET /:publicId/replies
Lấy danh sách comment
Trả về danh sách reply của bài theo cursor pagination, sắp xếp mới nhất. Public endpoint, không cần auth. done
06
Nested comment cấp 2+
Comment trả lời comment
Hiện tại reply một reply tạo thành nested 1 cấp. Logic chưa enforce giới hạn cấp, cần thêm validation. wip
07
Like count sync
Worker flush Redis → DB
Worker đọc LIKED_ADD_QUEUE / LIKED_REMOVE_QUEUE, batch update likesCount trong MySQL. Đảm bảo dữ liệu nhất quán. done
08
Thông báo khi tương tác
Notification
Gửi thông báo realtime cho chủ bài khi có like / comment / quote / repost. Hiện chưa implement. todo

Implementation Progress


✅ Đã xong
Like/Unlike qua Redis SET (idempotent)
Like counter qua Redis INCR/DECR
Queue sync like → MySQL
Reply (comment) đầy đủ tính năng
Repost — chia sẻ nguyên bài
Quote — trích dẫn + thêm lời
GET /:publicId/replies với pagination
⏳ Cần bổ sung
Enforce giới hạn nested comment (max 1 cấp)
Thông báo realtime khi like/comment/share
Đếm comment count trên bài gốc
Đếm repost/quote count trên bài gốc
Chống spam like (rate limit)

Design Notes


Redis Like
Like dùng Redis SET (SADD/SREM) vì cần idempotent + real-time. Worker background flush về DB theo batch, giảm tải write MySQL.
Comment = Reply
Trong hệ thống, "comment" thực chất là một bài viết loại REPLYparentPublicId trỏ về bài gốc. Dùng chung bảng Post.
Repost vs Quote
Repost = chia sẻ nguyên xi, không có transaction/meta. Quote = tạo bài mới chứa bài gốc bên trong, có đầy đủ mention/topic/transaction.
Nested 1 cấp
Reply một reply tạo thread 2 cấp. Cần enforce logic để không cho nested sâu hơn (tránh phức tạp UI + query).