2026-06-02 00:11:29 +07:00
|
|
|
import { sql } from "drizzle-orm";
|
|
|
|
|
import { config } from "../../shared/config/index.js";
|
2026-06-02 12:17:51 +07:00
|
|
|
import { getDatabase } from "../../shared/database/index.js";
|
2026-06-02 21:06:42 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-06-02 00:11:29 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("analysis.service");
|
|
|
|
|
|
|
|
|
|
export interface AnalysisSearchQuery {
|
|
|
|
|
q?: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
limit?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 12:17:51 +07:00
|
|
|
/** Full message columns for search results — matches MessageRecord from client.ts */
|
|
|
|
|
const FULL_COLUMNS = sql.raw(`
|
|
|
|
|
id, guild_id, channel_id, thread_id,
|
|
|
|
|
user_id, username, avatar_url,
|
|
|
|
|
content, edited_content, created_at, edited_at, deleted_at,
|
|
|
|
|
type, metadata,
|
|
|
|
|
ai_status, ai_moderation_flags, ai_moderation_score,
|
|
|
|
|
ai_analysis, ai_categories, ai_severity, ai_confidence,
|
|
|
|
|
ai_recommended_action, ai_analyzed_at, ai_error
|
|
|
|
|
`);
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
export class AnalysisService {
|
|
|
|
|
async search(query: AnalysisSearchQuery) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const { q = "", channelId, limit = 20 } = query;
|
|
|
|
|
const guildId = config.MONITOR_GUILD_ID;
|
|
|
|
|
|
|
|
|
|
logger.debug({ q, channelId, limit, guildId }, "Searching analysis");
|
|
|
|
|
|
|
|
|
|
const searchPattern = `%${q}%`;
|
|
|
|
|
const limitVal = limit;
|
|
|
|
|
|
|
|
|
|
let sqlQuery;
|
|
|
|
|
if (channelId && guildId) {
|
|
|
|
|
sqlQuery = sql`
|
2026-06-02 12:17:51 +07:00
|
|
|
SELECT ${FULL_COLUMNS}
|
2026-06-02 00:11:29 +07:00
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ${guildId}
|
|
|
|
|
AND channel_id = ${channelId}
|
|
|
|
|
AND content ILIKE ${searchPattern}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT ${limitVal}
|
|
|
|
|
`;
|
|
|
|
|
} else if (channelId) {
|
|
|
|
|
sqlQuery = sql`
|
2026-06-02 12:17:51 +07:00
|
|
|
SELECT ${FULL_COLUMNS}
|
2026-06-02 00:11:29 +07:00
|
|
|
FROM messages
|
|
|
|
|
WHERE channel_id = ${channelId}
|
|
|
|
|
AND content ILIKE ${searchPattern}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT ${limitVal}
|
|
|
|
|
`;
|
|
|
|
|
} else if (guildId) {
|
|
|
|
|
sqlQuery = sql`
|
2026-06-02 12:17:51 +07:00
|
|
|
SELECT ${FULL_COLUMNS}
|
2026-06-02 00:11:29 +07:00
|
|
|
FROM messages
|
|
|
|
|
WHERE guild_id = ${guildId}
|
|
|
|
|
AND content ILIKE ${searchPattern}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT ${limitVal}
|
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
sqlQuery = sql`
|
2026-06-02 12:17:51 +07:00
|
|
|
SELECT ${FULL_COLUMNS}
|
2026-06-02 00:11:29 +07:00
|
|
|
FROM messages
|
|
|
|
|
WHERE content ILIKE ${searchPattern}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT ${limitVal}
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { rows } = await db.execute(sqlQuery);
|
|
|
|
|
return { results: rows };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const analysisService = new AnalysisService();
|