feat: show users you follow first in search

getPubkeysBySearch() function refactored to accept a followList argument
This commit is contained in:
P. Reis
2024-09-17 11:04:27 -03:00
parent 2fe6a8fde5
commit 52001373e0
3 changed files with 43 additions and 15 deletions

View File

@@ -117,6 +117,7 @@ const accountSearchController: AppController = async (c) => {
const { signal } = c.req.raw;
const { limit } = c.get('pagination');
const kysely = await Storages.kysely();
const viewerPubkey = await c.get('signer')?.getPublicKey();
const result = accountSearchQuerySchema.safeParse(c.req.query());
@@ -135,7 +136,11 @@ const accountSearchController: AppController = async (c) => {
return c.json(pubkey ? [await accountFromPubkey(pubkey)] : []);
}
const pubkeys = await getPubkeysBySearch(kysely, { q: query, limit });
const followList: string[] = [];
if (viewerPubkey) {
followList.push(...await getFollowedPubkeys(viewerPubkey));
}
const pubkeys = (await getPubkeysBySearch(kysely, { q: query, limit, followList })).slice(0, limit);
let events = event ? [event] : await store.query([{ kinds: [0], authors: pubkeys, limit }], {
signal,

View File

@@ -10,6 +10,7 @@ import { extractIdentifier, lookupPubkey } from '@/utils/lookup.ts';
import { nip05Cache } from '@/utils/nip05.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { getFollowedPubkeys } from '@/queries.ts';
import { getPubkeysBySearch } from '@/utils/search.ts';
const searchQuerySchema = z.object({
@@ -26,6 +27,7 @@ type SearchQuery = z.infer<typeof searchQuerySchema>;
const searchController: AppController = async (c) => {
const result = searchQuerySchema.safeParse(c.req.query());
const { signal } = c.req.raw;
const viewerPubkey = await c.get('signer')?.getPublicKey();
if (!result.success) {
return c.json({ error: 'Bad request', schema: result.error }, 422);
@@ -49,9 +51,7 @@ const searchController: AppController = async (c) => {
if (event) {
events = [event];
}
events.push(...(await searchEvents(result.data, signal)));
const viewerPubkey = await c.get('signer')?.getPublicKey();
events.push(...(await searchEvents({ ...result.data, viewerPubkey }, signal)));
const [accounts, statuses] = await Promise.all([
Promise.all(
@@ -76,7 +76,10 @@ const searchController: AppController = async (c) => {
};
/** Get events for the search params. */
async function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: AbortSignal): Promise<NostrEvent[]> {
async function searchEvents(
{ q, type, limit, account_id, viewerPubkey }: SearchQuery & { viewerPubkey?: string },
signal: AbortSignal,
): Promise<NostrEvent[]> {
if (type === 'hashtags') return Promise.resolve([]);
const filter: NostrFilter = {
@@ -93,7 +96,11 @@ async function searchEvents({ q, type, limit, account_id }: SearchQuery, signal:
if (type === 'accounts') {
const kysely = await Storages.kysely();
pubkeys.push(...(await getPubkeysBySearch(kysely, { q, limit })));
const followList: string[] = [];
if (viewerPubkey) {
followList.push(...await getFollowedPubkeys(viewerPubkey));
}
pubkeys.push(...(await getPubkeysBySearch(kysely, { q, limit, followList })));
if (!filter?.authors) {
filter.authors = pubkeys;