feat: client tag

feat: attach relays to link for event
This commit is contained in:
kieran 2024-11-22 16:30:09 +00:00
parent edc59fbf79
commit d16df8086a
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
3 changed files with 19 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import "@szhsin/react-menu/dist/index.css";
import "@/assets/fonts/inter.css"; import "@/assets/fonts/inter.css";
import { unixNow, unixNowMs } from "@snort/shared"; import { unixNow, unixNowMs } from "@snort/shared";
import { EventBuilder } from "@snort/system";
import { SnortContext } from "@snort/system-react"; import { SnortContext } from "@snort/system-react";
import { StrictMode } from "react"; import { StrictMode } from "react";
import * as ReactDOM from "react-dom/client"; import * as ReactDOM from "react-dom/client";
@ -47,6 +48,7 @@ import { Day } from "./Utils/Const";
import { LoginStore } from "./Utils/Login"; import { LoginStore } from "./Utils/Login";
async function initSite() { async function initSite() {
EventBuilder.ClientTag = ["snort", __SNORT_VERSION__];
storeRefCode(); storeRefCode();
if (hasWasm) { if (hasWasm) {
await wasmInit(WasmPath); await wasmInit(WasmPath);

View File

@ -5,6 +5,11 @@ import { EventExt } from "./event-ext";
import { NostrLink, tryParseNostrLink } from "./nostr-link"; import { NostrLink, tryParseNostrLink } from "./nostr-link";
export class EventBuilder { export class EventBuilder {
/**
* Client tag to attach to all events
*/
static ClientTag: Array<string> | undefined = ["client", "snort_system"];
#kind?: EventKind; #kind?: EventKind;
#content?: string; #content?: string;
#createdAt?: number; #createdAt?: number;
@ -101,6 +106,9 @@ export class EventBuilder {
created_at: (this.#createdAt ?? unixNow()) - (this.#jitter ? Math.floor(jitter(this.#jitter)) : 0), created_at: (this.#createdAt ?? unixNow()) - (this.#jitter ? Math.floor(jitter(this.#jitter)) : 0),
tags: this.#tags.sort((a, b) => a[0].localeCompare(b[0])), tags: this.#tags.sort((a, b) => a[0].localeCompare(b[0])),
} as NostrEvent; } as NostrEvent;
if (EventBuilder.ClientTag && EventBuilder.ClientTag[0] === "client" && EventBuilder.ClientTag.length > 1) {
ev.tags.push(EventBuilder.ClientTag);
}
ev.id = EventExt.createId(ev); ev.id = EventExt.createId(ev);
return ev; return ev;
} }

View File

@ -1,4 +1,4 @@
import { bech32ToHex, hexToBech32, isHex, removeUndefined, unwrap, Bech32Regex } from "@snort/shared"; import { bech32ToHex, hexToBech32, isHex, removeUndefined, unwrap, Bech32Regex, sanitizeRelayUrl, appendDedupe } from "@snort/shared";
import { decodeTLV, encodeTLV, EventExt, EventKind, NostrEvent, NostrPrefix, TaggedNostrEvent, TLVEntryType } from "."; import { decodeTLV, encodeTLV, EventExt, EventKind, NostrEvent, NostrPrefix, TaggedNostrEvent, TLVEntryType } from ".";
import { findTag } from "./utils"; import { findTag } from "./utils";
@ -11,7 +11,7 @@ export interface ToNostrEventTag {
} }
export class NostrHashtagLink implements ToNostrEventTag { export class NostrHashtagLink implements ToNostrEventTag {
constructor(readonly tag: string) {} constructor(readonly tag: string) { }
equals(other: ToNostrEventTag): boolean { equals(other: ToNostrEventTag): boolean {
const otherTag = other.toEventTag(); const otherTag = other.toEventTag();
@ -24,7 +24,7 @@ export class NostrHashtagLink implements ToNostrEventTag {
} }
export class UnknownTag implements ToNostrEventTag { export class UnknownTag implements ToNostrEventTag {
constructor(readonly value: Array<string>) {} constructor(readonly value: Array<string>) { }
equals(other: ToNostrEventTag): boolean { equals(other: ToNostrEventTag): boolean {
const otherTag = other.toEventTag(); const otherTag = other.toEventTag();
@ -226,8 +226,13 @@ export class NostrLink implements ToNostrEventTag {
); );
} }
/**
* Create an event link from an existing nostr event
*/
static fromEvent(ev: TaggedNostrEvent | NostrEvent) { static fromEvent(ev: TaggedNostrEvent | NostrEvent) {
const relays = "relays" in ev ? ev.relays : undefined; let relays = "relays" in ev ? ev.relays : undefined;
const eventRelays = removeUndefined(ev.tags.filter(a => a[0] === "relays" || a[0] === "relay" || a[0] === "r").map(a => sanitizeRelayUrl(a[1])));
relays = appendDedupe(relays, eventRelays);
if (ev.kind >= 30_000 && ev.kind < 40_000) { if (ev.kind >= 30_000 && ev.kind < 40_000) {
const dTag = unwrap(findTag(ev, "d")); const dTag = unwrap(findTag(ev, "d"));