Thông báo Realtime

notification module
Loại: like · comment · mention · follow · friend_request · new_message  ·  Channel: Pusher private-user-{userId}
6
Loại thông báo
like comment mention follow friend message
Bảng Notification
model sẵn trong Prisma
Pusher
Delivery
sendRealtimeNotification()
Stub
Logic
chưa gọi trong service thật

Proposed: Notification Flow


SỰ KIỆN TRIGGER User A like bài User A comment bài User A @mention B User A follow B Lời mời kết bạn Tin nhắn mới NotificationService.create() type, actorId, recipientId, targetId, message INSERT Notification isRead=false, createdAt=now() Pusher: trigger notification channel=private-user-{recipientId} FE: toast popup badge count +1 PATCH /notifications/:id/read isRead=true → badge count -1

Mọi sự kiện trong app → NotificationService.create() → lưu DB + Pusher realtime → FE hiện toast + badge.

Implementation Checklist


#TaskChi tiếtStatus
01
Bảng Notification
Đã có trong Prisma. Cần review thêm field: type (enum), actorId, recipientId, targetType, targetId, message, isRead.done
02
NotificationService.create()
Method tạo notification: validate → INSERT → Pusher trigger. Cần gọi từ các service khác (Post, Chat, User).stub
03
Gọi từ PostService.like()
Sau khi like thành công → notificationService.create({ type: 'LIKE', actorId, recipientId: post.userId }). Không gửi nếu like bài của chính mình.todo
04
Gọi từ PostService.reply()
Sau khi reply → notification cho chủ bài gốc. Tương tự cho quote, repost.todo
05
Gọi từ mention processing
Khi attachPostMeta xử lý mentions → notification cho mỗi user được @mention.todo
06
Gọi từ follow/friend request
Khi follow → notify. Khi gửi friend request → notify. Khi accept → notify.todo
07
Gọi từ chat
Tin nhắn mới → notification (nếu user không đang ở conversation đó). Avoid duplicate: không gửi nếu đang online trong chat.todo
08
GET /notifications
Danh sách notifications của user. Cursor pagination. Sort mới nhất. Include actor info (name, avatar).todo
09
PATCH /notifications/:id/read
Đánh dấu đã đọc 1 notification. Hoặc POST /notifications/read-all đánh dấu tất cả.todo
10
GET /notifications/unread-count
Trả số lượng notification chưa đọc. Cache trong Redis, invalidate khi có mới hoặc đọc.todo

Potential Issues


Spam notif
User A like/unlike liên tục → spam notification. Giải pháp: debounce — group notifications cùng loại trong 5 phút. "A và 3 người khác thích bài của bạn".
Self-notif
Like/comment bài của chính mình → không nên gửi notification. Cần check actorId !== recipientId trước khi tạo.
Batch insert
Mention 5 người = 5 notifications. Dùng createMany thay vì loop create để giảm DB round-trip.
Push mobile
Pusher chỉ cho web realtime. Mobile cần FCM/APNs push notification. Implement Phase 2 — tách thành NotificationDispatcher với strategy pattern.