Add bookmarkController, refactor generic views
This commit is contained in:
parent
69a44f9d2b
commit
c8e2707704
|
@ -37,6 +37,7 @@ import {
|
|||
} from './controllers/api/accounts.ts';
|
||||
import { appCredentialsController, createAppController } from './controllers/api/apps.ts';
|
||||
import { blocksController } from './controllers/api/blocks.ts';
|
||||
import { bookmarksController } from './controllers/api/bookmarks.ts';
|
||||
import { emptyArrayController, emptyObjectController, notImplementedController } from './controllers/api/fallback.ts';
|
||||
import { instanceController } from './controllers/api/instance.ts';
|
||||
import { mediaController } from './controllers/api/media.ts';
|
||||
|
@ -175,12 +176,12 @@ app.get('/api/v1/trends', cache({ cacheName: 'web', expires: Time.minutes(15) })
|
|||
|
||||
app.get('/api/v1/notifications', requirePubkey, notificationsController);
|
||||
app.get('/api/v1/favourites', requirePubkey, favouritesController);
|
||||
app.get('/api/v1/bookmarks', requirePubkey, bookmarksController);
|
||||
app.get('/api/v1/blocks', requirePubkey, blocksController);
|
||||
|
||||
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);
|
||||
|
||||
// Not (yet) implemented.
|
||||
app.get('/api/v1/bookmarks', emptyArrayController);
|
||||
app.get('/api/v1/custom_emojis', emptyArrayController);
|
||||
app.get('/api/v1/filters', emptyArrayController);
|
||||
app.get('/api/v1/mutes', emptyArrayController);
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
import { type AppController } from '@/app.ts';
|
||||
import { eventsDB } from '@/db/events.ts';
|
||||
import { getTagSet } from '@/tags.ts';
|
||||
import { renderStatuses } from '@/views.ts';
|
||||
|
||||
/** https://docs.joinmastodon.org/methods/bookmarks/#get */
|
||||
const bookmarksController: AppController = async (c) => {
|
||||
const pubkey = c.get('pubkey')!;
|
||||
|
||||
const [event10003] = await eventsDB.getEvents([
|
||||
{ kinds: [10003], authors: [pubkey], limit: 1 },
|
||||
]);
|
||||
|
||||
if (event10003) {
|
||||
const eventIds = getTagSet(event10003.tags, 'e');
|
||||
return renderStatuses(c, [...eventIds].reverse());
|
||||
} else {
|
||||
return c.json([]);
|
||||
}
|
||||
};
|
||||
|
||||
export { bookmarksController };
|
68
src/views.ts
68
src/views.ts
|
@ -1,38 +1,68 @@
|
|||
import { AppContext } from '@/app.ts';
|
||||
import { eventsDB } from '@/db/events.ts';
|
||||
import { type Filter } from '@/deps.ts';
|
||||
import { getAuthor } from '@/queries.ts';
|
||||
import { renderAccount } from '@/views/mastodon/accounts.ts';
|
||||
import { paginated } from '@/utils/web.ts';
|
||||
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
||||
import { paginated, paginationSchema } from '@/utils/web.ts';
|
||||
|
||||
/** Render account objects for the author of each event. */
|
||||
async function renderEventAccounts(c: AppContext, filters: Filter[]) {
|
||||
const events = await eventsDB.getEvents(filters);
|
||||
async function renderEventAccounts(c: AppContext, filters: Filter[], signal = AbortSignal.timeout(1000)) {
|
||||
if (!filters.length) {
|
||||
return c.json([]);
|
||||
}
|
||||
|
||||
const events = await eventsDB.getEvents(filters, { signal });
|
||||
const pubkeys = new Set(events.map(({ pubkey }) => pubkey));
|
||||
|
||||
if (!pubkeys.size) {
|
||||
return c.json([]);
|
||||
}
|
||||
|
||||
const accounts = await Promise.all([...pubkeys].map(async (pubkey) => {
|
||||
const author = await getAuthor(pubkey, { relations: ['author_stats'] });
|
||||
if (author) {
|
||||
return renderAccount(author);
|
||||
}
|
||||
}));
|
||||
const authors = await eventsDB.getEvents(
|
||||
[{ kinds: [0], authors: [...pubkeys], relations: ['author_stats'] }],
|
||||
{ signal },
|
||||
);
|
||||
|
||||
const accounts = await Promise.all(
|
||||
authors.map((event) => renderAccount(event)),
|
||||
);
|
||||
|
||||
return paginated(c, events, accounts);
|
||||
}
|
||||
|
||||
async function renderAccounts(c: AppContext, pubkeys: string[]) {
|
||||
// TODO: pagination by offset.
|
||||
// FIXME: this is very inefficient!
|
||||
const accounts = await Promise.all(pubkeys.map(async (pubkey) => {
|
||||
const event = await getAuthor(pubkey);
|
||||
return event ? await renderAccount(event) : undefined;
|
||||
}));
|
||||
async function renderAccounts(c: AppContext, authors: string[], signal = AbortSignal.timeout(1000)) {
|
||||
const { since, until, limit } = paginationSchema.parse(c.req.query());
|
||||
|
||||
return c.json(accounts.filter(Boolean));
|
||||
const events = await eventsDB.getEvents(
|
||||
[{ kinds: [0], authors, relations: ['author_stats'], since, until, limit }],
|
||||
{ signal },
|
||||
);
|
||||
|
||||
const accounts = await Promise.all(
|
||||
events.map((event) => renderAccount(event)),
|
||||
);
|
||||
|
||||
return paginated(c, events, accounts);
|
||||
}
|
||||
|
||||
export { renderAccounts, renderEventAccounts };
|
||||
/** Render statuses by event IDs. */
|
||||
async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal.timeout(1000)) {
|
||||
const { since, until, limit } = paginationSchema.parse(c.req.query());
|
||||
|
||||
const events = await eventsDB.getEvents(
|
||||
[{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'], since, until, limit }],
|
||||
{ signal },
|
||||
);
|
||||
|
||||
if (!events.length) {
|
||||
return c.json([]);
|
||||
}
|
||||
|
||||
const statuses = await Promise.all(
|
||||
events.map((event) => renderStatus(event, c.get('pubkey'))),
|
||||
);
|
||||
|
||||
return paginated(c, events, statuses);
|
||||
}
|
||||
|
||||
export { renderAccounts, renderEventAccounts, renderStatuses };
|
||||
|
|
Loading…
Reference in New Issue