]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Playlist server API
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
54141398 1import * as express from 'express'
7e9334c3 2import { body, param } from 'express-validator/check'
da854ddd 3import { isTestInstance } from '../../helpers/core-utils'
50d6de9c 4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd
C
5import { logger } from '../../helpers/logger'
6import { getServerActor } from '../../helpers/utils'
06a05d5f 7import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers'
50d6de9c 8import { ActorFollowModel } from '../../models/activitypub/actor-follow'
a2431b7d 9import { areValidationErrors } from './utils'
54141398
C
10
11const 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') {
a3c1738e 17 return res.status(500)
54141398 18 .json({
48dce1c9 19 error: 'Cannot follow on a non HTTPS web server.'
54141398
C
20 })
21 .end()
22 }
23
24 logger.debug('Checking follow parameters', { parameters: req.body })
25
a2431b7d
C
26 if (areValidationErrors(req, res)) return
27
28 return next()
54141398
C
29 }
30]
31
32const removeFollowingValidator = [
50d6de9c 33 param('host').custom(isHostValid).withMessage('Should have a valid host'),
54141398 34
a2431b7d 35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0f91ae62 36 logger.debug('Checking unfollow parameters', { parameters: req.params })
54141398 37
a2431b7d 38 if (areValidationErrors(req, res)) return
54141398 39
50d6de9c 40 const serverActor = await getServerActor()
f37dc0dd 41 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
54141398 42
a2431b7d 43 if (!follow) {
eec63bbc
C
44 return res
45 .status(404)
46 .json({
47 error: `Follower ${req.params.host} not found.`
48 })
a2431b7d
C
49 .end()
50 }
54141398 51
a2431b7d
C
52 res.locals.follow = follow
53 return next()
54141398
C
54 }
55]
56
57// ---------------------------------------------------------------------------
58
59export {
60 followValidator,
61 removeFollowingValidator
62}