refactor: resolve architecture disconnects and codebase weaknesses
- Consolidate eventTypes.ts as single source of truth for Redis channels: - Remove duplicate DiscordGatewayEvent interface from eventBroadcaster.ts - Replace all hardcoded channel strings with EventChannels constants - eventTypes.ts is no longer an orphan file - Remove dangerous moderation action feature (selfbot safety): - Remove /messages/:id/moderate endpoint from backend - Remove moderation:action handler from commandHandler.ts - Remove publishFireAndForgetCommand (wrong envelope format) - Verified no remaining references to moderation:action in code - Remove unused getCommandPublisher import from redis-bridge.ts Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
3614d32701
commit
7d6612bb2d
@@ -124,64 +124,5 @@ export function createMessagesRouter(): Router {
|
||||
}),
|
||||
);
|
||||
|
||||
// POST /api/messages/:id/moderate — Trigger moderation action via DG
|
||||
router.post(
|
||||
"/messages/:id/moderate",
|
||||
asyncHandler(async (req: Request, res: Response) => {
|
||||
const id = String(req.params.id ?? "");
|
||||
if (!id) {
|
||||
res.status(400).json({ error: "MISSING_ID" });
|
||||
return;
|
||||
}
|
||||
|
||||
const { actionType, reason } = (req.body ?? {}) as {
|
||||
actionType?: string;
|
||||
reason?: string;
|
||||
};
|
||||
|
||||
const allowedActions = [
|
||||
"delete_message",
|
||||
"warn_user",
|
||||
"kick_user",
|
||||
"ban_user",
|
||||
"mute_user",
|
||||
];
|
||||
|
||||
if (!actionType || !allowedActions.includes(actionType)) {
|
||||
res.status(400).json({
|
||||
error: "INVALID_ACTION",
|
||||
message: `actionType must be one of: ${allowedActions.join(", ")}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch the message to get guild/user context
|
||||
const msg = await messagesService.getMessageById(id).catch(() => null);
|
||||
if (!msg) {
|
||||
res.status(404).json({ error: "MESSAGE_NOT_FOUND" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Publish command to DG via Redis
|
||||
const { publishCommand } = await import("../../ws/redis-bridge.js");
|
||||
await publishCommand({
|
||||
id: crypto.randomUUID(),
|
||||
type: "moderation:action",
|
||||
payload: {
|
||||
messageId: id,
|
||||
guildId: msg.guild_id,
|
||||
channelId: msg.thread_id || msg.channel_id,
|
||||
userId: msg.user_id,
|
||||
actionType,
|
||||
reason: reason ?? "Manual moderation from dashboard",
|
||||
requestedAt: Date.now(),
|
||||
},
|
||||
});
|
||||
|
||||
logger.info({ id, actionType, reason }, "Moderation action dispatched");
|
||||
res.json({ ok: true, actionType, messageId: id });
|
||||
}),
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { createChildLogger } from "@bete/shared/logger";
|
||||
import Redis from "ioredis";
|
||||
import { config } from "../shared/config/index.js";
|
||||
import { getCommandPublisher } from "../shared/redis/index.js";
|
||||
import { broadcastRaw } from "./broadcast.js";
|
||||
|
||||
const logger = createChildLogger("ws.redis-bridge");
|
||||
@@ -35,24 +34,6 @@ function createSubscriber(): Redis {
|
||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a command to the Discord Gateway via Redis.
|
||||
* The DG's commandHandler listens on "backend:command" channel.
|
||||
*/
|
||||
export async function publishCommand(
|
||||
payload: Record<string, unknown>,
|
||||
): Promise<void> {
|
||||
const pub = getCommandPublisher();
|
||||
const envelope = {
|
||||
type: "command",
|
||||
data: payload,
|
||||
timestamp: Date.now(),
|
||||
source: "backend",
|
||||
};
|
||||
await pub.publish("backend:command", JSON.stringify(envelope));
|
||||
logger.debug({ payload }, "Published command to DG");
|
||||
}
|
||||
|
||||
function handleSubscriptionMessage(channel: string, message: string): void {
|
||||
const mapping = SUBSCRIPTIONS.find((m) => m.channel === channel);
|
||||
if (!mapping) {
|
||||
|
||||
Reference in New Issue
Block a user