diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-08 11:52:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-08 11:52:43 +0200 |
commit | 0e9c48c2edbb3871b0ca3ccd6718f2c99f9760b6 (patch) | |
tree | 6ec1d1c4262811066224eff8c4a55865fdaa9d08 /server/middlewares/validators/follows.ts | |
parent | ae9bbed46dbc8d9870c9feb66bbada484c1c7582 (diff) | |
download | PeerTube-0e9c48c2edbb3871b0ca3ccd6718f2c99f9760b6.tar.gz PeerTube-0e9c48c2edbb3871b0ca3ccd6718f2c99f9760b6.tar.zst PeerTube-0e9c48c2edbb3871b0ca3ccd6718f2c99f9760b6.zip |
Add ability to remove an instance follower in API
Diffstat (limited to 'server/middlewares/validators/follows.ts')
-rw-r--r-- | server/middlewares/validators/follows.ts | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 73fa28be9..ef4151efe 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts | |||
@@ -7,6 +7,10 @@ import { getServerActor } from '../../helpers/utils' | |||
7 | import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers' | 7 | import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers' |
8 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | 8 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
9 | import { areValidationErrors } from './utils' | 9 | import { areValidationErrors } from './utils' |
10 | import { ActorModel } from '../../models/activitypub/actor' | ||
11 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | ||
12 | import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub' | ||
13 | import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | ||
10 | 14 | ||
11 | const followValidator = [ | 15 | const followValidator = [ |
12 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | 16 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), |
@@ -33,7 +37,7 @@ const removeFollowingValidator = [ | |||
33 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | 37 | param('host').custom(isHostValid).withMessage('Should have a valid host'), |
34 | 38 | ||
35 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 39 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
36 | logger.debug('Checking unfollow parameters', { parameters: req.params }) | 40 | logger.debug('Checking unfollowing parameters', { parameters: req.params }) |
37 | 41 | ||
38 | if (areValidationErrors(req, res)) return | 42 | if (areValidationErrors(req, res)) return |
39 | 43 | ||
@@ -44,7 +48,36 @@ const removeFollowingValidator = [ | |||
44 | return res | 48 | return res |
45 | .status(404) | 49 | .status(404) |
46 | .json({ | 50 | .json({ |
47 | error: `Follower ${req.params.host} not found.` | 51 | error: `Following ${req.params.host} not found.` |
52 | }) | ||
53 | .end() | ||
54 | } | ||
55 | |||
56 | res.locals.follow = follow | ||
57 | return next() | ||
58 | } | ||
59 | ] | ||
60 | |||
61 | const removeFollowerValidator = [ | ||
62 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), | ||
63 | |||
64 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
65 | logger.debug('Checking remove follower parameters', { parameters: req.params }) | ||
66 | |||
67 | if (areValidationErrors(req, res)) return | ||
68 | |||
69 | const serverActor = await getServerActor() | ||
70 | |||
71 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | ||
72 | const actor = await ActorModel.loadByUrl(actorUrl) | ||
73 | |||
74 | const follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | ||
75 | |||
76 | if (!follow) { | ||
77 | return res | ||
78 | .status(404) | ||
79 | .json({ | ||
80 | error: `Follower ${req.params.nameWithHost} not found.` | ||
48 | }) | 81 | }) |
49 | .end() | 82 | .end() |
50 | } | 83 | } |
@@ -58,5 +91,6 @@ const removeFollowingValidator = [ | |||
58 | 91 | ||
59 | export { | 92 | export { |
60 | followValidator, | 93 | followValidator, |
61 | removeFollowingValidator | 94 | removeFollowingValidator, |
95 | removeFollowerValidator | ||
62 | } | 96 | } |