From c294f5f0bd060bcfa8b3711b08de9f5643441148 Mon Sep 17 00:00:00 2001 From: Kieran Date: Mon, 17 Apr 2023 10:14:25 +0100 Subject: [PATCH] bug: dont add duplicate tags --- packages/app/src/SnortApi.ts | 11 ++++------- packages/app/src/System/EventBuilder.test.ts | 17 +++++++++++++++++ packages/app/src/System/EventBuilder.ts | 4 +++- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 packages/app/src/System/EventBuilder.test.ts diff --git a/packages/app/src/SnortApi.ts b/packages/app/src/SnortApi.ts index 88c6aa64..63917cce 100644 --- a/packages/app/src/SnortApi.ts +++ b/packages/app/src/SnortApi.ts @@ -63,14 +63,11 @@ export default class SnortApi { throw new Error("Publisher not set"); } const auth = await this.#publisher.generic(eb => { - eb.kind(EventKind.HttpAuthentication); - eb.tag(["url", `${this.#url}${path}`]); - eb.tag(["method", method ?? "GET"]); - return eb; + return eb + .kind(EventKind.HttpAuthentication) + .tag(["url", `${this.#url}${path}`]) + .tag(["method", method ?? "GET"]); }); - if (!auth) { - throw new Error("Failed to create auth event"); - } return this.#getJson(path, method, body, { ...headers, diff --git a/packages/app/src/System/EventBuilder.test.ts b/packages/app/src/System/EventBuilder.test.ts new file mode 100644 index 00000000..7e217d5e --- /dev/null +++ b/packages/app/src/System/EventBuilder.test.ts @@ -0,0 +1,17 @@ +import { EventKind } from "@snort/nostr"; +import { EventBuilder } from "./EventBuilder"; + +const PubKey = "test-key"; + +describe("EventBuilder", () => { + it("should not add duplicate tags", () => { + const eb = new EventBuilder(); + eb.pubKey(PubKey); + eb.kind(EventKind.TextNote); + + eb.tag(["p", PubKey]); + eb.tag(["p", PubKey]); + const out = eb.build(); + expect(out.tags.length).toBe(1); + }); +}); diff --git a/packages/app/src/System/EventBuilder.ts b/packages/app/src/System/EventBuilder.ts index d70d15e2..f202685e 100644 --- a/packages/app/src/System/EventBuilder.ts +++ b/packages/app/src/System/EventBuilder.ts @@ -30,7 +30,9 @@ export class EventBuilder { return this; } - tag(t: Array) { + tag(t: Array): EventBuilder { + const duplicate = this.#tags.some(a => a.length === t.length && a.every((b, i) => b !== a[i])); + if (duplicate) return this; this.#tags.push(t); return this; }