rank results in accountsSearchController

This commit is contained in:
Siddharth Singh 2024-05-27 09:42:43 +05:30
parent ae271b6505
commit 1dc895d49b
No known key found for this signature in database
3 changed files with 25 additions and 8 deletions

View file

@ -23,7 +23,7 @@
"@isaacs/ttlcache": "npm:@isaacs/ttlcache@^1.4.1",
"@lambdalisue/async": "jsr:@lambdalisue/async@^2.1.1",
"@noble/secp256k1": "npm:@noble/secp256k1@^2.0.0",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.22.0",
"@nostrify/nostrify": "jsr:@nostrify/nostrify@^0.23.0",
"@sentry/deno": "https://deno.land/x/sentry@7.112.2/index.mjs",
"@soapbox/kysely-deno-sqlite": "jsr:@soapbox/kysely-deno-sqlite@^2.1.0",
"@soapbox/stickynotes": "jsr:@soapbox/stickynotes@^0.4.0",

View file

@ -126,7 +126,7 @@ const accountSearchController: AppController = async (c) => {
const [event, events] = await Promise.all([
lookupAccount(query),
store.query([{ kinds: [0], search: query, limit }], { signal }),
store.query([{ kinds: [0], search: query + '$$ditto_order:ranked', limit }], { signal }),
]);
const results = await hydrateEvents({

View file

@ -1,6 +1,6 @@
// deno-lint-ignore-file require-await
import { NDatabase, NIP50, NKinds, NostrEvent, NostrFilter, NSchema as n, NStore } from '@nostrify/nostrify';
import { NDatabase, NIP50, NKinds, NostrEvent, NostrFilter, NSchema as n, NStore, NDatabaseSearchIndex } from '@nostrify/nostrify';
import { Stickynotes } from '@soapbox/stickynotes';
import { Kysely } from 'kysely';
@ -12,6 +12,9 @@ import { purifyEvent } from '@/storages/hydrate.ts';
import { isNostrId, isURL } from '@/utils.ts';
import { abortError } from '@/utils/abort.ts';
import { getTagSet } from '@/utils/tags.ts';
import { DittoDB } from '@/db/DittoDB.ts';
const db = await DittoDB.getInstance();
/** Function to decide whether or not to index a tag. */
type TagCondition = ({ event, count, value }: {
@ -165,16 +168,30 @@ class EventsDB implements NStore {
}
/** Build a search index from the event. */
static searchText(event: NostrEvent): string {
static async searchText(event: NostrEvent): Promise<NDatabaseSearchIndex | undefined> {
const res = await db.selectFrom('author_stats')
.select('author_stats.followers_count')
.where('pubkey', '=', event.pubkey)
.executeTakeFirst();
switch (event.kind) {
case 0:
return EventsDB.buildUserSearchContent(event);
return {
idx: EventsDB.buildUserSearchContent(event),
ranking: res?.followers_count || 0
}
case 1:
return event.content;
return {
idx: event.content,
ranking: res?.followers_count || 0
}
case 30009:
return EventsDB.buildTagsSearchContent(event.tags.filter(([t]) => t !== 'alt'));
return {
ranking: 0,
idx: EventsDB.buildTagsSearchContent(event.tags.filter(([t]) => t !== 'alt'))
}
default:
return '';
return;
}
}