mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
65 lines
1.4 KiB
TypeScript
65 lines
1.4 KiB
TypeScript
import { DittoConf } from '@ditto/conf';
|
|
import { NSchema } from '@nostrify/nostrify';
|
|
import { nip19 } from 'nostr-tools';
|
|
|
|
import { AdminSigner } from '../packages/signers/AdminSigner.ts';
|
|
import { DittoStorages } from '../packages/ditto/DittoStorages.ts';
|
|
import { nostrNow } from '../packages/ditto/utils.ts';
|
|
|
|
const conf = new DittoConf(Deno.env);
|
|
const storages = new DittoStorages(conf);
|
|
const store = await storages.db();
|
|
|
|
const [pubkeyOrNpub, role] = Deno.args;
|
|
const pubkey = pubkeyOrNpub.startsWith('npub1') ? nip19.decode(pubkeyOrNpub as `npub1${string}`).data : pubkeyOrNpub;
|
|
|
|
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);
|
|
}
|
|
|
|
const signer = new AdminSigner(conf);
|
|
const admin = await signer.getPublicKey();
|
|
|
|
const [existing] = await store.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,
|
|
content: '',
|
|
created_at: nostrNow(),
|
|
});
|
|
|
|
await store.event(event);
|
|
|
|
Deno.exit(0);
|