]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
605872ecf9c70a55f2d42fde15167ccde6c7a871
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isTestInstance } from '../../helpers/core-utils'
4 import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers'
5 import { logger } from '../../helpers/logger'
6 import { CONFIG, database as db } from '../../initializers'
7 import { areValidationErrors } from './utils'
8 import { getServerAccount } from '../../helpers/utils'
9 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
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 if (areValidationErrors(req, res)) return
27
28 return next()
29 }
30 ]
31
32 const removeFollowingValidator = [
33 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
34
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking unfollow parameters', { parameters: req.params })
37
38 if (areValidationErrors(req, res)) return
39
40 const serverAccount = await getServerAccount()
41 const follow = await db.AccountFollow.loadByAccountAndTarget(serverAccount.id, req.params.accountId)
42
43 if (!follow) {
44 return res.status(404)
45 .end()
46 }
47
48 res.locals.follow = follow
49 return next()
50 }
51 ]
52
53 // ---------------------------------------------------------------------------
54
55 export {
56 followValidator,
57 removeFollowingValidator
58 }