From 927fa4b11f692174d6296aa096d7a74bacdeea8b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 26 Jul 2022 14:46:15 +0200 Subject: Add rejected state to follows Prevent reprocessing already rejected follows --- server/controllers/api/server/follows.ts | 34 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 37e8d88f7..60d36ed59 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -1,5 +1,6 @@ import express from 'express' import { getServerActor } from '@server/models/application/application' +import { ServerFollowCreate } from '@shared/models' import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' import { UserRight } from '../../../../shared/models/users' import { logger } from '../../../helpers/logger' @@ -20,16 +21,16 @@ import { setDefaultSort } from '../../../middlewares' import { - acceptOrRejectFollowerValidator, - instanceFollowersSortValidator, - instanceFollowingSortValidator, + acceptFollowerValidator, followValidator, getFollowerValidator, + instanceFollowersSortValidator, + instanceFollowingSortValidator, listFollowsValidator, + rejectFollowerValidator, removeFollowingValidator } from '../../../middlewares/validators' import { ActorFollowModel } from '../../../models/actor/actor-follow' -import { ServerFollowCreate } from '@shared/models' const serverFollowsRouter = express.Router() serverFollowsRouter.get('/following', @@ -69,22 +70,22 @@ serverFollowsRouter.delete('/followers/:nameWithHost', authenticate, ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), asyncMiddleware(getFollowerValidator), - asyncMiddleware(removeOrRejectFollower) + asyncMiddleware(removeFollower) ) serverFollowsRouter.post('/followers/:nameWithHost/reject', authenticate, ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), asyncMiddleware(getFollowerValidator), - acceptOrRejectFollowerValidator, - asyncMiddleware(removeOrRejectFollower) + rejectFollowerValidator, + asyncMiddleware(rejectFollower) ) serverFollowsRouter.post('/followers/:nameWithHost/accept', authenticate, ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), asyncMiddleware(getFollowerValidator), - acceptOrRejectFollowerValidator, + acceptFollowerValidator, asyncMiddleware(acceptFollower) ) @@ -176,10 +177,23 @@ async function removeFollowing (req: express.Request, res: express.Response) { return res.status(HttpStatusCode.NO_CONTENT_204).end() } -async function removeOrRejectFollower (req: express.Request, res: express.Response) { +async function rejectFollower (req: express.Request, res: express.Response) { const follow = res.locals.follow - await sendReject(follow.url, follow.ActorFollower, follow.ActorFollowing) + follow.state = 'rejected' + await follow.save() + + sendReject(follow.url, follow.ActorFollower, follow.ActorFollowing) + + return res.status(HttpStatusCode.NO_CONTENT_204).end() +} + +async function removeFollower (req: express.Request, res: express.Response) { + const follow = res.locals.follow + + if (follow.state === 'accepted' || follow.state === 'pending') { + sendReject(follow.url, follow.ActorFollower, follow.ActorFollowing) + } await follow.destroy() -- cgit v1.2.3