bug: search page fix

This commit is contained in:
2023-06-19 09:55:40 +01:00
parent 12250c5e3d
commit d573d075fe
8 changed files with 54 additions and 58 deletions

View File

@ -1,6 +1,6 @@
import debug from "debug";
import { v4 as uuid } from "uuid";
import { appendDedupe, dedupe, unixNowMs } from "@snort/shared";
import { appendDedupe, sanitizeRelayUrl, unixNowMs } from "@snort/shared";
import { ReqFilter, u256, HexKey, EventKind } from ".";
import { diffFilters } from "./RequestSplitter";
@ -24,9 +24,9 @@ export enum RequestStrategy {
AuthorsRelays = 2,
/**
* Relay hints are usually provided when using replies
* Use pre-determined relays for query
*/
RelayHintedEventIds = 3,
ExplicitRelays = 3,
}
/**
@ -152,14 +152,21 @@ export class RequestBuilder {
*/
export class RequestFilterBuilder {
#filter: ReqFilter = {};
#relayHints = new Map<u256, Array<string>>();
#relays = new Set<string>();
get filter() {
return { ...this.#filter };
}
get relayHints() {
return new Map(this.#relayHints);
/**
* Use a specific relay for this request filter
*/
relay(u: string) {
const uClean = sanitizeRelayUrl(u);
if (uClean) {
this.#relays.add(uClean);
}
return this;
}
ids(ids: Array<u256>) {
@ -167,13 +174,6 @@ export class RequestFilterBuilder {
return this;
}
id(id: u256, relay?: string) {
if (relay) {
this.#relayHints.set(id, appendDedupe(this.#relayHints.get(id), [relay]));
}
return this.ids([id]);
}
authors(authors?: Array<HexKey>) {
if (!authors) return this;
this.#filter.authors = appendDedupe(this.#filter.authors, authors);
@ -220,20 +220,13 @@ export class RequestFilterBuilder {
* Build/expand this filter into a set of relay specific queries
*/
build(relays: RelayCache, id: string): Array<BuiltRawReqFilter> {
// when querying for specific event ids with relay hints
// take the first approach which is to split the filter by relay
if (this.#filter.ids && this.#relayHints.size > 0) {
const relays = dedupe([...this.#relayHints.values()].flat());
return relays.map(r => {
// use the explicit relay list first
if (this.#relays.size > 0) {
return [...this.#relays].map(r => {
return {
filters: [
{
...this.#filter,
ids: [...this.#relayHints.entries()].filter(([, v]) => v.includes(r)).map(([k]) => k),
},
],
filters: [this.#filter],
relay: r,
strategy: RequestStrategy.RelayHintedEventIds,
strategy: RequestStrategy.ExplicitRelays,
};
});
}

View File

@ -3,8 +3,14 @@ import { splitAllByWriteRelays } from "../src/GossipModel"
describe("GossipModel", () => {
it("should not output empty", () => {
const Relays = {
get: (pk?: string) => {
return [];
getFromCache: (pk?: string) => {
if (pk) {
return {
pubkey: pk,
created_at: 0,
relays: []
};
}
}
}
const a = [{

View File

@ -2,22 +2,26 @@ import { RelayCache } from "../src/GossipModel";
import { RequestBuilder, RequestStrategy } from "../src/RequestBuilder";
import { describe, expect } from "@jest/globals";
import { expandFilter } from "../src/RequestExpander";
import { unixNowMs } from "../src/Utils";
import { bytesToHex } from "@noble/curves/abstract/utils";
import { unixNow, unixNowMs } from "@snort/shared";
const DummyCache = {
get: (pk?: string) => {
getFromCache: (pk?: string) => {
if (!pk) return undefined;
return [
{
url: `wss://${pk}.com/`,
settings: {
read: true,
write: true,
return {
pubkey: pk,
created_at: unixNow(),
relays: [
{
url: `wss://${pk}.com/`,
settings: {
read: true,
write: true,
},
},
},
];
]
};
},
} as RelayCache;