]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/follows.ts
/!\ Use a dedicated config file for development
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
... / ...
CommitLineData
1import express from 'express'
2import { body, param, query } from 'express-validator'
3import { isProdInstance } from '@server/helpers/core-utils'
4import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
5import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
6import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
7import { getServerActor } from '@server/models/application/application'
8import { MActorFollowActorsDefault } from '@server/types/models'
9import { ServerFollowCreate } from '@shared/models'
10import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
11import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
12import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
13import { logger } from '../../helpers/logger'
14import { WEBSERVER } from '../../initializers/constants'
15import { ActorModel } from '../../models/actor/actor'
16import { ActorFollowModel } from '../../models/actor/actor-follow'
17import { areValidationErrors } from './shared'
18
19const listFollowsValidator = [
20 query('state')
21 .optional()
22 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
23 query('actorType')
24 .optional()
25 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
26
27 (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 if (areValidationErrors(req, res)) return
29
30 return next()
31 }
32]
33
34const followValidator = [
35 body('hosts')
36 .toArray()
37 .custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
38
39 body('handles')
40 .toArray()
41 .custom(isEachUniqueHandleValid).withMessage('Should have an array of handles'),
42
43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 // Force https if the administrator wants to follow remote actors
45 if (isProdInstance() && WEBSERVER.SCHEME === 'http') {
46 return res
47 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
48 .json({
49 error: 'Cannot follow on a non HTTPS web server.'
50 })
51 }
52
53 logger.debug('Checking follow parameters', { parameters: req.body })
54
55 if (areValidationErrors(req, res)) return
56
57 const body: ServerFollowCreate = req.body
58 if (body.hosts.length === 0 && body.handles.length === 0) {
59
60 return res
61 .status(HttpStatusCode.BAD_REQUEST_400)
62 .json({
63 error: 'You must provide at least one handle or one host.'
64 })
65 }
66
67 return next()
68 }
69]
70
71const removeFollowingValidator = [
72 param('hostOrHandle')
73 .custom(value => isHostValid(value) || isRemoteHandleValid(value))
74 .withMessage('Should have a valid host/handle'),
75
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking unfollowing parameters', { parameters: req.params })
78
79 if (areValidationErrors(req, res)) return
80
81 const serverActor = await getServerActor()
82
83 const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle)
84 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, name, host)
85
86 if (!follow) {
87 return res.fail({
88 status: HttpStatusCode.NOT_FOUND_404,
89 message: `Follow ${req.params.hostOrHandle} not found.`
90 })
91 }
92
93 res.locals.follow = follow
94 return next()
95 }
96]
97
98const getFollowerValidator = [
99 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
100
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
102 logger.debug('Checking get follower parameters', { parameters: req.params })
103
104 if (areValidationErrors(req, res)) return
105
106 let follow: MActorFollowActorsDefault
107 try {
108 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
109 const actor = await ActorModel.loadByUrl(actorUrl)
110
111 const serverActor = await getServerActor()
112 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
113 } catch (err) {
114 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
115 }
116
117 if (!follow) {
118 return res.fail({
119 status: HttpStatusCode.NOT_FOUND_404,
120 message: `Follower ${req.params.nameWithHost} not found.`
121 })
122 }
123
124 res.locals.follow = follow
125 return next()
126 }
127]
128
129const acceptOrRejectFollowerValidator = [
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
131 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
132
133 const follow = res.locals.follow
134 if (follow.state !== 'pending') {
135 return res.fail({ message: 'Follow is not in pending state.' })
136 }
137
138 return next()
139 }
140]
141
142// ---------------------------------------------------------------------------
143
144export {
145 followValidator,
146 removeFollowingValidator,
147 getFollowerValidator,
148 acceptOrRejectFollowerValidator,
149 listFollowsValidator
150}