web/website/node/organisation-motd.mjs
2026-11-21 20:22:37 +01:00

16 lines
1.6 KiB
JavaScript

// Shared storage for organisation welcome messages. Domain routes own permissions.
export const ORGANISATION_MOTD_SCHEMA='CREATE TABLE IF NOT EXISTS organisation_motd (organisation_kind VARCHAR(16) NOT NULL,organisation_id CHAR(36) NOT NULL,message TEXT NOT NULL,updated_by CHAR(36) NOT NULL,updated_at BIGINT NOT NULL,PRIMARY KEY(organisation_kind,organisation_id))';
const kind=value=>{if(!['clan','company','politics'].includes(value))throw new TypeError('Unknown organisation type');return value;};
export function validateMotd(value){
if(typeof value!=='string')throw new TypeError('Enter a message of up to 1,000 characters.');
const text=value.replaceAll('\r\n','\n').trim();
if(text.length>1000||/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text))throw new TypeError('Enter a message of up to 1,000 characters without control characters.');
return text;
}
export async function readOrganisationMotd(db,organisationKind,organisationId){
const row=await db.prepare('SELECT message FROM organisation_motd WHERE organisation_kind=? AND organisation_id=?').bind(kind(organisationKind),organisationId).first();
return row?.message||'';
}
export function writeOrganisationMotd(db,{organisationKind,organisationId,message,actorId,now}){
return db.prepare('INSERT INTO organisation_motd(organisation_kind,organisation_id,message,updated_by,updated_at) VALUES(?,?,?,?,?) ON CONFLICT(organisation_kind,organisation_id) DO UPDATE SET message=excluded.message,updated_by=excluded.updated_by,updated_at=excluded.updated_at').bind(kind(organisationKind),organisationId,validateMotd(message),actorId,now);
}