]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
54141398 1import * as express from 'express'
b8f4167f 2import { body, param, query } from 'express-validator'
da854ddd 3import { isTestInstance } from '../../helpers/core-utils'
50d6de9c 4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd 5import { logger } from '../../helpers/logger'
74dc3bca 6import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
50d6de9c 7import { ActorFollowModel } from '../../models/activitypub/actor-follow'
a2431b7d 8import { areValidationErrors } from './utils'
0e9c48c2
C
9import { ActorModel } from '../../models/activitypub/actor'
10import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
97ecddae 11import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
26d6bf65 12import { MActorFollowActorsDefault } from '@server/types/models'
b8f4167f 13import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
8dc8a34e 14import { getServerActor } from '@server/models/application/application'
b8f4167f
C
15
16const listFollowsValidator = [
17 query('state')
18 .optional()
19 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
97ecddae
C
20 query('actorType')
21 .optional()
22 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
b8f4167f
C
23
24 (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 if (areValidationErrors(req, res)) return
26
27 return next()
28 }
29]
54141398
C
30
31const followValidator = [
32 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
33
34 (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 // Force https if the administrator wants to make friends
6dd9de95 36 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
a3c1738e 37 return res.status(500)
54141398 38 .json({
48dce1c9 39 error: 'Cannot follow on a non HTTPS web server.'
54141398
C
40 })
41 .end()
42 }
43
44 logger.debug('Checking follow parameters', { parameters: req.body })
45
a2431b7d
C
46 if (areValidationErrors(req, res)) return
47
48 return next()
54141398
C
49 }
50]
51
52const removeFollowingValidator = [
50d6de9c 53 param('host').custom(isHostValid).withMessage('Should have a valid host'),
54141398 54
a2431b7d 55 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0e9c48c2 56 logger.debug('Checking unfollowing parameters', { parameters: req.params })
54141398 57
a2431b7d 58 if (areValidationErrors(req, res)) return
54141398 59
50d6de9c 60 const serverActor = await getServerActor()
f37dc0dd 61 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
54141398 62
a2431b7d 63 if (!follow) {
eec63bbc
C
64 return res
65 .status(404)
66 .json({
0e9c48c2
C
67 error: `Following ${req.params.host} not found.`
68 })
69 .end()
70 }
71
72 res.locals.follow = follow
73 return next()
74 }
75]
76
14893eb7 77const getFollowerValidator = [
0e9c48c2
C
78 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
79
80 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 81 logger.debug('Checking get follower parameters', { parameters: req.params })
0e9c48c2
C
82
83 if (areValidationErrors(req, res)) return
84
453e83ea 85 let follow: MActorFollowActorsDefault
5b9c965d
C
86 try {
87 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
88 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 89
5b9c965d
C
90 const serverActor = await getServerActor()
91 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
92 } catch (err) {
93 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
94 }
0e9c48c2
C
95
96 if (!follow) {
97 return res
98 .status(404)
99 .json({
100 error: `Follower ${req.params.nameWithHost} not found.`
eec63bbc 101 })
a2431b7d
C
102 .end()
103 }
54141398 104
a2431b7d
C
105 res.locals.follow = follow
106 return next()
54141398
C
107 }
108]
109
14893eb7
C
110const acceptOrRejectFollowerValidator = [
111 (req: express.Request, res: express.Response, next: express.NextFunction) => {
112 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
113
114 const follow = res.locals.follow
115 if (follow.state !== 'pending') {
116 return res.status(400).json({ error: 'Follow is not in pending state.' }).end()
117 }
118
119 return next()
120 }
121]
122
54141398
C
123// ---------------------------------------------------------------------------
124
125export {
126 followValidator,
0e9c48c2 127 removeFollowingValidator,
14893eb7 128 getFollowerValidator,
b8f4167f
C
129 acceptOrRejectFollowerValidator,
130 listFollowsValidator
54141398 131}