Merge branch 'admin-cli' into 'main'

Add admin:event and admin:role CLI tasks

See merge request soapbox-pub/ditto!179
This commit is contained in:
Alex Gleason 2024-04-24 01:19:36 +00:00
commit b2ef87c5b5
4 changed files with 75 additions and 28 deletions

View File

@ -8,19 +8,24 @@
"test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A",
"check": "deno check src/server.ts",
"relays:sync": "deno run -A scripts/relays.ts sync",
"nsec": "deno run scripts/nsec.ts"
"nsec": "deno run scripts/nsec.ts",
"admin:event": "deno run -A scripts/admin-event.ts",
"admin:role": "deno run -A scripts/admin-role.ts"
},
"unstable": ["ffi", "kv"],
"exclude": ["./public"],
"imports": {
"@/": "./src/",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.12.1",
"~/fixtures/": "./fixtures/",
"@std/cli": "jsr:@std/cli@^0.223.0",
"@std/json": "jsr:@std/json@^0.223.0",
"@std/streams": "jsr:@std/streams@^0.223.0",
"hono": "https://deno.land/x/hono@v3.10.1/mod.ts",
"hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts",
"kysely": "npm:kysely@^0.26.3",
"kysely_deno_postgres": "https://deno.land/x/kysely_deno_postgres@v0.4.0/mod.ts",
"zod": "npm:zod@^3.23.4"
"zod": "npm:zod@^3.23.4",
"~/fixtures/": "./fixtures/"
},
"lint": {
"include": ["src/", "scripts/"],

30
scripts/admin-event.ts Normal file
View File

@ -0,0 +1,30 @@
import { JsonParseStream } from '@std/json/json-parse-stream';
import { TextLineStream } from '@std/streams/text-line-stream';
import { db } from '@/db.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { EventsDB } from '@/storages/events-db.ts';
import { type EventStub } from '@/utils/api.ts';
import { nostrNow } from '@/utils.ts';
const signer = new AdminSigner();
const eventsDB = new EventsDB(db);
const readable = Deno.stdin.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new JsonParseStream());
for await (const t of readable) {
const event = await signer.signEvent({
content: '',
created_at: nostrNow(),
tags: [],
...t as EventStub,
});
await eventsDB.event(event);
}
Deno.exit(0);

37
scripts/admin-role.ts Normal file
View File

@ -0,0 +1,37 @@
import { NSchema } from '@nostrify/nostrify';
import { db } from '@/db.ts';
import { Conf } from '@/config.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { EventsDB } from '@/storages/events-db.ts';
import { nostrNow } from '@/utils.ts';
const eventsDB = new EventsDB(db);
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);
}
const event = await new AdminSigner().signEvent({
kind: 30361,
tags: [
['d', pubkey],
['role', role],
// NIP-31: https://github.com/nostr-protocol/nips/blob/master/31.md
['alt', `User's account was updated by the admins of ${Conf.url.host}`],
],
content: '',
created_at: nostrNow(),
});
await eventsDB.event(event);
Deno.exit(0);

View File

@ -1,25 +0,0 @@
import * as pipeline from '@/pipeline.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts';
import { type EventStub } from '@/utils/api.ts';
import { nostrNow } from '@/utils.ts';
switch (Deno.args[0]) {
case 'publish':
await publish(JSON.parse(Deno.args[1]));
break;
default:
console.log('Usage: deno run -A scripts/admin.ts <command>');
}
async function publish(t: EventStub) {
const signer = new AdminSigner();
const event = await signer.signEvent({
content: '',
created_at: nostrNow(),
tags: [],
...t,
});
await pipeline.handleEvent(event, AbortSignal.timeout(5000));
}