ditto/scripts/admin-role.ts

63 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2024-04-23 23:13:18 +00:00
import { NSchema } from '@nostrify/nostrify';
2024-05-14 23:51:35 +00:00
import { DittoDB } from '@/db/DittoDB.ts';
2024-04-23 23:13:18 +00:00
import { AdminSigner } from '@/signers/AdminSigner.ts';
2024-05-15 13:18:31 +00:00
import { EventsDB } from '@/storages/EventsDB.ts';
2024-04-23 23:13:18 +00:00
import { nostrNow } from '@/utils.ts';
2024-05-14 23:51:35 +00:00
const kysely = await DittoDB.getInstance();
const eventsDB = new EventsDB(kysely);
2024-04-23 23:13:18 +00:00
const [pubkey, role] = Deno.args;
if (!NSchema.id().safeParse(pubkey).success) {
console.error('Invalid pubkey');
Deno.exit(1);
}
if (!['admin', 'user'].includes(role)) {
console.error('Invalid role');
Deno.exit(1);
}
2024-06-08 16:13:15 +00:00
const signer = new AdminSigner();
const admin = await signer.getPublicKey();
const [existing] = await eventsDB.query([{
kinds: [30382],
authors: [admin],
'#d': [pubkey],
limit: 1,
}]);
const prevTags = (existing?.tags ?? []).filter(([name, value]) => {
if (name === 'd') {
return false;
}
if (name === 'n' && value === 'admin') {
return false;
}
return true;
});
const tags: string[][] = [
['d', pubkey],
];
if (role === 'admin') {
tags.push(['n', 'admin']);
}
tags.push(...prevTags);
const event = await signer.signEvent({
kind: 30382,
tags,
2024-04-23 23:13:18 +00:00
content: '',
created_at: nostrNow(),
});
await eventsDB.event(event);
Deno.exit(0);