Overview
The moderation system provides automated content moderation for user-generated content including chat messages and reviews. It uses a multi-layered approach combining OpenAI’s Moderation API, custom LLM analysis, and Google’s Perspective API to ensure content safety while minimizing false positives.System Flow
Components
1. InstantDB Subscriber Service
Location:server/src/instantdb/subscriber.service.ts
Subscribes to real-time message updates from InstantDB and queues unmoderated messages for processing.
2. Moderation Service
Location:server/src/moderation/moderation.service.ts
Orchestrates the moderation pipeline and determines final actions.
CHAT- Chat messagesREVIEW- Product/service reviews
3. Analysis Service
Location:server/src/moderation/analysis.service.ts
Provides AI/ML analysis capabilities:
4. Queue Processors
Location:server/src/moderation/processors/
Decision Flow
Step 1: OpenAI Moderation (Gate)
All content first passes through OpenAI’s Moderation API. This is a fast, binary check.- Not Flagged: Immediately enqueue notification, skip detailed analysis
- Flagged: Proceed to detailed LLM analysis
Step 2: LLM Flagging Analysis
For flagged content, the LLM provides:reason: Human-readable explanationrecommendedAction:RETAIN,WARN, orREMOVEneedsHumanReview: Boolean for edge cases
Step 3: Context-Specific Analysis
Additional analysis based on content type and context:Data Model
ModerationResultDto
ModerationAction Enum
RETAIN- Content is safeWARN- Content is borderline, may need reviewREMOVE- Content should be hidden/removed
SentimentLabel Enum
POSITIVENEUTRALNEGATIVE
Configuration
Environment variables:Moderation Flow Implementation & Cleanup
Implemented a robust, sequential moderation-to-notification flow and performed comprehensive code cleanup.Changes Made
1. Moderation→Notification Flow Restructure
- Sequential Processing: Modified
ModerationServiceto trigger notifications only after OpenAI moderation passes. - Notification Gate: OpenAI moderation acts as the first filter. If content is flagged, detailed LLM analysis runs but notifications are skipped.
- Decoupled Subscriber: Removed notification logic from
instantdb.service.tsandsubscriber.service.ts, making them purely responsible for queuing moderation. - Idempotency: Leveraged BullMQ
jobIdfor robust deduplication.
2. Code Cleanup & Optimization
- Debug Logs: Removed all
this.logger.debugstatements from moderation-related services. - Console Logs: Removed
console.logstatements fromanalysis.service.ts. - Comment Stripping: Removed all JSDoc and internal comments from:
moderation.service.tsanalysis.service.tsinstantdb.service.ts
- Protocol Fix: Verified and restored URLs corrupted during comment stripping.
Verification Results
Integration Flow
Clean Code Verification
Rangrep checks confirming 0 instances of debug logs and comments in the modified service files.
Queue Configuration
Moderation queues are configured with:removeOnComplete: false(for debugging/audit)attempts: 3with exponential backoffjobId: chat-mod-{messageId}for deduplication
Integration Points
Inbound
- InstantDB Subscriptions: Real-time message stream
- Direct API Calls: For batch moderation
Outbound
- Notification Service: Enqueues notifications for clean content
- InstantDB Updates: Marks messages as moderated with results
Notification Flow
Notifications are only sent for content that passes OpenAI moderation:- OpenAI check returns
!flagged enqueueNotificationForChat()fetches message details- Validates sender/room metadata
- Queues notification job
- Marks message as
notificationProcessedAt