]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
7808135f73c18298205129ef65dc59ef35e310a5
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isTestInstance } from '../../helpers/core-utils'
4 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
5 import { logger } from '../../helpers/logger'
6 import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
7 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
8 import { areValidationErrors } from './utils'
9 import { ActorModel } from '../../models/activitypub/actor'
10 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
11 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
12 import { MActorFollowActorsDefault } from '@server/typings/models'
13 import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
14 import { getServerActor } from '@server/models/application/application'
15
16 const listFollowsValidator = [
17 query('state')
18 .optional()
19 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
20 query('actorType')
21 .optional()
22 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
23
24 (req: express.Request, res: express.Response, next: express.NextFunction) => {
25 if (areValidationErrors(req, res)) return
26
27 return next()
28 }
29 ]
30
31 const 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
36 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
37 return res.status(500)
38 .json({
39 error: 'Cannot follow on a non HTTPS web server.'
40 })
41 .end()
42 }
43
44 logger.debug('Checking follow parameters', { parameters: req.body })
45
46 if (areValidationErrors(req, res)) return
47
48 return next()
49 }
50 ]
51
52 const removeFollowingValidator = [
53 param('host').custom(isHostValid).withMessage('Should have a valid host'),
54
55 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
56 logger.debug('Checking unfollowing parameters', { parameters: req.params })
57
58 if (areValidationErrors(req, res)) return
59
60 const serverActor = await getServerActor()
61 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
62
63 if (!follow) {
64 return res
65 .status(404)
66 .json({
67 error: `Following ${req.params.host} not found.`
68 })
69 .end()
70 }
71
72 res.locals.follow = follow
73 return next()
74 }
75 ]
76
77 const getFollowerValidator = [
78 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
79
80 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 logger.debug('Checking get follower parameters', { parameters: req.params })
82
83 if (areValidationErrors(req, res)) return
84
85 let follow: MActorFollowActorsDefault
86 try {
87 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
88 const actor = await ActorModel.loadByUrl(actorUrl)
89
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 }
95
96 if (!follow) {
97 return res
98 .status(404)
99 .json({
100 error: `Follower ${req.params.nameWithHost} not found.`
101 })
102 .end()
103 }
104
105 res.locals.follow = follow
106 return next()
107 }
108 ]
109
110 const 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
123 // ---------------------------------------------------------------------------
124
125 export {
126 followValidator,
127 removeFollowingValidator,
128 getFollowerValidator,
129 acceptOrRejectFollowerValidator,
130 listFollowsValidator
131 }