]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
Merge branch 'master' into release/3.3.0
[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 { isFollowStateValid } from '@server/helpers/custom-validators/follows'
4 import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
5 import { getServerActor } from '@server/models/application/application'
6 import { MActorFollowActorsDefault } from '@server/types/models'
7 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 import { isTestInstance } from '../../helpers/core-utils'
9 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
10 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
11 import { logger } from '../../helpers/logger'
12 import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
13 import { ActorModel } from '../../models/actor/actor'
14 import { ActorFollowModel } from '../../models/actor/actor-follow'
15 import { areValidationErrors } from './shared'
16
17 const listFollowsValidator = [
18 query('state')
19 .optional()
20 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
21 query('actorType')
22 .optional()
23 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 if (areValidationErrors(req, res)) return
27
28 return next()
29 }
30 ]
31
32 const followValidator = [
33 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
34
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 // Force https if the administrator wants to make friends
37 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
38 return res
39 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
40 .json({
41 error: 'Cannot follow on a non HTTPS web server.'
42 })
43 .end()
44 }
45
46 logger.debug('Checking follow parameters', { parameters: req.body })
47
48 if (areValidationErrors(req, res)) return
49
50 return next()
51 }
52 ]
53
54 const removeFollowingValidator = [
55 param('host').custom(isHostValid).withMessage('Should have a valid host'),
56
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking unfollowing parameters', { parameters: req.params })
59
60 if (areValidationErrors(req, res)) return
61
62 const serverActor = await getServerActor()
63 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
64
65 if (!follow) {
66 return res.fail({
67 status: HttpStatusCode.NOT_FOUND_404,
68 message: `Following ${req.params.host} not found.`
69 })
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.fail({
98 status: HttpStatusCode.NOT_FOUND_404,
99 message: `Follower ${req.params.nameWithHost} not found.`
100 })
101 }
102
103 res.locals.follow = follow
104 return next()
105 }
106 ]
107
108 const acceptOrRejectFollowerValidator = [
109 (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
111
112 const follow = res.locals.follow
113 if (follow.state !== 'pending') {
114 return res.fail({ message: 'Follow is not in pending state.' })
115 }
116
117 return next()
118 }
119 ]
120
121 // ---------------------------------------------------------------------------
122
123 export {
124 followValidator,
125 removeFollowingValidator,
126 getFollowerValidator,
127 acceptOrRejectFollowerValidator,
128 listFollowsValidator
129 }