]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import * as express from 'express' |
7e9334c3 | 2 | import { body, param } from 'express-validator/check' |
54141398 | 3 | import { isTestInstance } from '../../helpers/core-utils' |
54141398 C |
4 | import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' |
5 | import { logger } from '../../helpers/logger' | |
6 | import { CONFIG, database as db } from '../../initializers' | |
7 | import { checkErrors } from './utils' | |
8 | import { getServerAccount } from '../../helpers/utils' | |
7e9334c3 | 9 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
54141398 C |
10 | |
11 | const followValidator = [ | |
12 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | |
13 | ||
14 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
15 | // Force https if the administrator wants to make friends | |
16 | if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { | |
17 | return res.status(400) | |
18 | .json({ | |
19 | error: 'Cannot follow non HTTPS web server.' | |
20 | }) | |
21 | .end() | |
22 | } | |
23 | ||
24 | logger.debug('Checking follow parameters', { parameters: req.body }) | |
25 | ||
26 | checkErrors(req, res, next) | |
27 | } | |
28 | ] | |
29 | ||
30 | const removeFollowingValidator = [ | |
7e9334c3 | 31 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), |
54141398 C |
32 | |
33 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
0f91ae62 | 34 | logger.debug('Checking unfollow parameters', { parameters: req.params }) |
54141398 C |
35 | |
36 | checkErrors(req, res, async () => { | |
37 | try { | |
38 | const serverAccount = await getServerAccount() | |
0f91ae62 | 39 | const follow = await db.AccountFollow.loadByAccountAndTarget(serverAccount.id, req.params.accountId) |
54141398 | 40 | |
0f91ae62 | 41 | if (!follow) { |
54141398 C |
42 | return res.status(404) |
43 | .end() | |
44 | } | |
45 | ||
0f91ae62 | 46 | res.locals.follow = follow |
54141398 C |
47 | |
48 | return next() | |
49 | } catch (err) { | |
50 | logger.error('Error in remove following validator.', err) | |
51 | return res.sendStatus(500) | |
52 | } | |
53 | }) | |
54 | } | |
55 | ] | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
60 | followValidator, | |
61 | removeFollowingValidator | |
62 | } |