diff --git a/packages/app/src/Element/Thread.tsx b/packages/app/src/Element/Thread.tsx index 143003ea..4a250eae 100644 --- a/packages/app/src/Element/Thread.tsx +++ b/packages/app/src/Element/Thread.tsx @@ -1,10 +1,10 @@ import "./Thread.css"; import { useMemo, useState, ReactNode, useContext } from "react"; import { useIntl } from "react-intl"; -import { useNavigate, Link, useParams } from "react-router-dom"; +import { useNavigate, useParams } from "react-router-dom"; import { TaggedNostrEvent, u256, NostrPrefix, EventExt, parseNostrLink } from "@snort/system"; -import { eventLink, getReactions, getAllReactions } from "SnortUtils"; +import { getReactions, getAllReactions } from "SnortUtils"; import BackButton from "Element/BackButton"; import Note from "Element/Note"; import NoteGhost from "Element/NoteGhost"; @@ -154,9 +154,8 @@ const TierThree = ({ active, isLastSubthread, notes, related, chains, onNavigate return ( <>
+ className={`subthread-container ${hasMultipleNotes ? "subthread-multi" : ""} ${isLast ? "subthread-last" : "subthread-mid" + }`}> + className={`subthread-container ${lastReply ? "" : "subthread-multi"} ${lastReply ? "subthread-last" : "subthread-mid" + }`}> void }) { function navigateThread(e: TaggedNostrEvent) { thread.setCurrent(e.id); - //const link = encodeTLV(e.id, NostrPrefix.Event, e.relays); + //router.navigate(`/e/${NostrLink.fromEvent(e).encode()}`, { replace: true }) } const parent = useMemo(() => { @@ -239,8 +237,6 @@ export function Thread(props: { onBack?: () => void }) { } }, [thread.root]); - const brokenChains = Array.from(thread.chains?.keys()).filter(a => !thread.data?.some(b => b.id === a)); - function renderRoot(note: TaggedNostrEvent) { const className = `thread-root${isSingleNote ? " thread-root-single" : ""}`; if (note) { @@ -307,18 +303,6 @@ export function Thread(props: { onBack?: () => void }) {
{thread.root && renderRoot(thread.root)} {thread.root && renderChain(thread.root.id)} - - {brokenChains.length > 0 &&

Other replies

} - {brokenChains.map(a => { - return ( -
- - Missing event {a.substring(0, 8)} - - {renderChain(a)} -
- ); - })}
); diff --git a/packages/app/src/Feed/EventFeed.ts b/packages/app/src/Feed/EventFeed.ts index f96944df..3f8fce44 100644 --- a/packages/app/src/Feed/EventFeed.ts +++ b/packages/app/src/Feed/EventFeed.ts @@ -1,29 +1,11 @@ import { useMemo } from "react"; -import { NostrPrefix, RequestBuilder, ReplaceableNoteStore, NostrLink, NoteCollection } from "@snort/system"; +import { RequestBuilder, ReplaceableNoteStore, NostrLink, NoteCollection } from "@snort/system"; import { useRequestBuilder } from "@snort/system-react"; -import { unwrap } from "SnortUtils"; - export function useEventFeed(link: NostrLink) { const sub = useMemo(() => { const b = new RequestBuilder(`event:${link.id.slice(0, 12)}`); - if (link.type === NostrPrefix.Address) { - const f = b.withFilter().tag("d", [link.id]); - if (link.author) { - f.authors([unwrap(link.author)]); - } - if (link.kind) { - f.kinds([unwrap(link.kind)]); - } - } else { - const f = b.withFilter().ids([link.id]); - if (link.relays) { - link.relays.slice(0, 2).forEach(r => f.relay(r)); - } - if (link.author) { - f.authors([link.author]); - } - } + b.withFilter().link(link); return b; }, [link]); @@ -33,25 +15,7 @@ export function useEventFeed(link: NostrLink) { export function useEventsFeed(id: string, links: Array) { const sub = useMemo(() => { const b = new RequestBuilder(`events:${id}`); - for (const l of links) { - if (l.type === NostrPrefix.Address) { - const f = b.withFilter().tag("d", [l.id]); - if (l.author) { - f.authors([unwrap(l.author)]); - } - if (l.kind) { - f.kinds([unwrap(l.kind)]); - } - } else { - const f = b.withFilter().ids([l.id]); - if (l.relays) { - l.relays.slice(0, 2).forEach(r => f.relay(r)); - } - if (l.author) { - f.authors([l.author]); - } - } - } + links.forEach(v => b.withFilter().link(v)); return b; }, [id, links]); diff --git a/packages/app/src/Feed/LiveChatFeed.tsx b/packages/app/src/Feed/LiveChatFeed.tsx deleted file mode 100644 index e4d16e82..00000000 --- a/packages/app/src/Feed/LiveChatFeed.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import { EventKind, FlatNoteStore, NostrLink, RequestBuilder } from "@snort/system"; -import { useRequestBuilder } from "@snort/system-react"; -import { useMemo } from "react"; - -export function useLiveChatFeed(link: NostrLink) { - const sub = useMemo(() => { - const rb = new RequestBuilder(`live:${link.id}:${link.author}`); - rb.withOptions({ - leaveOpen: true, - }); - rb.withFilter() - .kinds([EventKind.ZapReceipt, 1311 as EventKind]) - .tag("a", [`${link.kind}:${link.author}:${link.id}`]) - .limit(100); - return rb; - }, [link]); - - return useRequestBuilder(FlatNoteStore, sub); -} diff --git a/packages/app/src/Feed/ThreadFeed.ts b/packages/app/src/Feed/ThreadFeed.ts index cefde939..cee21fbd 100644 --- a/packages/app/src/Feed/ThreadFeed.ts +++ b/packages/app/src/Feed/ThreadFeed.ts @@ -2,25 +2,26 @@ import { useEffect, useMemo, useState } from "react"; import { EventKind, NostrLink, RequestBuilder, NoteCollection } from "@snort/system"; import { useRequestBuilder } from "@snort/system-react"; -import useLogin from "Hooks/useLogin"; import { useReactions } from "./FeedReactions"; export default function useThreadFeed(link: NostrLink) { const [allEvents, setAllEvents] = useState>([]); - const pref = useLogin().preferences; const sub = useMemo(() => { - const sub = new RequestBuilder(`thread:${link.id}`); + const sub = new RequestBuilder(`thread:${link.id.slice(0, 12)}`); sub.withOptions({ leaveOpen: true, }); - sub.withFilter().kinds([EventKind.TextNote]).link(link); - if (allEvents.length > 0) { - const f = sub.withFilter().kinds([EventKind.TextNote]); - allEvents.forEach(x => f.replyToLink(x)); - } + sub + .withFilter() + .kinds([EventKind.TextNote]) + .link(link) + .replyToLink(link); + allEvents.forEach(x => { + sub.withFilter().kinds([EventKind.TextNote]).link(x).replyToLink(x); + }); return sub; - }, [allEvents.length, pref]); + }, [allEvents.length]); const store = useRequestBuilder(NoteCollection, sub); @@ -37,7 +38,7 @@ export default function useThreadFeed(link: NostrLink) { } }, [store.data?.length]); - const reactions = useReactions(`thread:${link.id}:reactions`, allEvents); + const reactions = useReactions(`thread:${link.id.slice(0, 12)}:reactions`, allEvents); return { thread: store.data ?? [], diff --git a/packages/app/src/Feed/ZapsFeed.ts b/packages/app/src/Feed/ZapsFeed.ts index e82bc188..3026e385 100644 --- a/packages/app/src/Feed/ZapsFeed.ts +++ b/packages/app/src/Feed/ZapsFeed.ts @@ -1,5 +1,5 @@ import { useMemo } from "react"; -import { EventKind, RequestBuilder, parseZap, NostrLink, NostrPrefix, NoteCollection } from "@snort/system"; +import { EventKind, RequestBuilder, parseZap, NostrLink, NoteCollection } from "@snort/system"; import { useRequestBuilder } from "@snort/system-react"; import { UserCache } from "Cache"; @@ -7,11 +7,7 @@ export default function useZapsFeed(link?: NostrLink) { const sub = useMemo(() => { if (!link) return null; const b = new RequestBuilder(`zaps:${link.encode()}`); - if (link.type === NostrPrefix.PublicKey) { - b.withFilter().tag("p", [link.id]).kinds([EventKind.ZapReceipt]); - } else if (link.type === NostrPrefix.Event || link.type === NostrPrefix.Note) { - b.withFilter().tag("e", [link.id]).kinds([EventKind.ZapReceipt]); - } + b.withFilter().kinds([EventKind.ZapReceipt]).replyToLink(link); return b; }, [link]); diff --git a/packages/app/src/Hooks/useThreadContext.tsx b/packages/app/src/Hooks/useThreadContext.tsx index d719889a..15649b23 100644 --- a/packages/app/src/Hooks/useThreadContext.tsx +++ b/packages/app/src/Hooks/useThreadContext.tsx @@ -27,18 +27,20 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil ?.sort((a, b) => b.created_at - a.created_at) .forEach(v => { const t = EventExt.extractThread(v); - let replyTo = t?.replyTo?.value ?? t?.root?.value; - if (t?.root?.key === "a" && t?.root?.value) { - const parsed = t.root.value.split(":"); - replyTo = feed.thread?.find( - a => a.kind === Number(parsed[0]) && a.pubkey === parsed[1] && findTag(a, "d") === parsed[2], - )?.id; - } - if (replyTo) { - if (!chains.has(replyTo)) { - chains.set(replyTo, [v]); - } else { - unwrap(chains.get(replyTo)).push(v); + if (t) { + let replyTo = t.replyTo?.value ?? t.root?.value; + if (t.root?.key === "a" && t.root?.value) { + const parsed = t.root.value.split(":"); + replyTo = feed.thread?.find( + a => a.kind === Number(parsed[0]) && a.pubkey === parsed[1] && findTag(a, "d") === parsed[2], + )?.id; + } + if (replyTo) { + if (!chains.has(replyTo)) { + chains.set(replyTo, [v]); + } else { + unwrap(chains.get(replyTo)).push(v); + } } } }); @@ -91,7 +93,7 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil } } } - }, [feed.thread, currentId, location]); + }, [feed.thread.length, currentId, location]); const ctxValue = useMemo(() => { return { diff --git a/packages/system/src/event-ext.ts b/packages/system/src/event-ext.ts index 2e8d76d4..2795bf67 100644 --- a/packages/system/src/event-ext.ts +++ b/packages/system/src/event-ext.ts @@ -94,6 +94,7 @@ export abstract class EventExt { value: tag[1], } as Tag; switch (ret.key) { + case "a": case "e": { ret.relay = tag.length > 2 ? tag[2] : undefined; ret.marker = tag.length > 3 ? tag[3] : undefined; @@ -102,38 +103,38 @@ export abstract class EventExt { } return ret; } - static extractThread(ev: NostrEvent) { - const isThread = ev.tags.some(a => (a[0] === "e" && a[3] !== "mention") || a[0] == "a"); - if (!isThread) { - return undefined; - } + static extractThread(ev: NostrEvent) { const shouldWriteMarkers = ev.kind === EventKind.TextNote; const ret = { mentions: [], pubKeys: [], } as Thread; - const eTags = ev.tags.filter(a => a[0] === "e" || a[0] === "a").map(a => EventExt.parseTag(a)); - const marked = eTags.some(a => a.marker); - if (!marked) { - ret.root = eTags[0]; - ret.root.marker = shouldWriteMarkers ? "root" : undefined; - if (eTags.length > 1) { - ret.replyTo = eTags[eTags.length - 1]; - ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined; - } - if (eTags.length > 2) { - ret.mentions = eTags.slice(1, -1); - if (shouldWriteMarkers) { - ret.mentions.forEach(a => (a.marker = "mention")); + const replyTags = ev.tags.filter(a => a[0] === "e" || a[0] === "a").map(a => EventExt.parseTag(a)); + if(replyTags.length > 0) { + const marked = replyTags.some(a => a.marker); + if (!marked) { + ret.root = replyTags[0]; + ret.root.marker = shouldWriteMarkers ? "root" : undefined; + if (replyTags.length > 1) { + ret.replyTo = replyTags[replyTags.length - 1]; + ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined; } + if (replyTags.length > 2) { + ret.mentions = replyTags.slice(1, -1); + if (shouldWriteMarkers) { + ret.mentions.forEach(a => (a.marker = "mention")); + } + } + } else { + const root = replyTags.find(a => a.marker === "root"); + const reply = replyTags.find(a => a.marker === "reply"); + ret.root = root; + ret.replyTo = reply; + ret.mentions = replyTags.filter(a => a.marker === "mention"); } } else { - const root = eTags.find(a => a.marker === "root"); - const reply = eTags.find(a => a.marker === "reply"); - ret.root = root; - ret.replyTo = reply; - ret.mentions = eTags.filter(a => a.marker === "mention"); + return undefined; } ret.pubKeys = Array.from(new Set(ev.tags.filter(a => a[0] === "p").map(a => a[1]))); return ret; diff --git a/packages/system/src/nostr-system.ts b/packages/system/src/nostr-system.ts index e07674cc..1439b7b8 100644 --- a/packages/system/src/nostr-system.ts +++ b/packages/system/src/nostr-system.ts @@ -5,7 +5,7 @@ import { NostrEvent, TaggedNostrEvent } from "./nostr"; import { AuthHandler, Connection, RelaySettings, ConnectionStateSnapshot } from "./connection"; import { Query } from "./query"; import { NoteCollection, NoteStore, NoteStoreSnapshotData } from "./note-collection"; -import { BuiltRawReqFilter, RequestBuilder } from "./request-builder"; +import { BuiltRawReqFilter, RequestBuilder, RequestStrategy } from "./request-builder"; import { RelayMetricHandler } from "./relay-metric-handler"; import { MetadataCache, @@ -22,6 +22,7 @@ import { import { EventsCache } from "./cache/events"; import { RelayCache } from "./gossip-model"; import { QueryOptimizer, DefaultQueryOptimizer } from "./query-optimizer"; +import { trimFilters } from "./request-trim"; /** * Manages nostr content retrieval system @@ -295,20 +296,22 @@ export class NostrSystem extends ExternalStore implements System if (cacheResults.length > 0) { const resultIds = new Set(cacheResults.map(a => a.id)); f.ids = f.ids.filter(a => !resultIds.has(a)); - q.feed.add(cacheResults as Array); + q.insertCompletedTrace({ + filters:[{...f, ids: [...resultIds]}], + strategy: RequestStrategy.ExplicitRelays, + relay: "" + }, cacheResults as Array); } } } // check for empty filters - qSend.filters = qSend.filters.filter(a => - Object.values(a) - .filter(v => Array.isArray(v)) - .every(b => (b as Array).length > 0), - ); - if (qSend.filters.length === 0) { + const fNew = trimFilters(qSend.filters); + if (fNew.length === 0) { return; } + qSend.filters = fNew; + if (qSend.relay) { this.#log("Sending query to %s %O", qSend.relay, qSend); const s = this.#sockets.get(qSend.relay); diff --git a/packages/system/src/profile-cache.ts b/packages/system/src/profile-cache.ts index 9a350693..16b06af9 100644 --- a/packages/system/src/profile-cache.ts +++ b/packages/system/src/profile-cache.ts @@ -37,13 +37,11 @@ export class ProfileLoaderService { * Request profile metadata for a set of pubkeys */ TrackMetadata(pk: HexKey | Array) { - const bufferNow = []; for (const p of Array.isArray(pk) ? pk : [pk]) { - if (p.length === 64 && this.#wantsMetadata.add(p)) { - bufferNow.push(p); + if (p.length === 64) { + this.#wantsMetadata.add(p) } } - this.#cache.buffer(bufferNow); } /** diff --git a/packages/system/src/query.ts b/packages/system/src/query.ts index 613a0907..076febec 100644 --- a/packages/system/src/query.ts +++ b/packages/system/src/query.ts @@ -197,6 +197,28 @@ export class Query implements QueryBase { this.#stopCheckTraces(); } + /** + * Insert a new trace as a placeholder + */ + insertCompletedTrace(subq: BuiltRawReqFilter, data: Readonly>) { + const qt = new QueryTrace( + "", + subq.filters, + "", + () => { + // nothing to close + }, + () => { + // nothing to progress + }, + ); + qt.sentToRelay(); + qt.gotEose(); + this.#tracing.push(qt); + this.feed.add(data); + return qt; + } + sendToRelay(c: Connection, subq: BuiltRawReqFilter) { if (!this.#canSendQuery(c, subq)) { return; diff --git a/packages/system/src/request-builder.ts b/packages/system/src/request-builder.ts index 480ed115..d54219c0 100644 --- a/packages/system/src/request-builder.ts +++ b/packages/system/src/request-builder.ts @@ -113,7 +113,7 @@ export class RequestBuilder { const diff = system.QueryOptimizer.getDiff(prev, this.buildRaw()); const ts = unixNowMs() - start; - this.#log("buildDiff %s %d ms", this.id, ts); + this.#log("buildDiff %s %d ms +%d %O=>%O", this.id, ts, diff.length, prev, this.buildRaw()); if (diff.length > 0) { return splitFlatByWriteRelays(system.RelayCache, diff).map(a => { return { @@ -146,7 +146,7 @@ export class RequestBuilder { const filtersSquashed = [...relayMerged.values()].map(a => { return { - filters: system.QueryOptimizer.compress(a.flatMap(b => b.filters)), + filters: system.QueryOptimizer.flatMerge(a.flatMap(b => b.filters.flatMap(c => system.QueryOptimizer.expandFilter(c)))), relay: a[0].relay, strategy: a[0].strategy, } as BuiltRawReqFilter; @@ -234,12 +234,15 @@ export class RequestFilterBuilder { */ link(link: NostrLink) { if (link.type === NostrPrefix.Address) { - return this.tag("d", [link.id]) + this.tag("d", [link.id]) .kinds([unwrap(link.kind)]) .authors([unwrap(link.author)]); + link.relays?.forEach(v => this.relay(v)); } else { - return this.ids([link.id]); + this.ids([link.id]); + link.relays?.forEach(v => this.relay(v)); } + return this; } /** @@ -247,10 +250,16 @@ export class RequestFilterBuilder { */ replyToLink(link: NostrLink) { if (link.type === NostrPrefix.Address) { - return this.tag("a", [`${link.kind}:${link.author}:${link.id}`]); + this.tag("a", [`${link.kind}:${link.author}:${link.id}`]); + link.relays?.forEach(v => this.relay(v)); + } else if(link.type === NostrPrefix.PublicKey || link.type === NostrPrefix.Profile) { + this.tag("p", [link.id]); + link.relays?.forEach(v => this.relay(v)); } else { - return this.tag("e", [link.id]); + this.tag("e", [link.id]); + link.relays?.forEach(v => this.relay(v)); } + return this; } /** diff --git a/packages/system/src/request-trim.ts b/packages/system/src/request-trim.ts new file mode 100644 index 00000000..bc3da93d --- /dev/null +++ b/packages/system/src/request-trim.ts @@ -0,0 +1,26 @@ +import { ReqFilter } from "nostr"; + +/** + * Remove empty filters, filters which would result in no results + */ +export function trimFilters(filters: Array) { + const fNew = []; + for(const f of filters) { + let arrays = 0; + for(const [k, v] of Object.entries(f)) { + if(Array.isArray(v)) { + arrays++; + if(v.length === 0) { + delete f[k]; + } + } + } + + if(arrays > 0 && Object.entries(f).some(v => Array.isArray(v))) { + fNew.push(f); + } else if(arrays === 0) { + fNew.push(f); + } + } + return fNew; +} \ No newline at end of file