bug: dont add duplicate tags

This commit is contained in:
Kieran 2023-04-17 10:14:25 +01:00
parent 445fedcb43
commit c294f5f0bd
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 24 additions and 8 deletions

View File

@ -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<T>(path, method, body, {
...headers,

View File

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

View File

@ -30,7 +30,9 @@ export class EventBuilder {
return this;
}
tag(t: Array<string>) {
tag(t: Array<string>): 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;
}