Begin moving video channel to actor
[oweals/peertube.git] / server / middlewares / validators / follows.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { getServerActor, isTestInstance, logger } from '../../helpers'
4 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
5 import { CONFIG } from '../../initializers'
6 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
7 import { areValidationErrors } from './utils'
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
24     if (areValidationErrors(req, res)) return
25
26     return next()
27   }
28 ]
29
30 const removeFollowingValidator = [
31   param('host').custom(isHostValid).withMessage('Should have a valid host'),
32
33   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34     logger.debug('Checking unfollow parameters', { parameters: req.params })
35
36     if (areValidationErrors(req, res)) return
37
38     const serverActor = await getServerActor()
39     const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host)
40
41     if (!follow) {
42       return res.status(404)
43         .end()
44     }
45
46     res.locals.follow = follow
47     return next()
48   }
49 ]
50
51 // ---------------------------------------------------------------------------
52
53 export {
54   followValidator,
55   removeFollowingValidator
56 }