Cleanup
This commit is contained in:
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
26
packages/system/src/request-trim.ts
Normal file
26
packages/system/src/request-trim.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user