The notification system is a comprehensive feature that handles various types of notifications across the platform. It supports multiple notification types, entities, and provides a flexible way to manage user notifications.
-
Notification Types
- Base Notifications
- Bundled Notifications
- Official Notifications
-
Database Schema
- Notice
- NoticeDetail
- NoticeActor
- NoticeEntity
-
Notification Service
- Core Methods
- Features
-
GraphQL Interface
- Notice Types
- Queries and Mutations
-
User Notifications
interface NoticeUserNewFollowerParams { event: NOTICE_TYPE.user_new_follower recipientId: string actorId: string tag: string }
-
Article Notifications
interface NoticeArticlePublishedParams { event: NOTICE_TYPE.article_published recipientId: string entities: [NotificationEntity<'target', 'article'>] }
-
Collection Notifications
interface NoticeCollectionLikedParams { event: NOTICE_TYPE.collection_liked recipientId: string actorId: string entities: [NotificationEntity<'target', 'collection'>] tag: string }
-
Moment Notifications
interface NoticeMomentLikedParams { event: NOTICE_TYPE.moment_liked recipientId: string actorId: string entities: [NotificationEntity<'target', 'moment'>] tag: string }
-
Comment Notifications
interface NoticeArticleNewCommentParams { event: NOTICE_TYPE.article_new_comment recipientId: string actorId: string entities: [ NotificationEntity<'target', 'article'>, NotificationEntity<'comment', 'comment'> ] tag: string }
-
Transaction Notifications
interface NoticePaymentReceivedDonationParams { event: NOTICE_TYPE.payment_received_donation recipientId: string actorId: string | null entities: [NotificationEntity<'target', 'transaction'>] }
-
Circle Notifications
interface NoticeCircleInvitationParams { event: NOTICE_TYPE.circle_invitation actorId: string recipientId: string entities: [NotificationEntity<'target', 'circle'>] }
Special notifications that group related events:
- Circle broadcast replies
- Circle discussion updates
- Circle member activities
System-level notifications:
- User status changes (banned, frozen, unbanned)
- Content moderation (article/comment bans)
- Content reports
- Write challenges
- Badge awards
interface Notice {
id: string
uuid: string
unread: boolean
deleted: boolean
noticeDetailId: string
recipientId: string
createdAt: Date
updatedAt: Date
}interface NoticeDetail {
id: string
noticeType: string
message: string
data: any
createdAt: Date
}The NotificationService class provides the following key functionalities:
-
Trigger Notification
public trigger = async (params: NotificationParams) => { // Sends a new notification }
-
Withdraw Notification
public withdraw = async (tag: string) => { // Cancels/withdraws notifications with a specific tag }
-
Mark All as Read
public markAllNoticesAsRead = async (userId: string) => { // Marks all notifications as read for a user }
-
Find User Notifications
public findByUser = async ({ userId, onlyRecent, take, skip, }: { userId: string onlyRecent?: boolean take?: number skip?: number }): Promise<NoticeItem[]>
-
Count Notifications
public countNotice = async ({ userId, unread, onlyRecent, }: { userId: string unread?: boolean onlyRecent?: boolean })
The system exposes a GraphQL API with the following main types:
-
UserNotice
type UserNotice implements Notice { id: ID! unread: Boolean! createdAt: DateTime! actors: [User!] type: UserNoticeType! target: User! }
-
ArticleNotice
type ArticleNotice implements Notice { id: ID! unread: Boolean! createdAt: DateTime! actors: [User!] type: ArticleNoticeType! target: Article! }
-
CircleNotice
type CircleNotice implements Notice { id: ID! unread: Boolean! createdAt: DateTime! actors: [User!] type: CircleNoticeType! target: Circle! comments: [Comment!] replies: [Comment!] mentions: [Comment!] }
// Example: Creating a new follower notification
const notificationParams: NoticeUserNewFollowerParams = {
event: NOTICE_TYPE.user_new_follower,
recipientId: "123",
actorId: "456",
tag: "follow-123"
};
await notificationService.trigger(notificationParams);// Get recent notifications for a user
const notices = await notificationService.findByUser({
userId: "123",
onlyRecent: true,
take: 20,
skip: 0
});// Mark all notifications as read
await notificationService.markAllNoticesAsRead("123");
// Withdraw a notification
await notificationService.withdraw("follow-123");-
Notification Tags
- Use tags for notifications that might need to be withdrawn
- Tags should be unique and descriptive
-
Performance
- Use the
onlyRecentflag when querying notifications - Implement pagination for large notification lists
- Utilize the caching system for frequently accessed notifications
- Use the
To add a new notice type to the system, follow these steps:
-
Add Notice Type Enum
// In src/common/enums/notification.ts export enum NOTICE_TYPE { // ... existing types ... new_notice_type = 'new_notice_type' }
-
Define Notice Parameters Interface
// In src/definitions/notification.d.ts interface NoticeNewTypeParams extends NotificationRequiredParams { event: NOTICE_TYPE.new_notice_type recipientId: string entities: [NotificationEntity<'target', 'article'>] // Adjust entity types as needed }
-
Add to NotificationParams Union Type
// In src/definitions/notification.d.ts export type NotificationParams = | // ... existing types ... | NoticeNewTypeParams
-
Update Notice Type Mapping
// In src/queries/notice/index.ts const notice: { // ... existing mappings ... new_notice_type: NOTICE_TYPE.ArticleNotice, // Choose appropriate notice type }
-
Add Type Case Handler
// In src/queries/notice/index.ts case INNER_NOTICE_TYPE.new_notice_type: return 'NewType'
-
Implement Trigger Logic
// In your service/mutation file await notificationService.trigger({ event: NOTICE_TYPE.new_notice_type, recipientId: userId, entities: [ { type: 'target', entityTable: 'article', entity: article, }, ], })
- Notifications are stored for 6 months by default
- Some notification types may be deprecated (e.g.,
circle_new_article) - The system requires Redis and AWS SQS for full functionality
src/definitions/notification.d.ts: Type definitionssrc/queries/notice/index.ts: GraphQL resolverssrc/connectors/notificationService/index.ts: Core service implementationsrc/common/enums/notification.ts: Notification type enums