2026-05-14 19:46:47 +07:00
|
|
|
import type { Router } from "express";
|
|
|
|
|
import express from "express";
|
|
|
|
|
import { AppError } from "../errors";
|
|
|
|
|
import {
|
|
|
|
|
getAnalysisQueueStatus,
|
|
|
|
|
queueMessageAnalysis,
|
|
|
|
|
} from "../moderation/aiAnalyzer";
|
2026-05-18 06:04:30 +07:00
|
|
|
import {
|
|
|
|
|
getMessageById,
|
|
|
|
|
updateMessageAIAnalysis,
|
|
|
|
|
} from "../moderation/messageStore";
|
2026-05-14 19:46:47 +07:00
|
|
|
|
|
|
|
|
export function createAnalysisRoutes(): Router {
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
|
|
|
|
// GET /api/analysis/status - Get current analysis queue status
|
|
|
|
|
router.get("/analysis/status", (_req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const status = getAnalysisQueueStatus();
|
|
|
|
|
res.json(status);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /api/messages/:id/reanalyze - Queue a message for re-analysis
|
|
|
|
|
router.post("/messages/:id/reanalyze", async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!id) {
|
|
|
|
|
throw new AppError("Message ID is required", "MISSING_MESSAGE_ID", 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify message exists
|
|
|
|
|
const message = await getMessageById(id);
|
|
|
|
|
if (!message) {
|
|
|
|
|
throw new AppError("Message not found", "MESSAGE_NOT_FOUND", 404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 06:04:30 +07:00
|
|
|
// Reset analysis status to pending so it gets picked up by the analyzer
|
|
|
|
|
await updateMessageAIAnalysis(id, {
|
|
|
|
|
status: "pending",
|
|
|
|
|
flags: null,
|
|
|
|
|
score: null,
|
|
|
|
|
raw: null,
|
|
|
|
|
analysis: null,
|
|
|
|
|
analyzedAt: null,
|
|
|
|
|
error: null,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 19:46:47 +07:00
|
|
|
// Queue for analysis
|
|
|
|
|
await queueMessageAnalysis(id);
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
success: true,
|
|
|
|
|
messageId: id,
|
|
|
|
|
queued: true,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return router;
|
|
|
|
|
}
|