import type { RowDataPacket } from 'mysql2' import { db, DB } from './db' import type { SessionData } from './session' /** Nivel GM mínimo para moderar el foro (equivale a FORUM_MOD_GMLEVEL en Django). */ const MOD_GMLEVEL = Number(process.env.FORUM_MOD_GMLEVEL || 2) /** Nivel GM máximo de una cuenta (acore_auth.account_access). 0 si no tiene. */ export async function getGmlevel(accountId: number | undefined): Promise { if (!accountId) return 0 try { const [rows] = await db(DB.auth).query( 'SELECT MAX(SecurityLevel) AS lvl FROM account_access WHERE AccountID = ?', [accountId], ) return rows[0] && rows[0].lvl != null ? Number(rows[0].lvl) : 0 } catch { return 0 } } /** ¿El usuario puede moderar el foro? */ export async function forumIsModerator(session: SessionData): Promise { if (!session.accountId) return false return (await getGmlevel(session.accountId)) >= MOD_GMLEVEL } /** Puede editar/borrar un post: su autor o un moderador. */ export function canEditPost( session: SessionData, isMod: boolean, posterId: number | null | undefined, ): boolean { if (!session.accountId) return false if (posterId != null && Number(posterId) === Number(session.accountId)) return true return isMod }