From b73b964fda3daa34a12c3b7cd4d21e69ce0cb398 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 23 Apr 2024 17:32:24 -0500 Subject: [PATCH] Add a better admin:event script --- deno.json | 10 +++++++--- scripts/admin-event.ts | 30 ++++++++++++++++++++++++++++++ scripts/admin.ts | 25 ------------------------- 3 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 scripts/admin-event.ts delete mode 100644 scripts/admin.ts diff --git a/deno.json b/deno.json index 26cf366..b806a74 100644 --- a/deno.json +++ b/deno.json @@ -8,19 +8,23 @@ "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" }, "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/"], diff --git a/scripts/admin-event.ts b/scripts/admin-event.ts new file mode 100644 index 0000000..a9939ad --- /dev/null +++ b/scripts/admin-event.ts @@ -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); diff --git a/scripts/admin.ts b/scripts/admin.ts deleted file mode 100644 index a9c08f1..0000000 --- a/scripts/admin.ts +++ /dev/null @@ -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 '); -} - -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)); -}