refactor: split api routes by concern

This commit is contained in:
MythEclipse
2026-05-14 19:46:47 +07:00
parent 3fb1fcb72c
commit c81a499535
7 changed files with 503 additions and 207 deletions

View File

@@ -0,0 +1,57 @@
import type { Router } from "express";
import express from "express";
import { AppError } from "../errors";
import { createChildLogger } from "../logger";
import {
getAnalysisQueueStatus,
queueMessageAnalysis,
} from "../moderation/aiAnalyzer";
import { getMessageById } from "../moderation/messageStore";
const logger = createChildLogger("analysis-routes");
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);
}
// Queue for analysis
await queueMessageAnalysis(id);
logger.info({ messageId: id }, "Message queued for re-analysis");
res.json({
success: true,
messageId: id,
queued: true,
});
} catch (error) {
next(error);
}
});
return router;
}

189
src/routes/messageRoutes.ts Normal file
View File

@@ -0,0 +1,189 @@
import type { Router } from "express";
import express from "express";
import { AppError } from "../errors";
import { createChildLogger } from "../logger";
import {
getAttachmentsByChannel,
getMessageById,
getMessagesByChannel,
listMessages,
listReviewMessages,
} from "../moderation/messageStore";
import type { MessageQuery } from "../moderation/types";
const logger = createChildLogger("message-routes");
export function createMessageRoutes(): Router {
const router = express.Router();
// GET /api/messages - List messages by channel (backward compatible)
// Query params: channel (required), type (text|image), limit, offset
// Also supports new params: channelId, status, cursor, limit
router.get("/messages", async (req, res, next) => {
try {
const {
channel,
channelId,
type,
limit = "50",
offset = "0",
status,
cursor,
} = req.query as {
channel?: string;
channelId?: string;
type?: string;
limit?: string;
offset?: string;
status?: string;
cursor?: string;
};
// Support both 'channel' (legacy) and 'channelId' (new)
const targetChannel = channelId || channel;
if (!targetChannel) {
throw new AppError(
"channel or channelId query parameter is required",
"MISSING_CHANNEL",
400,
);
}
const limitNum = Math.min(parseInt(limit) || 50, 100);
const offsetNum = parseInt(offset) || 0;
if (type === "image") {
const attachments = await getAttachmentsByChannel(
targetChannel,
limitNum,
offsetNum,
);
res.json({
type: "image",
data: attachments,
count: attachments.length,
});
} else {
const messages = await getMessagesByChannel(
targetChannel,
limitNum,
offsetNum,
);
res.json({
type: "text",
data: messages,
count: messages.length,
});
}
} catch (error) {
next(error);
}
});
// GET /api/messages/:id - Get a specific message
router.get("/messages/:id", async (req, res, next) => {
try {
const { id } = req.params;
if (!id) {
throw new AppError("Message ID is required", "MISSING_MESSAGE_ID", 400);
}
const message = await getMessageById(id);
if (!message) {
throw new AppError("Message not found", "MESSAGE_NOT_FOUND", 404);
}
res.json(message);
} catch (error) {
next(error);
}
});
// GET /api/review - List messages flagged for review
// Query params: guildId, channelId, threadId, userId, cursor, limit
router.get("/review", async (req, res, next) => {
try {
const {
guildId,
channelId,
threadId,
userId,
cursor,
limit = "50",
} = req.query as {
guildId?: string;
channelId?: string;
threadId?: string;
userId?: string;
cursor?: string;
limit?: string;
};
const limitNum = Math.min(parseInt(limit) || 50, 100);
const query: Omit<MessageQuery, "status"> = {
guildId,
channelId,
threadId,
userId,
cursor,
limit: limitNum,
};
const result = await listReviewMessages(query);
res.json({
data: result.data,
nextCursor: result.nextCursor,
count: result.data.length,
});
} catch (error) {
next(error);
}
});
// GET /api/attachments - List attachments by channel
// Query params: channel (required), limit, offset
router.get("/attachments", async (req, res, next) => {
try {
const {
channel,
limit = "50",
offset = "0",
} = req.query as {
channel?: string;
limit?: string;
offset?: string;
};
if (!channel) {
throw new AppError(
"channel query parameter is required",
"MISSING_CHANNEL",
400,
);
}
const limitNum = Math.min(parseInt(limit) || 50, 100);
const offsetNum = parseInt(offset) || 0;
const attachments = await getAttachmentsByChannel(
channel,
limitNum,
offsetNum,
);
res.json({
data: attachments,
count: attachments.length,
});
} catch (error) {
next(error);
}
});
return router;
}

53
src/routes/syncRoutes.ts Normal file
View File

@@ -0,0 +1,53 @@
import type { Client } from "discord.js-selfbot-v13";
import type { Router } from "express";
import express from "express";
import { AppError } from "../errors";
import { createChildLogger } from "../logger";
import { syncSelectedChannelBacklog } from "../moderation/backlogSync";
const logger = createChildLogger("sync-routes");
export function createSyncRoutes(client: Client): Router {
const router = express.Router();
// POST /api/backlog-sync - Sync message backlog for a channel
router.post("/backlog-sync", async (req, res, next) => {
try {
const { guildId, channelId } = req.body as {
guildId?: string;
channelId?: string;
};
if (!guildId || !channelId) {
throw new AppError(
"guildId and channelId are required",
"MISSING_BACKLOG_PARAMS",
400,
);
}
logger.info({ guildId, channelId }, "Starting backlog sync");
const count = await syncSelectedChannelBacklog(
client,
guildId,
channelId,
);
logger.info(
{ guildId, channelId, messagesSync: count },
"Backlog sync complete",
);
res.json({
success: true,
channelId,
messagesSync: count,
});
} catch (error) {
next(error);
}
});
return router;
}

View File

@@ -0,0 +1,47 @@
import type { Router } from "express";
import express from "express";
import { createChildLogger } from "../logger";
const logger = createChildLogger("ui-state-routes");
export interface SharedUIState {
selectedGuild: string;
selectedVoiceChannel: string;
selectedTextChannel: string;
activeTab: "voice" | "text";
isListening: boolean;
isStreaming: boolean;
}
export interface UIStateRouteOptions {
getSharedUIState: () => SharedUIState;
patchSharedUIState: (patch: Partial<SharedUIState>) => SharedUIState;
}
export function createUIStateRoutes(options: UIStateRouteOptions): Router {
const router = express.Router();
const { getSharedUIState, patchSharedUIState } = options;
// GET /api/ui-state - Get current UI state
router.get("/ui-state", (_req, res, next) => {
try {
const state = getSharedUIState();
res.json(state);
} catch (error) {
next(error);
}
});
// POST /api/ui-state - Update UI state
router.post("/ui-state", (req, res, next) => {
try {
const patch = req.body as Partial<SharedUIState>;
const updated = patchSharedUIState(patch);
res.json(updated);
} catch (error) {
next(error);
}
});
return router;
}

118
src/routes/voiceRoutes.ts Normal file
View File

@@ -0,0 +1,118 @@
import type { Router } from "express";
import express from "express";
import { AppError } from "../errors";
import { createChildLogger } from "../logger";
import type { VoiceController } from "../voiceController";
const logger = createChildLogger("voice-routes");
export function createVoiceRoutes(voiceController: VoiceController): Router {
const router = express.Router();
// GET /api/status - Get voice connection status
router.get("/status", (_req, res, next) => {
try {
const status = voiceController.getStatus();
res.json(status);
} catch (error) {
next(error);
}
});
// GET /api/guilds - List available guilds
router.get("/guilds", (_req, res, next) => {
try {
const guilds = voiceController.listGuilds();
res.json(guilds);
} catch (error) {
next(error);
}
});
// GET /api/guilds/:guildId/voice-channels - List voice channels in a guild
router.get("/guilds/:guildId/voice-channels", async (req, res, next) => {
try {
const { guildId } = req.params;
if (!guildId) {
throw new AppError("Guild ID is required", "MISSING_GUILD_ID", 400);
}
const channels = await voiceController.listVoiceChannels(guildId);
res.json(channels);
} catch (error) {
next(error);
}
});
// GET /api/guilds/:guildId/channels - List text channels in a guild
router.get("/guilds/:guildId/channels", async (req, res, next) => {
try {
const { guildId } = req.params;
if (!guildId) {
throw new AppError("Guild ID is required", "MISSING_GUILD_ID", 400);
}
const channels = await voiceController.listWatchableChannels(guildId);
res.json(channels);
} catch (error) {
next(error);
}
});
// GET /api/guilds/:guildId/threads - List threads in a guild
router.get("/guilds/:guildId/threads", async (req, res, next) => {
try {
const { guildId } = req.params;
if (!guildId) {
throw new AppError("Guild ID is required", "MISSING_GUILD_ID", 400);
}
const threads = await voiceController.listThreads(guildId);
res.json(threads);
} catch (error) {
next(error);
}
});
// POST /api/connect - Connect to a voice channel
router.post("/connect", async (req, res, next) => {
try {
const { guildId, channelId } = req.body as {
guildId?: string;
channelId?: string;
};
if (!guildId || !channelId) {
throw new AppError(
"guildId and channelId are required",
"MISSING_CONNECT_FIELDS",
400,
);
}
logger.info({ guildId, channelId }, "Connecting to voice channel");
const status = await voiceController.connect(guildId, channelId);
res.json(status);
} catch (error) {
next(error);
}
});
// POST /api/disconnect - Disconnect from voice channel
router.post("/disconnect", async (_req, res, next) => {
try {
logger.info("Disconnecting from voice channel");
const status = await voiceController.disconnect();
res.json(status);
} catch (error) {
next(error);
}
});
return router;
}