From 1dc895d49b98d7900aea7c66500a8aa7e77cfb7d Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Mon, 27 May 2024 09:42:43 +0530 Subject: [PATCH] rank results in accountsSearchController --- deno.json | 2 +- src/controllers/api/accounts.ts | 2 +- src/storages/EventsDB.ts | 29 +++++++++++++++++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/deno.json b/deno.json index e54bb7ae..d5f6119c 100644 --- a/deno.json +++ b/deno.json @@ -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", diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 13ef07e7..71665751 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -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({ diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index a550f39b..63d831fe 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -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 { + 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; } }