chore: formatting

This commit is contained in:
2024-04-05 14:12:31 +01:00
parent a938e466d7
commit 95b160dd04
7 changed files with 127 additions and 110 deletions

View File

@ -62,7 +62,7 @@ export class OutboxModel extends BaseRequestRouter {
const topRelays = [...relayUserMap.entries()].sort(([, v], [, v1]) => v1.size - v.size);
if (missing.length > 0) {
this.#log("No relay metadata found, outbox model will not work for %O", missing)
this.#log("No relay metadata found, outbox model will not work for %O", missing);
}
// <relay, key[]> - count keys per relay
// <key, relay[]> - pick n top relays
@ -101,9 +101,11 @@ export class OutboxModel extends BaseRequestRouter {
return [filter];
}
const topWriteRelays = this.pickTopRelays(unwrap(authors),
const topWriteRelays = this.pickTopRelays(
unwrap(authors),
pickN ?? DefaultPickNRelays,
pattern === "inbox" ? "read" : "write");
pattern === "inbox" ? "read" : "write",
);
const pickedRelays = dedupe(topWriteRelays.flatMap(a => a.relays));
const picked = pickedRelays.map(a => {
@ -111,7 +113,7 @@ export class OutboxModel extends BaseRequestRouter {
return {
...filter,
[key]: keysOnPickedRelay,
relays: appendDedupe(filter.relays, [a])
relays: appendDedupe(filter.relays, [a]),
} as ReqFilter;
});
const noRelays = dedupe(topWriteRelays.filter(a => a.relays.length === 0).map(a => a.key));
@ -175,7 +177,7 @@ export class OutboxModel extends BaseRequestRouter {
await this.updateRelayLists(recipients);
const relays = this.pickTopRelays(recipients, pickN ?? DefaultPickNRelays, "read");
const ret = removeUndefined(dedupe(relays.map(a => a.relays).flat()));
this.#log("Picked: pattern=%s, input=%O, output=%O", "inbox", ev, ret);
return ret;
}

View File

@ -19,10 +19,7 @@ export class ProfileLoaderService extends BackgroundLoader<CachedMetadata> {
override buildSub(missing: string[]): RequestBuilder {
const sub = new RequestBuilder(`profiles`);
sub.withFilter()
.kinds([EventKind.SetMetadata])
.authors(missing)
.relay(MetadataRelays);
sub.withFilter().kinds([EventKind.SetMetadata]).authors(missing).relay(MetadataRelays);
return sub;
}

View File

@ -8,63 +8,63 @@ import { ReqFilter, RequestBuilder, SystemInterface, TaggedNostrEvent } from "."
const NostrBirthday: number = new Date(2021, 1, 1).getTime() / 1000;
interface RangeSyncEvents {
event: (ev: Array<TaggedNostrEvent>) => void
scan: (from: number) => void
event: (ev: Array<TaggedNostrEvent>) => void;
scan: (from: number) => void;
}
/**
* A simple time based sync for pulling lots of data from nostr
*/
export class RangeSync extends EventEmitter<RangeSyncEvents> {
#start: number = NostrBirthday;
#windowSize: number = 60 * 60 * 12;
#start: number = NostrBirthday;
#windowSize: number = 60 * 60 * 12;
constructor(readonly system: SystemInterface) {
super();
constructor(readonly system: SystemInterface) {
super();
}
/**
* Set window size in seconds
*/
setWindowSize(n: number) {
if (n < 60) {
throw new Error("Window size too small");
}
this.#windowSize = n;
}
/**
* Set start time for range sync
* @param n Unix timestamp
*/
setStartPoint(n: number) {
if (n < NostrBirthday) {
throw new Error("Start point cannot be before nostr's birthday");
}
this.#start = n;
}
/**
* Request to sync with a given filter
*/
async sync(filter: ReqFilter) {
if (filter.since !== undefined || filter.until !== undefined || filter.limit !== undefined) {
throw new Error("Filter must not contain since/until/limit");
}
/**
* Set window size in seconds
*/
setWindowSize(n: number) {
if (n < 60) {
throw new Error("Window size too small");
}
this.#windowSize = n;
if (!this.system.requestRouter) {
throw new Error("RangeSync cannot work without request router!");
}
/**
* Set start time for range sync
* @param n Unix timestamp
*/
setStartPoint(n: number) {
if (n < NostrBirthday) {
throw new Error("Start point cannot be before nostr's birthday");
}
this.#start = n;
const now = unixNow();
for (let end = now; end > this.#start; end -= this.#windowSize) {
const rb = new RequestBuilder(`range-query:${end}`);
rb.withBareFilter(filter)
.since(end - this.#windowSize)
.until(end);
this.emit("scan", end);
const results = await this.system.Fetch(rb);
this.emit("event", results);
}
/**
* Request to sync with a given filter
*/
async sync(filter: ReqFilter) {
if (filter.since !== undefined || filter.until !== undefined || filter.limit !== undefined) {
throw new Error("Filter must not contain since/until/limit");
}
if (!this.system.requestRouter) {
throw new Error("RangeSync cannot work without request router!");
}
const now = unixNow();
for (let end = now; end > this.#start; end -= this.#windowSize) {
const rb = new RequestBuilder(`range-query:${end}`);
rb.withBareFilter(filter)
.since(end - this.#windowSize)
.until(end);
this.emit("scan", end);
const results = await this.system.Fetch(rb);
this.emit("event", results);
}
}
}
}
}