Skip to content

Commit

Permalink
fix username autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Nov 16, 2024
1 parent dd37773 commit 1fa3e65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/services/page-api.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions src/services/username-search.ts
Original file line number Diff line number Diff line change
@@ -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<string, string[]>;
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<UserDirectory>((dir, row) => ({ ...dir, [row.pubkey]: row.names }), {});
});

const updates = eventStore.stream([{ kinds: [kinds.Metadata] }]).pipe(
filter((event) => !isFromCache(event)),
bufferTime(500),
Expand All @@ -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<UserDirectory>((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<UserDirectory, SearchDirectory>((dir) => Object.entries(dir).map(([pubkey, names]) => ({ pubkey, names }))),
shareReplay(1),
);

0 comments on commit 1fa3e65

Please sign in to comment.