2026-05-14 15:41:11 +07:00
|
|
|
import { getDatabase } from "../database/drizzle";
|
|
|
|
|
import { messagesTable, attachmentsTable } from "../database/schema";
|
|
|
|
|
import { eq, or, desc, asc, and, isNull } from "drizzle-orm";
|
2026-05-14 15:02:23 +07:00
|
|
|
import { createChildLogger } from "../logger";
|
|
|
|
|
import type { AttachmentRecord, MessageRecord } from "./types";
|
2026-05-13 19:34:14 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("message-store");
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function insertMessage(
|
2026-05-14 15:02:23 +07:00
|
|
|
message: MessageRecord,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db.insert(messagesTable).values(message).onConflictDoNothing();
|
2026-05-13 19:34:14 +07:00
|
|
|
|
2026-05-14 15:02:23 +07:00
|
|
|
logger.debug(
|
|
|
|
|
{ messageId: message.id, channelId: message.channel_id },
|
|
|
|
|
"Message inserted",
|
|
|
|
|
);
|
2026-05-13 19:34:14 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
messageId: message.id,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to insert message",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function updateMessageAsEdited(
|
2026-05-13 19:34:14 +07:00
|
|
|
messageId: string,
|
|
|
|
|
editedContent: string,
|
|
|
|
|
editedAt: number,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db
|
|
|
|
|
.update(messagesTable)
|
|
|
|
|
.set({
|
|
|
|
|
edited_content: editedContent,
|
|
|
|
|
edited_at: editedAt,
|
|
|
|
|
type: "edited",
|
|
|
|
|
})
|
|
|
|
|
.where(eq(messagesTable.id, messageId));
|
2026-05-13 19:34:14 +07:00
|
|
|
|
|
|
|
|
logger.debug({ messageId }, "Message marked as edited");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
messageId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to update message as edited",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function updateMessageAsDeleted(
|
2026-05-13 19:34:14 +07:00
|
|
|
messageId: string,
|
|
|
|
|
deletedAt: number,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db
|
|
|
|
|
.update(messagesTable)
|
|
|
|
|
.set({
|
|
|
|
|
deleted_at: deletedAt,
|
|
|
|
|
type: "deleted",
|
|
|
|
|
})
|
|
|
|
|
.where(eq(messagesTable.id, messageId));
|
2026-05-13 19:34:14 +07:00
|
|
|
|
|
|
|
|
logger.debug({ messageId }, "Message marked as deleted");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
messageId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to update message as deleted",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function getMessagesByChannel(
|
2026-05-13 19:34:14 +07:00
|
|
|
channelId: string,
|
|
|
|
|
limit: number = 50,
|
|
|
|
|
offset: number = 0,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<MessageRecord[]> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(messagesTable)
|
|
|
|
|
.where(
|
|
|
|
|
or(
|
|
|
|
|
eq(messagesTable.channel_id, channelId),
|
|
|
|
|
eq(messagesTable.thread_id, channelId),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(desc(messagesTable.created_at))
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.offset(offset);
|
2026-05-13 19:34:14 +07:00
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
return rows as MessageRecord[];
|
2026-05-13 19:34:14 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
channelId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to get messages by channel",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function insertAttachment(
|
2026-05-14 15:02:23 +07:00
|
|
|
attachment: AttachmentRecord,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db.insert(attachmentsTable).values(attachment).onConflictDoNothing();
|
2026-05-13 19:34:14 +07:00
|
|
|
|
2026-05-14 15:02:23 +07:00
|
|
|
logger.debug(
|
|
|
|
|
{ attachmentId: attachment.id, messageId: attachment.message_id },
|
|
|
|
|
"Attachment inserted",
|
|
|
|
|
);
|
2026-05-13 19:34:14 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
attachmentId: attachment.id,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to insert attachment",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function getAttachmentsByChannel(
|
2026-05-13 19:34:14 +07:00
|
|
|
channelId: string,
|
|
|
|
|
limit: number = 50,
|
|
|
|
|
offset: number = 0,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<AttachmentRecord[]> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(attachmentsTable)
|
|
|
|
|
.where(
|
|
|
|
|
or(
|
|
|
|
|
eq(attachmentsTable.channel_id, channelId),
|
|
|
|
|
eq(attachmentsTable.thread_id, channelId),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(desc(attachmentsTable.created_at))
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.offset(offset);
|
2026-05-13 19:34:14 +07:00
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
return rows as AttachmentRecord[];
|
2026-05-13 19:34:14 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
channelId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to get attachments by channel",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function updateAttachmentAsUploaded(
|
2026-05-13 19:34:14 +07:00
|
|
|
attachmentId: string,
|
|
|
|
|
uploadedUrl: string,
|
|
|
|
|
uploadedAt: number,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db
|
|
|
|
|
.update(attachmentsTable)
|
|
|
|
|
.set({
|
|
|
|
|
uploaded_url: uploadedUrl,
|
|
|
|
|
upload_status: "uploaded",
|
|
|
|
|
uploaded_at: uploadedAt,
|
|
|
|
|
})
|
|
|
|
|
.where(eq(attachmentsTable.id, attachmentId));
|
2026-05-13 19:34:14 +07:00
|
|
|
|
2026-05-14 15:02:23 +07:00
|
|
|
logger.debug(
|
|
|
|
|
{ attachmentId, uploadedUrl },
|
|
|
|
|
"Attachment marked as uploaded",
|
|
|
|
|
);
|
2026-05-13 19:34:14 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
attachmentId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to update attachment as uploaded",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function updateAttachmentAsFailedUpload(
|
2026-05-13 19:34:14 +07:00
|
|
|
attachmentId: string,
|
|
|
|
|
error: string,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<void> {
|
2026-05-13 19:34:14 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db
|
|
|
|
|
.update(attachmentsTable)
|
|
|
|
|
.set({
|
|
|
|
|
upload_status: "failed",
|
|
|
|
|
upload_error: error,
|
|
|
|
|
})
|
|
|
|
|
.where(eq(attachmentsTable.id, attachmentId));
|
2026-05-13 19:34:14 +07:00
|
|
|
|
|
|
|
|
logger.debug({ attachmentId, error }, "Attachment marked as failed upload");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
attachmentId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-13 19:34:14 +07:00
|
|
|
"Failed to update attachment as failed",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-14 02:31:16 +07:00
|
|
|
|
|
|
|
|
interface AIAnalysisUpdate {
|
feat: add warn category for minor rule violations
- Add "warn" status between "clean" and "flagged" for minor violations
- Update AI analyzer system prompt with community rules and warn category
- Warn: profanity, OOT, tone issues - requires warning but not deletion
- Flagged: NSFW, illegal, hacking, scam, harassment, violence, SARA - requires review/deletion
- Update types to support warn status in MessageRecord and AIAnalysisUpdate
- Update client UI to show three panels: All Messages, Warned, Flagged
- Warned messages show in right-top panel for quick review
- Flagged messages show in right-bottom panel for moderation action
This resolves:
- Need to distinguish between minor and severe violations
- Moderators can now warn users before taking action
- Better moderation workflow with three-tier system
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-14 04:23:11 +07:00
|
|
|
status: "pending" | "clean" | "warn" | "flagged" | "error";
|
2026-05-14 02:31:16 +07:00
|
|
|
flags?: string | null;
|
|
|
|
|
score?: number | null;
|
|
|
|
|
raw?: string | null;
|
|
|
|
|
analysis?: string | null;
|
|
|
|
|
analyzedAt?: number | null;
|
|
|
|
|
error?: string | null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function updateMessageAIAnalysis(
|
2026-05-14 02:31:16 +07:00
|
|
|
messageId: string,
|
|
|
|
|
result: AIAnalysisUpdate,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<MessageRecord | null> {
|
2026-05-14 02:31:16 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
await db
|
|
|
|
|
.update(messagesTable)
|
|
|
|
|
.set({
|
|
|
|
|
ai_status: result.status,
|
|
|
|
|
ai_moderation_flags: result.flags ?? null,
|
|
|
|
|
ai_moderation_score: result.score ?? null,
|
|
|
|
|
ai_moderation_raw: result.raw ?? null,
|
|
|
|
|
ai_analysis: result.analysis ?? null,
|
|
|
|
|
ai_analyzed_at: result.analyzedAt ?? Date.now(),
|
|
|
|
|
ai_error: result.error ?? null,
|
|
|
|
|
})
|
|
|
|
|
.where(eq(messagesTable.id, messageId));
|
2026-05-14 02:31:16 +07:00
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(messagesTable)
|
|
|
|
|
.where(eq(messagesTable.id, messageId));
|
2026-05-14 02:31:16 +07:00
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
return (rows[0] as MessageRecord) ?? null;
|
2026-05-14 02:31:16 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
messageId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
2026-05-14 02:31:16 +07:00
|
|
|
"Failed to update message AI analysis",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function getPendingAIAnalysisMessages(
|
2026-05-14 02:31:16 +07:00
|
|
|
limit: number = 25,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<MessageRecord[]> {
|
2026-05-14 02:31:16 +07:00
|
|
|
try {
|
2026-05-14 15:41:11 +07:00
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(messagesTable)
|
|
|
|
|
.where(
|
|
|
|
|
and(
|
|
|
|
|
eq(messagesTable.ai_status, "pending"),
|
|
|
|
|
isNull(messagesTable.deleted_at),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.orderBy(asc(messagesTable.created_at))
|
|
|
|
|
.limit(limit);
|
|
|
|
|
|
|
|
|
|
return rows as MessageRecord[];
|
2026-05-14 02:31:16 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{ error: error instanceof Error ? error.message : String(error) },
|
|
|
|
|
"Failed to get pending AI analysis messages",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 15:41:11 +07:00
|
|
|
export async function getMessageById(
|
2026-05-14 15:02:23 +07:00
|
|
|
messageId: string,
|
2026-05-14 15:41:11 +07:00
|
|
|
): Promise<MessageRecord | null> {
|
|
|
|
|
try {
|
|
|
|
|
const db = getDatabase() as any;
|
|
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
|
|
|
|
.from(messagesTable)
|
|
|
|
|
.where(eq(messagesTable.id, messageId));
|
|
|
|
|
|
|
|
|
|
return (rows[0] as MessageRecord) ?? null;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(
|
|
|
|
|
{
|
|
|
|
|
messageId,
|
|
|
|
|
error: error instanceof Error ? error.message : String(error),
|
|
|
|
|
},
|
|
|
|
|
"Failed to get message by id",
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-05-14 02:31:16 +07:00
|
|
|
}
|