Add a better admin:event script

This commit is contained in:
Alex Gleason 2024-04-23 17:32:24 -05:00
parent 3204b61f0b
commit b73b964fda
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 37 additions and 28 deletions

View File

@ -8,19 +8,23 @@
"test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A", "test": "DATABASE_URL=\"sqlite://:memory:\" deno test -A",
"check": "deno check src/server.ts", "check": "deno check src/server.ts",
"relays:sync": "deno run -A scripts/relays.ts sync", "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"
}, },
"unstable": ["ffi", "kv"], "unstable": ["ffi", "kv"],
"exclude": ["./public"], "exclude": ["./public"],
"imports": { "imports": {
"@/": "./src/", "@/": "./src/",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.12.1", "@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": "https://deno.land/x/hono@v3.10.1/mod.ts",
"hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts", "hono/middleware": "https://deno.land/x/hono@v3.10.1/middleware.ts",
"kysely": "npm:kysely@^0.26.3", "kysely": "npm:kysely@^0.26.3",
"kysely_deno_postgres": "https://deno.land/x/kysely_deno_postgres@v0.4.0/mod.ts", "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": { "lint": {
"include": ["src/", "scripts/"], "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);

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));
}