Add bookmarkController, refactor generic views

This commit is contained in:
Alex Gleason 2024-01-01 14:07:39 -06:00
parent 69a44f9d2b
commit c8e2707704
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 73 additions and 20 deletions

View File

@ -37,6 +37,7 @@ import {
} from './controllers/api/accounts.ts'; } from './controllers/api/accounts.ts';
import { appCredentialsController, createAppController } from './controllers/api/apps.ts'; import { appCredentialsController, createAppController } from './controllers/api/apps.ts';
import { blocksController } from './controllers/api/blocks.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 { emptyArrayController, emptyObjectController, notImplementedController } from './controllers/api/fallback.ts';
import { instanceController } from './controllers/api/instance.ts'; import { instanceController } from './controllers/api/instance.ts';
import { mediaController } from './controllers/api/media.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/notifications', requirePubkey, notificationsController);
app.get('/api/v1/favourites', requirePubkey, favouritesController); app.get('/api/v1/favourites', requirePubkey, favouritesController);
app.get('/api/v1/bookmarks', requirePubkey, bookmarksController);
app.get('/api/v1/blocks', requirePubkey, blocksController); app.get('/api/v1/blocks', requirePubkey, blocksController);
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController); app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);
// Not (yet) implemented. // Not (yet) implemented.
app.get('/api/v1/bookmarks', emptyArrayController);
app.get('/api/v1/custom_emojis', emptyArrayController); app.get('/api/v1/custom_emojis', emptyArrayController);
app.get('/api/v1/filters', emptyArrayController); app.get('/api/v1/filters', emptyArrayController);
app.get('/api/v1/mutes', emptyArrayController); app.get('/api/v1/mutes', emptyArrayController);

View File

@ -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 };

View File

@ -1,38 +1,68 @@
import { AppContext } from '@/app.ts'; import { AppContext } from '@/app.ts';
import { eventsDB } from '@/db/events.ts'; import { eventsDB } from '@/db/events.ts';
import { type Filter } from '@/deps.ts'; import { type Filter } from '@/deps.ts';
import { getAuthor } from '@/queries.ts';
import { renderAccount } from '@/views/mastodon/accounts.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. */ /** Render account objects for the author of each event. */
async function renderEventAccounts(c: AppContext, filters: Filter[]) { async function renderEventAccounts(c: AppContext, filters: Filter[], signal = AbortSignal.timeout(1000)) {
const events = await eventsDB.getEvents(filters); if (!filters.length) {
return c.json([]);
}
const events = await eventsDB.getEvents(filters, { signal });
const pubkeys = new Set(events.map(({ pubkey }) => pubkey)); const pubkeys = new Set(events.map(({ pubkey }) => pubkey));
if (!pubkeys.size) { if (!pubkeys.size) {
return c.json([]); return c.json([]);
} }
const accounts = await Promise.all([...pubkeys].map(async (pubkey) => { const authors = await eventsDB.getEvents(
const author = await getAuthor(pubkey, { relations: ['author_stats'] }); [{ kinds: [0], authors: [...pubkeys], relations: ['author_stats'] }],
if (author) { { signal },
return renderAccount(author); );
}
})); const accounts = await Promise.all(
authors.map((event) => renderAccount(event)),
);
return paginated(c, events, accounts); return paginated(c, events, accounts);
} }
async function renderAccounts(c: AppContext, pubkeys: string[]) { async function renderAccounts(c: AppContext, authors: string[], signal = AbortSignal.timeout(1000)) {
// TODO: pagination by offset. const { since, until, limit } = paginationSchema.parse(c.req.query());
// 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;
}));
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 };