fix: align dashboard message api shape
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import type { Router } from "express";
|
import type { Router } from "express";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import { AppError } from "../errors";
|
import { AppError } from "../errors";
|
||||||
import { createChildLogger } from "../logger";
|
|
||||||
import {
|
import {
|
||||||
getAttachmentsByChannel,
|
getAttachmentsByChannel,
|
||||||
getMessageById,
|
getMessageById,
|
||||||
@@ -9,9 +8,22 @@ import {
|
|||||||
listMessages,
|
listMessages,
|
||||||
listReviewMessages,
|
listReviewMessages,
|
||||||
} from "../moderation/messageStore";
|
} from "../moderation/messageStore";
|
||||||
import type { MessageQuery } from "../moderation/types";
|
import type { AIStatus, MessageQuery } from "../moderation/types";
|
||||||
|
|
||||||
const logger = createChildLogger("message-routes");
|
const aiStatuses = new Set<AIStatus>([
|
||||||
|
"pending",
|
||||||
|
"clean",
|
||||||
|
"warn",
|
||||||
|
"flagged",
|
||||||
|
"error",
|
||||||
|
]);
|
||||||
|
|
||||||
|
function parseStatuses(value?: string): AIStatus[] | undefined {
|
||||||
|
if (!value) return undefined;
|
||||||
|
return value.split(",").filter((item): item is AIStatus =>
|
||||||
|
aiStatuses.has(item as AIStatus),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function createMessageRoutes(): Router {
|
export function createMessageRoutes(): Router {
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@@ -39,21 +51,19 @@ export function createMessageRoutes(): Router {
|
|||||||
cursor?: string;
|
cursor?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Support both 'channel' (legacy) and 'channelId' (new)
|
|
||||||
const targetChannel = channelId || channel;
|
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 limitNum = Math.min(parseInt(limit) || 50, 100);
|
||||||
const offsetNum = parseInt(offset) || 0;
|
const offsetNum = parseInt(offset) || 0;
|
||||||
|
|
||||||
if (type === "image") {
|
if (type === "image") {
|
||||||
|
if (!targetChannel) {
|
||||||
|
throw new AppError(
|
||||||
|
"channel or channelId query parameter is required",
|
||||||
|
"MISSING_CHANNEL",
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const attachments = await getAttachmentsByChannel(
|
const attachments = await getAttachmentsByChannel(
|
||||||
targetChannel,
|
targetChannel,
|
||||||
limitNum,
|
limitNum,
|
||||||
@@ -63,8 +73,30 @@ export function createMessageRoutes(): Router {
|
|||||||
type: "image",
|
type: "image",
|
||||||
data: attachments,
|
data: attachments,
|
||||||
count: attachments.length,
|
count: attachments.length,
|
||||||
|
nextCursor: null,
|
||||||
|
});
|
||||||
|
} else if (channelId || cursor || status) {
|
||||||
|
const result = await listMessages({
|
||||||
|
channelId: targetChannel,
|
||||||
|
cursor,
|
||||||
|
limit: limitNum,
|
||||||
|
status: parseStatuses(status),
|
||||||
|
});
|
||||||
|
res.json({
|
||||||
|
type: "text",
|
||||||
|
data: result.data,
|
||||||
|
count: result.data.length,
|
||||||
|
nextCursor: result.nextCursor,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (!targetChannel) {
|
||||||
|
throw new AppError(
|
||||||
|
"channel or channelId query parameter is required",
|
||||||
|
"MISSING_CHANNEL",
|
||||||
|
400,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const messages = await getMessagesByChannel(
|
const messages = await getMessagesByChannel(
|
||||||
targetChannel,
|
targetChannel,
|
||||||
limitNum,
|
limitNum,
|
||||||
@@ -74,6 +106,7 @@ export function createMessageRoutes(): Router {
|
|||||||
type: "text",
|
type: "text",
|
||||||
data: messages,
|
data: messages,
|
||||||
count: messages.length,
|
count: messages.length,
|
||||||
|
nextCursor: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user