This commit is contained in:
2023-09-19 13:03:29 +01:00
parent 1c52db933f
commit dde730238d
12 changed files with 138 additions and 151 deletions

View File

@ -1,10 +1,10 @@
import "./Thread.css"; import "./Thread.css";
import { useMemo, useState, ReactNode, useContext } from "react"; import { useMemo, useState, ReactNode, useContext } from "react";
import { useIntl } from "react-intl"; 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 { 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 BackButton from "Element/BackButton";
import Note from "Element/Note"; import Note from "Element/Note";
import NoteGhost from "Element/NoteGhost"; import NoteGhost from "Element/NoteGhost";
@ -154,8 +154,7 @@ const TierThree = ({ active, isLastSubthread, notes, related, chains, onNavigate
return ( return (
<> <>
<div <div
className={`subthread-container ${hasMultipleNotes ? "subthread-multi" : ""} ${ className={`subthread-container ${hasMultipleNotes ? "subthread-multi" : ""} ${isLast ? "subthread-last" : "subthread-mid"
isLast ? "subthread-last" : "subthread-mid"
}`}> }`}>
<Divider variant="small" /> <Divider variant="small" />
<Note <Note
@ -185,8 +184,7 @@ const TierThree = ({ active, isLastSubthread, notes, related, chains, onNavigate
return ( return (
<div <div
key={r.id} key={r.id}
className={`subthread-container ${lastReply ? "" : "subthread-multi"} ${ className={`subthread-container ${lastReply ? "" : "subthread-multi"} ${lastReply ? "subthread-last" : "subthread-mid"
lastReply ? "subthread-last" : "subthread-mid"
}`}> }`}>
<Divider variant="small" /> <Divider variant="small" />
<Note <Note
@ -225,7 +223,7 @@ export function Thread(props: { onBack?: () => void }) {
function navigateThread(e: TaggedNostrEvent) { function navigateThread(e: TaggedNostrEvent) {
thread.setCurrent(e.id); 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(() => { const parent = useMemo(() => {
@ -239,8 +237,6 @@ export function Thread(props: { onBack?: () => void }) {
} }
}, [thread.root]); }, [thread.root]);
const brokenChains = Array.from(thread.chains?.keys()).filter(a => !thread.data?.some(b => b.id === a));
function renderRoot(note: TaggedNostrEvent) { function renderRoot(note: TaggedNostrEvent) {
const className = `thread-root${isSingleNote ? " thread-root-single" : ""}`; const className = `thread-root${isSingleNote ? " thread-root-single" : ""}`;
if (note) { if (note) {
@ -307,18 +303,6 @@ export function Thread(props: { onBack?: () => void }) {
<div className="main-content"> <div className="main-content">
{thread.root && renderRoot(thread.root)} {thread.root && renderRoot(thread.root)}
{thread.root && renderChain(thread.root.id)} {thread.root && renderChain(thread.root.id)}
{brokenChains.length > 0 && <h3>Other replies</h3>}
{brokenChains.map(a => {
return (
<div className="mb10">
<NoteGhost className={`thread-note thread-root ghost-root`} key={a}>
Missing event <Link to={eventLink(a)}>{a.substring(0, 8)}</Link>
</NoteGhost>
{renderChain(a)}
</div>
);
})}
</div> </div>
</> </>
); );

View File

@ -1,29 +1,11 @@
import { useMemo } from "react"; 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 { useRequestBuilder } from "@snort/system-react";
import { unwrap } from "SnortUtils";
export function useEventFeed(link: NostrLink) { export function useEventFeed(link: NostrLink) {
const sub = useMemo(() => { const sub = useMemo(() => {
const b = new RequestBuilder(`event:${link.id.slice(0, 12)}`); const b = new RequestBuilder(`event:${link.id.slice(0, 12)}`);
if (link.type === NostrPrefix.Address) { b.withFilter().link(link);
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]);
}
}
return b; return b;
}, [link]); }, [link]);
@ -33,25 +15,7 @@ export function useEventFeed(link: NostrLink) {
export function useEventsFeed(id: string, links: Array<NostrLink>) { export function useEventsFeed(id: string, links: Array<NostrLink>) {
const sub = useMemo(() => { const sub = useMemo(() => {
const b = new RequestBuilder(`events:${id}`); const b = new RequestBuilder(`events:${id}`);
for (const l of links) { links.forEach(v => b.withFilter().link(v));
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]);
}
}
}
return b; return b;
}, [id, links]); }, [id, links]);

View File

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

View File

@ -2,25 +2,26 @@ import { useEffect, useMemo, useState } from "react";
import { EventKind, NostrLink, RequestBuilder, NoteCollection } from "@snort/system"; import { EventKind, NostrLink, RequestBuilder, NoteCollection } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react"; import { useRequestBuilder } from "@snort/system-react";
import useLogin from "Hooks/useLogin";
import { useReactions } from "./FeedReactions"; import { useReactions } from "./FeedReactions";
export default function useThreadFeed(link: NostrLink) { export default function useThreadFeed(link: NostrLink) {
const [allEvents, setAllEvents] = useState<Array<NostrLink>>([]); const [allEvents, setAllEvents] = useState<Array<NostrLink>>([]);
const pref = useLogin().preferences;
const sub = useMemo(() => { const sub = useMemo(() => {
const sub = new RequestBuilder(`thread:${link.id}`); const sub = new RequestBuilder(`thread:${link.id.slice(0, 12)}`);
sub.withOptions({ sub.withOptions({
leaveOpen: true, leaveOpen: true,
}); });
sub.withFilter().kinds([EventKind.TextNote]).link(link); sub
if (allEvents.length > 0) { .withFilter()
const f = sub.withFilter().kinds([EventKind.TextNote]); .kinds([EventKind.TextNote])
allEvents.forEach(x => f.replyToLink(x)); .link(link)
} .replyToLink(link);
allEvents.forEach(x => {
sub.withFilter().kinds([EventKind.TextNote]).link(x).replyToLink(x);
});
return sub; return sub;
}, [allEvents.length, pref]); }, [allEvents.length]);
const store = useRequestBuilder(NoteCollection, sub); const store = useRequestBuilder(NoteCollection, sub);
@ -37,7 +38,7 @@ export default function useThreadFeed(link: NostrLink) {
} }
}, [store.data?.length]); }, [store.data?.length]);
const reactions = useReactions(`thread:${link.id}:reactions`, allEvents); const reactions = useReactions(`thread:${link.id.slice(0, 12)}:reactions`, allEvents);
return { return {
thread: store.data ?? [], thread: store.data ?? [],

View File

@ -1,5 +1,5 @@
import { useMemo } from "react"; 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 { useRequestBuilder } from "@snort/system-react";
import { UserCache } from "Cache"; import { UserCache } from "Cache";
@ -7,11 +7,7 @@ export default function useZapsFeed(link?: NostrLink) {
const sub = useMemo(() => { const sub = useMemo(() => {
if (!link) return null; if (!link) return null;
const b = new RequestBuilder(`zaps:${link.encode()}`); const b = new RequestBuilder(`zaps:${link.encode()}`);
if (link.type === NostrPrefix.PublicKey) { b.withFilter().kinds([EventKind.ZapReceipt]).replyToLink(link);
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]);
}
return b; return b;
}, [link]); }, [link]);

View File

@ -27,8 +27,9 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
?.sort((a, b) => b.created_at - a.created_at) ?.sort((a, b) => b.created_at - a.created_at)
.forEach(v => { .forEach(v => {
const t = EventExt.extractThread(v); const t = EventExt.extractThread(v);
let replyTo = t?.replyTo?.value ?? t?.root?.value; if (t) {
if (t?.root?.key === "a" && t?.root?.value) { let replyTo = t.replyTo?.value ?? t.root?.value;
if (t.root?.key === "a" && t.root?.value) {
const parsed = t.root.value.split(":"); const parsed = t.root.value.split(":");
replyTo = feed.thread?.find( replyTo = feed.thread?.find(
a => a.kind === Number(parsed[0]) && a.pubkey === parsed[1] && findTag(a, "d") === parsed[2], a => a.kind === Number(parsed[0]) && a.pubkey === parsed[1] && findTag(a, "d") === parsed[2],
@ -41,6 +42,7 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
unwrap(chains.get(replyTo)).push(v); unwrap(chains.get(replyTo)).push(v);
} }
} }
}
}); });
} }
return chains; return chains;
@ -91,7 +93,7 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
} }
} }
} }
}, [feed.thread, currentId, location]); }, [feed.thread.length, currentId, location]);
const ctxValue = useMemo(() => { const ctxValue = useMemo(() => {
return { return {

View File

@ -94,6 +94,7 @@ export abstract class EventExt {
value: tag[1], value: tag[1],
} as Tag; } as Tag;
switch (ret.key) { switch (ret.key) {
case "a":
case "e": { case "e": {
ret.relay = tag.length > 2 ? tag[2] : undefined; ret.relay = tag.length > 2 ? tag[2] : undefined;
ret.marker = tag.length > 3 ? tag[3] : undefined; ret.marker = tag.length > 3 ? tag[3] : undefined;
@ -102,38 +103,38 @@ export abstract class EventExt {
} }
return ret; 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 shouldWriteMarkers = ev.kind === EventKind.TextNote;
const ret = { const ret = {
mentions: [], mentions: [],
pubKeys: [], pubKeys: [],
} as Thread; } as Thread;
const eTags = ev.tags.filter(a => a[0] === "e" || a[0] === "a").map(a => EventExt.parseTag(a)); const replyTags = ev.tags.filter(a => a[0] === "e" || a[0] === "a").map(a => EventExt.parseTag(a));
const marked = eTags.some(a => a.marker); if(replyTags.length > 0) {
const marked = replyTags.some(a => a.marker);
if (!marked) { if (!marked) {
ret.root = eTags[0]; ret.root = replyTags[0];
ret.root.marker = shouldWriteMarkers ? "root" : undefined; ret.root.marker = shouldWriteMarkers ? "root" : undefined;
if (eTags.length > 1) { if (replyTags.length > 1) {
ret.replyTo = eTags[eTags.length - 1]; ret.replyTo = replyTags[replyTags.length - 1];
ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined; ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined;
} }
if (eTags.length > 2) { if (replyTags.length > 2) {
ret.mentions = eTags.slice(1, -1); ret.mentions = replyTags.slice(1, -1);
if (shouldWriteMarkers) { if (shouldWriteMarkers) {
ret.mentions.forEach(a => (a.marker = "mention")); ret.mentions.forEach(a => (a.marker = "mention"));
} }
} }
} else { } else {
const root = eTags.find(a => a.marker === "root"); const root = replyTags.find(a => a.marker === "root");
const reply = eTags.find(a => a.marker === "reply"); const reply = replyTags.find(a => a.marker === "reply");
ret.root = root; ret.root = root;
ret.replyTo = reply; ret.replyTo = reply;
ret.mentions = eTags.filter(a => a.marker === "mention"); ret.mentions = replyTags.filter(a => a.marker === "mention");
}
} else {
return undefined;
} }
ret.pubKeys = Array.from(new Set(ev.tags.filter(a => a[0] === "p").map(a => a[1]))); ret.pubKeys = Array.from(new Set(ev.tags.filter(a => a[0] === "p").map(a => a[1])));
return ret; return ret;

View File

@ -5,7 +5,7 @@ import { NostrEvent, TaggedNostrEvent } from "./nostr";
import { AuthHandler, Connection, RelaySettings, ConnectionStateSnapshot } from "./connection"; import { AuthHandler, Connection, RelaySettings, ConnectionStateSnapshot } from "./connection";
import { Query } from "./query"; import { Query } from "./query";
import { NoteCollection, NoteStore, NoteStoreSnapshotData } from "./note-collection"; 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 { RelayMetricHandler } from "./relay-metric-handler";
import { import {
MetadataCache, MetadataCache,
@ -22,6 +22,7 @@ import {
import { EventsCache } from "./cache/events"; import { EventsCache } from "./cache/events";
import { RelayCache } from "./gossip-model"; import { RelayCache } from "./gossip-model";
import { QueryOptimizer, DefaultQueryOptimizer } from "./query-optimizer"; import { QueryOptimizer, DefaultQueryOptimizer } from "./query-optimizer";
import { trimFilters } from "./request-trim";
/** /**
* Manages nostr content retrieval system * Manages nostr content retrieval system
@ -295,20 +296,22 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
if (cacheResults.length > 0) { if (cacheResults.length > 0) {
const resultIds = new Set(cacheResults.map(a => a.id)); const resultIds = new Set(cacheResults.map(a => a.id));
f.ids = f.ids.filter(a => !resultIds.has(a)); f.ids = f.ids.filter(a => !resultIds.has(a));
q.feed.add(cacheResults as Array<TaggedNostrEvent>); q.insertCompletedTrace({
filters:[{...f, ids: [...resultIds]}],
strategy: RequestStrategy.ExplicitRelays,
relay: ""
}, cacheResults as Array<TaggedNostrEvent>);
} }
} }
} }
// check for empty filters // check for empty filters
qSend.filters = qSend.filters.filter(a => const fNew = trimFilters(qSend.filters);
Object.values(a) if (fNew.length === 0) {
.filter(v => Array.isArray(v))
.every(b => (b as Array<string | number>).length > 0),
);
if (qSend.filters.length === 0) {
return; return;
} }
qSend.filters = fNew;
if (qSend.relay) { if (qSend.relay) {
this.#log("Sending query to %s %O", qSend.relay, qSend); this.#log("Sending query to %s %O", qSend.relay, qSend);
const s = this.#sockets.get(qSend.relay); const s = this.#sockets.get(qSend.relay);

View File

@ -37,13 +37,11 @@ export class ProfileLoaderService {
* Request profile metadata for a set of pubkeys * Request profile metadata for a set of pubkeys
*/ */
TrackMetadata(pk: HexKey | Array<HexKey>) { TrackMetadata(pk: HexKey | Array<HexKey>) {
const bufferNow = [];
for (const p of Array.isArray(pk) ? pk : [pk]) { for (const p of Array.isArray(pk) ? pk : [pk]) {
if (p.length === 64 && this.#wantsMetadata.add(p)) { if (p.length === 64) {
bufferNow.push(p); this.#wantsMetadata.add(p)
} }
} }
this.#cache.buffer(bufferNow);
} }
/** /**

View File

@ -197,6 +197,28 @@ export class Query implements QueryBase {
this.#stopCheckTraces(); this.#stopCheckTraces();
} }
/**
* Insert a new trace as a placeholder
*/
insertCompletedTrace(subq: BuiltRawReqFilter, data: Readonly<Array<TaggedNostrEvent>>) {
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) { sendToRelay(c: Connection, subq: BuiltRawReqFilter) {
if (!this.#canSendQuery(c, subq)) { if (!this.#canSendQuery(c, subq)) {
return; return;

View File

@ -113,7 +113,7 @@ export class RequestBuilder {
const diff = system.QueryOptimizer.getDiff(prev, this.buildRaw()); const diff = system.QueryOptimizer.getDiff(prev, this.buildRaw());
const ts = unixNowMs() - start; 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) { if (diff.length > 0) {
return splitFlatByWriteRelays(system.RelayCache, diff).map(a => { return splitFlatByWriteRelays(system.RelayCache, diff).map(a => {
return { return {
@ -146,7 +146,7 @@ export class RequestBuilder {
const filtersSquashed = [...relayMerged.values()].map(a => { const filtersSquashed = [...relayMerged.values()].map(a => {
return { 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, relay: a[0].relay,
strategy: a[0].strategy, strategy: a[0].strategy,
} as BuiltRawReqFilter; } as BuiltRawReqFilter;
@ -234,12 +234,15 @@ export class RequestFilterBuilder {
*/ */
link(link: NostrLink) { link(link: NostrLink) {
if (link.type === NostrPrefix.Address) { if (link.type === NostrPrefix.Address) {
return this.tag("d", [link.id]) this.tag("d", [link.id])
.kinds([unwrap(link.kind)]) .kinds([unwrap(link.kind)])
.authors([unwrap(link.author)]); .authors([unwrap(link.author)]);
link.relays?.forEach(v => this.relay(v));
} else { } 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) { replyToLink(link: NostrLink) {
if (link.type === NostrPrefix.Address) { 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 { } else {
return this.tag("e", [link.id]); this.tag("e", [link.id]);
link.relays?.forEach(v => this.relay(v));
} }
return this;
} }
/** /**

View File

@ -0,0 +1,26 @@
import { ReqFilter } from "nostr";
/**
* Remove empty filters, filters which would result in no results
*/
export function trimFilters(filters: Array<ReqFilter>) {
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;
}