]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
Use sequelize scopes
[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 { getServerAccount, isTestInstance, logger } from '../../helpers'
4 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
5 import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers'
6 import { CONFIG } from '../../initializers'
7 import { AccountFollowModel } from '../../models/account/account-follow'
8 import { areValidationErrors } from './utils'
9
10 const followValidator = [
11 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
12
13 (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 // Force https if the administrator wants to make friends
15 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
16 return res.status(400)
17 .json({
18 error: 'Cannot follow non HTTPS web server.'
19 })
20 .end()
21 }
22
23 logger.debug('Checking follow parameters', { parameters: req.body })
24
25 if (areValidationErrors(req, res)) return
26
27 return next()
28 }
29 ]
30
31 const removeFollowingValidator = [
32 param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'),
33
34 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking unfollow parameters', { parameters: req.params })
36
37 if (areValidationErrors(req, res)) return
38
39 const serverAccount = await getServerAccount()
40 const follow = await AccountFollowModel.loadByAccountAndTarget(serverAccount.id, req.params.accountId)
41
42 if (!follow) {
43 return res.status(404)
44 .end()
45 }
46
47 res.locals.follow = follow
48 return next()
49 }
50 ]
51
52 // ---------------------------------------------------------------------------
53
54 export {
55 followValidator,
56 removeFollowingValidator
57 }