diff --git a/prisma/migrations/20240219080503_add_name_disabled/migration.sql b/prisma/migrations/20240219080503_add_name_disabled/migration.sql new file mode 100644 index 0000000..892c49b --- /dev/null +++ b/prisma/migrations/20240219080503_add_name_disabled/migration.sql @@ -0,0 +1,16 @@ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Names" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" TEXT NOT NULL, + "npub" TEXT NOT NULL, + "timestamp" BIGINT NOT NULL, + "disabled" INTEGER NOT NULL DEFAULT 0 +); +INSERT INTO "new_Names" ("id", "name", "npub", "timestamp") SELECT "id", "name", "npub", "timestamp" FROM "Names"; +DROP TABLE "Names"; +ALTER TABLE "new_Names" RENAME TO "Names"; +CREATE UNIQUE INDEX "Names_name_key" ON "Names"("name"); +CREATE INDEX "Names_npub_idx" ON "Names"("npub"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ce3ef32..e9d7b89 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -30,6 +30,7 @@ model Names { name String @unique npub String timestamp BigInt + disabled Int @default(0) @@index([npub]) } diff --git a/src/index.js b/src/index.js index 36b650e..9c6da8e 100644 --- a/src/index.js +++ b/src/index.js @@ -801,8 +801,24 @@ app.post(NAME_PATH, async (req, res) => { const alreadyAssigned = names.find((n) => n.name === name); if (alreadyAssigned) { console.log("Name", name, "already assigned to", npub); + + const status = alreadyAssigned.disabled ? 201 : 200; + if (alreadyAssigned.disabled) { + const dbr = await prisma.names.update({ + where: { + npub, + name + }, + data: { + timestamp: Date.now(), + disabled: 0 + }, + }); + console.log("Name", name, "activated for", npub, { dbr }); + } + // reply ok - res.status(200).send({ + res.status(status).send({ ok: true, }); return; @@ -855,7 +871,7 @@ app.get(NAME_PATH, async (req, res) => { console.log("npub", npub, recs); const data = { - names: recs.map((r) => r.name), + names: recs.filter((r) => !r.disabled).map((r) => r.name), }; res.status(200).send(data); @@ -874,7 +890,7 @@ app.delete(NAME_PATH, async (req, res) => { if (!(await verifyAuthNostr(req, npub, NAME_PATH))) { console.log("auth failed", npub); res.status(403).send({ - error: `Bad auth` + error: `Bad auth`, }); return; } @@ -923,7 +939,7 @@ app.put(NAME_PATH, async (req, res) => { if (!(await verifyAuthNostr(req, npub, NAME_PATH))) { console.log("auth failed", npub); res.status(403).send({ - error: `Bad auth` + error: `Bad auth`, }); return; } @@ -950,6 +966,7 @@ app.put(NAME_PATH, async (req, res) => { npub: newNpub, name, timestamp: Date.now(), + disabled: 1, }, }); console.log({ dbr }); @@ -964,7 +981,6 @@ app.put(NAME_PATH, async (req, res) => { res.status(201).send({ ok: true, }); - } catch (e) { console.log(new Date(), "error req from ", req.ip, e.toString()); res.status(500).send({ @@ -973,7 +989,6 @@ app.put(NAME_PATH, async (req, res) => { } }); - const JSON_PATH = "/.well-known/nostr.json"; app.get(JSON_PATH, async (req, res) => { try { @@ -1001,7 +1016,7 @@ app.get(JSON_PATH, async (req, res) => { }); console.log("name", name, rec); - if (rec) { + if (rec && !rec.disabled) { const { data: pubkey } = nip19.decode(rec.npub); data.names[rec.name] = pubkey; data.nip46[pubkey] = [BUNKER_RELAY];