From 3807ca175f2541c36b6f07b6c2ff4690bd2672b2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 1 Jan 2024 12:50:09 -0600 Subject: [PATCH] Add unblock and unfollow endpoints --- src/app.ts | 8 ++++++-- src/controllers/api/accounts.ts | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index 59ee717..2c73369 100644 --- a/src/app.ts +++ b/src/app.ts @@ -30,6 +30,8 @@ import { followersController, followingController, relationshipsController, + unblockController, + unfollowController, updateCredentialsController, verifyCredentialsController, } from './controllers/api/accounts.ts'; @@ -137,8 +139,10 @@ app.patch( app.get('/api/v1/accounts/search', accountSearchController); app.get('/api/v1/accounts/lookup', accountLookupController); app.get('/api/v1/accounts/relationships', relationshipsController); -app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/block', blockController); -app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/follow', followController); +app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/block', requirePubkey, blockController); +app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/unblock', requirePubkey, unblockController); +app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/follow', requirePubkey, followController); +app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/unfollow', requirePubkey, unfollowController); app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/followers', followersController); app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/following', followingController); app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/statuses', accountStatusesController); diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 7486b02..b25dbe4 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -7,7 +7,7 @@ import { type DittoFilter } from '@/filter.ts'; import { getAuthor, getFollowedPubkeys } from '@/queries.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; -import { addTag } from '@/tags.ts'; +import { addTag, deleteTag } from '@/tags.ts'; import { uploadFile } from '@/upload.ts'; import { lookupAccount, nostrNow } from '@/utils.ts'; import { paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/web.ts'; @@ -213,6 +213,7 @@ const updateCredentialsController: AppController = async (c) => { return c.json(account); }; +/** https://docs.joinmastodon.org/methods/accounts/#follow */ const followController: AppController = async (c) => { const sourcePubkey = c.get('pubkey')!; const targetPubkey = c.req.param('pubkey'); @@ -227,6 +228,21 @@ const followController: AppController = async (c) => { return c.json(relationship); }; +/** https://docs.joinmastodon.org/methods/accounts/#unfollow */ +const unfollowController: AppController = async (c) => { + const sourcePubkey = c.get('pubkey')!; + const targetPubkey = c.req.param('pubkey'); + + await updateListEvent( + { kinds: [3], authors: [sourcePubkey] }, + (tags) => deleteTag(tags, ['p', targetPubkey]), + c, + ); + + const relationship = await renderRelationship(sourcePubkey, targetPubkey); + return c.json(relationship); +}; + const followersController: AppController = (c) => { const pubkey = c.req.param('pubkey'); const params = paginationSchema.parse(c.req.query()); @@ -254,6 +270,21 @@ const blockController: AppController = async (c) => { return c.json(relationship); }; +/** https://docs.joinmastodon.org/methods/accounts/#unblock */ +const unblockController: AppController = async (c) => { + const sourcePubkey = c.get('pubkey')!; + const targetPubkey = c.req.param('pubkey'); + + await updateListEvent( + { kinds: [10000], authors: [sourcePubkey] }, + (tags) => deleteTag(tags, ['p', targetPubkey]), + c, + ); + + const relationship = await renderRelationship(sourcePubkey, targetPubkey); + return c.json(relationship); +}; + const favouritesController: AppController = async (c) => { const pubkey = c.get('pubkey')!; const params = paginationSchema.parse(c.req.query()); @@ -290,6 +321,8 @@ export { followersController, followingController, relationshipsController, + unblockController, + unfollowController, updateCredentialsController, verifyCredentialsController, };