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

View File

@ -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,8 +154,7 @@ const TierThree = ({ active, isLastSubthread, notes, related, chains, onNavigate
return (
<>
<div
className={`subthread-container ${hasMultipleNotes ? "subthread-multi" : ""} ${
isLast ? "subthread-last" : "subthread-mid"
className={`subthread-container ${hasMultipleNotes ? "subthread-multi" : ""} ${isLast ? "subthread-last" : "subthread-mid"
}`}>
<Divider variant="small" />
<Note
@ -185,8 +184,7 @@ const TierThree = ({ active, isLastSubthread, notes, related, chains, onNavigate
return (
<div
key={r.id}
className={`subthread-container ${lastReply ? "" : "subthread-multi"} ${
lastReply ? "subthread-last" : "subthread-mid"
className={`subthread-container ${lastReply ? "" : "subthread-multi"} ${lastReply ? "subthread-last" : "subthread-mid"
}`}>
<Divider variant="small" />
<Note
@ -225,7 +223,7 @@ export function Thread(props: { onBack?: () => 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 }) {
<div className="main-content">
{thread.root && renderRoot(thread.root)}
{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>
</>
);

View File

@ -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<NostrLink>) {
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]);

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 { useRequestBuilder } from "@snort/system-react";
import useLogin from "Hooks/useLogin";
import { useReactions } from "./FeedReactions";
export default function useThreadFeed(link: NostrLink) {
const [allEvents, setAllEvents] = useState<Array<NostrLink>>([]);
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 ?? [],

View File

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

View File

@ -27,8 +27,9 @@ 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) {
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],
@ -41,6 +42,7 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
unwrap(chains.get(replyTo)).push(v);
}
}
}
});
}
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(() => {
return {

View File

@ -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);
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 = eTags[0];
ret.root = replyTags[0];
ret.root.marker = shouldWriteMarkers ? "root" : undefined;
if (eTags.length > 1) {
ret.replyTo = eTags[eTags.length - 1];
if (replyTags.length > 1) {
ret.replyTo = replyTags[replyTags.length - 1];
ret.replyTo.marker = shouldWriteMarkers ? "reply" : undefined;
}
if (eTags.length > 2) {
ret.mentions = eTags.slice(1, -1);
if (replyTags.length > 2) {
ret.mentions = replyTags.slice(1, -1);
if (shouldWriteMarkers) {
ret.mentions.forEach(a => (a.marker = "mention"));
}
}
} else {
const root = eTags.find(a => a.marker === "root");
const reply = eTags.find(a => a.marker === "reply");
const root = replyTags.find(a => a.marker === "root");
const reply = replyTags.find(a => a.marker === "reply");
ret.root = root;
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])));
return ret;

View File

@ -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<SystemSnapshot> 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<TaggedNostrEvent>);
q.insertCompletedTrace({
filters:[{...f, ids: [...resultIds]}],
strategy: RequestStrategy.ExplicitRelays,
relay: ""
}, cacheResults as Array<TaggedNostrEvent>);
}
}
}
// check for empty filters
qSend.filters = qSend.filters.filter(a =>
Object.values(a)
.filter(v => Array.isArray(v))
.every(b => (b as Array<string | number>).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);

View File

@ -37,13 +37,11 @@ export class ProfileLoaderService {
* Request profile metadata for a set of pubkeys
*/
TrackMetadata(pk: HexKey | Array<HexKey>) {
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);
}
/**

View File

@ -197,6 +197,28 @@ export class Query implements QueryBase {
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) {
if (!this.#canSendQuery(c, subq)) {
return;

View File

@ -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;
}
/**

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