Add /api/v1/favourites

This commit is contained in:
Alex Gleason 2023-09-01 13:14:27 -05:00
parent 7c2de9b2cf
commit 4ff27ddc04
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 23 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import {
accountSearchController,
accountStatusesController,
createAccountController,
favouritesController,
followController,
followersController,
followingController,
@ -133,6 +134,7 @@ app.get('/api/v1/trends/tags', trendingTagsController);
app.get('/api/v1/trends', trendingTagsController);
app.get('/api/v1/notifications', requirePubkey, notificationsController);
app.get('/api/v1/favourites', requirePubkey, favouritesController);
// Not (yet) implemented.
app.get('/api/v1/bookmarks', emptyArrayController);
@ -144,7 +146,6 @@ app.get('/api/v1/mutes', emptyArrayController);
app.get('/api/v1/domain_blocks', emptyArrayController);
app.get('/api/v1/markers', emptyObjectController);
app.get('/api/v1/conversations', emptyArrayController);
app.get('/api/v1/favourites', emptyArrayController);
app.get('/api/v1/lists', emptyArrayController);
app.get('/', indexController);

View File

@ -5,7 +5,7 @@ import { getAuthor, getFollowedPubkeys, getFollows, syncUser } from '@/queries.t
import { booleanParamSchema } from '@/schema.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { toAccount, toRelationship, toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { isFollowing, lookupAccount } from '@/utils.ts';
import { isFollowing, lookupAccount, Time } from '@/utils.ts';
import { paginated, paginationSchema, parseBody } from '@/utils/web.ts';
import { createEvent } from '@/utils/web.ts';
import { renderEventAccounts } from '@/views.ts';
@ -193,12 +193,32 @@ const followingController: AppController = async (c) => {
return c.json(accounts.filter(Boolean));
};
const favouritesController: AppController = async (c) => {
const pubkey = c.get('pubkey')!;
const params = paginationSchema.parse(c.req.query());
const events7 = await mixer.getFilters(
[{ kinds: [7], authors: [pubkey], ...params }],
{ timeout: Time.seconds(1) },
);
const ids = events7
.map((event) => event.tags.find((tag) => tag[0] === 'e')?.[1])
.filter((id): id is string => !!id);
const events1 = await mixer.getFilters([{ kinds: [1], ids }], { timeout: Time.seconds(1) });
const statuses = await Promise.all(events1.map((event) => toStatus(event, c.get('pubkey'))));
return paginated(c, events1, statuses);
};
export {
accountController,
accountLookupController,
accountSearchController,
accountStatusesController,
createAccountController,
favouritesController,
followController,
followersController,
followingController,