From 1fa3e6573f853361a8c7f69d8efec36fc8f4f0a2 Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Sat, 16 Nov 2024 15:18:31 -0600 Subject: [PATCH] fix username autocomplete --- src/services/page-api.ts | 7 +++++++ src/services/username-search.ts | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/services/page-api.ts b/src/services/page-api.ts index 8cf4e9836..5b26227e6 100644 --- a/src/services/page-api.ts +++ b/src/services/page-api.ts @@ -1,6 +1,7 @@ import accountService from "./account"; import channelMetadataService from "./channel-metadata"; import { eventStore, queryStore } from "./event-store"; +import { localRelay } from "./local-relay"; import localSettings from "./local-settings"; import readStatusService from "./read-status"; import relayInfoService from "./relay-info"; @@ -28,6 +29,12 @@ const noStrudel = { /** Signing queue */ signingService, + /** + * Cache relay interface + * @type MemoryRelay|WasmRelay|CacheRelay|Relay|undefined + */ + cacheRelay: localRelay, + // other internal services replaceableEventsService, userSearchDirectory, diff --git a/src/services/username-search.ts b/src/services/username-search.ts index e370a2f0d..0f6460c64 100644 --- a/src/services/username-search.ts +++ b/src/services/username-search.ts @@ -1,17 +1,25 @@ import _throttle from "lodash.throttle"; import { kinds } from "nostr-tools"; import { from } from "rxjs"; -import { filter, bufferTime, concatMap, mergeWith, reduce, shareReplay, map } from "rxjs/operators"; +import { filter, bufferTime, concatMap, mergeWith, shareReplay, map, scan } from "rxjs/operators"; import { getProfileContent, isFromCache } from "applesauce-core/helpers"; import { getSearchNames } from "../helpers/nostr/user-metadata"; import db from "./db"; import { eventStore } from "./event-store"; +import { logger } from "../helpers/debug"; export type UserDirectory = Record; export type SearchDirectory = { pubkey: string; names: string[] }[]; -const cached = from(db.getAll("userSearch") as Promise<{ pubkey: string; names: string[] }[]>); +const log = logger.extend("UsernameSearch"); + +log(`Started loading profiles`); +const cache = db.getAll("userSearch").then((rows: { pubkey: string; names: string[] }[]) => { + log(`Loaded ${rows.length} profiles`); + return rows.reduce((dir, row) => ({ ...dir, [row.pubkey]: row.names }), {}); +}); + const updates = eventStore.stream([{ kinds: [kinds.Metadata] }]).pipe( filter((event) => !isFromCache(event)), bufferTime(500), @@ -28,14 +36,14 @@ const updates = eventStore.stream([{ kinds: [kinds.Metadata] }]).pipe( } transaction.commit(); await transaction.done; + log(`Updated ${events.length} profiles`); return updates; }), ); -export const userSearchDirectory = cached.pipe( - map((rows) => rows.reduce((dir, row) => ({ ...dir, [row.pubkey]: row.names }), {})), +export const userSearchDirectory = from(cache).pipe( mergeWith(updates), - reduce((dir, updates) => ({ ...dir, ...updates })), + scan((dir, updates) => ({ ...dir, ...updates })), map((dir) => Object.entries(dir).map(([pubkey, names]) => ({ pubkey, names }))), shareReplay(1), );