Add local suggestions controller

This commit is contained in:
Alex Gleason 2025-01-30 21:16:38 -06:00
parent c3403ba724
commit 99d52f8640
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 30 additions and 3 deletions

View file

@ -109,7 +109,11 @@ import {
zappedByController,
} from '@/controllers/api/statuses.ts';
import { streamingController } from '@/controllers/api/streaming.ts';
import { suggestionsV1Controller, suggestionsV2Controller } from '@/controllers/api/suggestions.ts';
import {
localSuggestionsController,
suggestionsV1Controller,
suggestionsV2Controller,
} from '@/controllers/api/suggestions.ts';
import {
hashtagTimelineController,
homeTimelineController,
@ -348,6 +352,7 @@ app.get(
app.get('/api/v1/suggestions', suggestionsV1Controller);
app.get('/api/v2/suggestions', suggestionsV2Controller);
app.get('/api/v2/ditto/suggestions/local', localSuggestionsController);
app.get('/api/v1/notifications', rateLimitMiddleware(8, Time.seconds(30)), requireSigner, notificationsController);
app.get('/api/v1/notifications/:id', requireSigner, notificationController);

View file

@ -4,7 +4,7 @@ import { matchFilter } from 'nostr-tools';
import { AppContext, AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginatedList } from '@/utils/api.ts';
import { paginated, paginatedList } from '@/utils/api.ts';
import { getTagSet } from '@/utils/tags.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
@ -87,3 +87,24 @@ async function renderV2Suggestions(c: AppContext, params: { offset: number; limi
};
}));
}
export const localSuggestionsController: AppController = async (c) => {
const signal = c.req.raw.signal;
const params = c.get('pagination');
const store = c.get('store');
const events = await store.query(
[{ kinds: [0], search: `domain:${Conf.url.host}`, ...params }],
{ signal },
)
.then((events) => hydrateEvents({ store, events, signal }));
const suggestions = await Promise.all(events.map(async (event) => {
return {
source: 'global',
account: await renderAccount(event),
};
}));
return paginated(c, events, suggestions);
};

View file

@ -207,7 +207,8 @@ function buildLinkHeader(url: string, events: NostrEvent[]): string | undefined
return `<${next}>; rel="next", <${prev}>; rel="prev"`;
}
type Entity = { id: string };
// deno-lint-ignore ban-types
type Entity = {};
type HeaderRecord = Record<string, string | string[]>;
/** Return results with pagination headers. Assumes chronological sorting of events. */