]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import * as express from 'express' |
7e9334c3 | 2 | import { body, param } from 'express-validator/check' |
50d6de9c C |
3 | import { getServerActor, isTestInstance, logger } from '../../helpers' |
4 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' | |
3fd3ab2d | 5 | import { CONFIG } from '../../initializers' |
50d6de9c | 6 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
a2431b7d | 7 | import { areValidationErrors } from './utils' |
54141398 C |
8 | |
9 | const followValidator = [ | |
10 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | |
11 | ||
12 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
13 | // Force https if the administrator wants to make friends | |
14 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { | |
15 | return res.status(400) | |
16 | .json({ | |
17 | error: 'Cannot follow non HTTPS web server.' | |
18 | }) | |
19 | .end() | |
20 | } | |
21 | ||
22 | logger.debug('Checking follow parameters', { parameters: req.body }) | |
23 | ||
a2431b7d C |
24 | if (areValidationErrors(req, res)) return |
25 | ||
26 | return next() | |
54141398 C |
27 | } |
28 | ] | |
29 | ||
30 | const removeFollowingValidator = [ | |
50d6de9c | 31 | param('host').custom(isHostValid).withMessage('Should have a valid host'), |
54141398 | 32 | |
a2431b7d | 33 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
0f91ae62 | 34 | logger.debug('Checking unfollow parameters', { parameters: req.params }) |
54141398 | 35 | |
a2431b7d | 36 | if (areValidationErrors(req, res)) return |
54141398 | 37 | |
50d6de9c C |
38 | const serverActor = await getServerActor() |
39 | const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host) | |
54141398 | 40 | |
a2431b7d C |
41 | if (!follow) { |
42 | return res.status(404) | |
43 | .end() | |
44 | } | |
54141398 | 45 | |
a2431b7d C |
46 | res.locals.follow = follow |
47 | return next() | |
54141398 C |
48 | } |
49 | ] | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | followValidator, | |
55 | removeFollowingValidator | |
56 | } |