]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
Prevent logging an error on lazy static 404
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isProdInstance } from '@server/helpers/core-utils'
4 import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
5 import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
6 import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
7 import { getServerActor } from '@server/models/application/application'
8 import { MActorFollowActorsDefault } from '@server/types/models'
9 import { ServerFollowCreate } from '@shared/models'
10 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
11 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
12 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
13 import { logger } from '../../helpers/logger'
14 import { WEBSERVER } from '../../initializers/constants'
15 import { ActorModel } from '../../models/actor/actor'
16 import { ActorFollowModel } from '../../models/actor/actor-follow'
17 import { areValidationErrors } from './shared'
18
19 const 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
34 const 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
71 const 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({
85 actorId: serverActor.id,
86 targetName: name,
87 targetHost: host
88 })
89
90 if (!follow) {
91 return res.fail({
92 status: HttpStatusCode.NOT_FOUND_404,
93 message: `Follow ${req.params.hostOrHandle} not found.`
94 })
95 }
96
97 res.locals.follow = follow
98 return next()
99 }
100 ]
101
102 const getFollowerValidator = [
103 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
104
105 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
106 logger.debug('Checking get follower parameters', { parameters: req.params })
107
108 if (areValidationErrors(req, res)) return
109
110 let follow: MActorFollowActorsDefault
111 try {
112 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
113 const actor = await ActorModel.loadByUrl(actorUrl)
114
115 const serverActor = await getServerActor()
116 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
117 } catch (err) {
118 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
119 }
120
121 if (!follow) {
122 return res.fail({
123 status: HttpStatusCode.NOT_FOUND_404,
124 message: `Follower ${req.params.nameWithHost} not found.`
125 })
126 }
127
128 res.locals.follow = follow
129 return next()
130 }
131 ]
132
133 const acceptFollowerValidator = [
134 (req: express.Request, res: express.Response, next: express.NextFunction) => {
135 logger.debug('Checking accept follower parameters', { parameters: req.params })
136
137 const follow = res.locals.follow
138 if (follow.state !== 'pending' && follow.state !== 'rejected') {
139 return res.fail({ message: 'Follow is not in pending/rejected state.' })
140 }
141
142 return next()
143 }
144 ]
145
146 const rejectFollowerValidator = [
147 (req: express.Request, res: express.Response, next: express.NextFunction) => {
148 logger.debug('Checking reject follower parameters', { parameters: req.params })
149
150 const follow = res.locals.follow
151 if (follow.state !== 'pending' && follow.state !== 'accepted') {
152 return res.fail({ message: 'Follow is not in pending/accepted state.' })
153 }
154
155 return next()
156 }
157 ]
158
159 // ---------------------------------------------------------------------------
160
161 export {
162 followValidator,
163 removeFollowingValidator,
164 getFollowerValidator,
165 acceptFollowerValidator,
166 rejectFollowerValidator,
167 listFollowsValidator
168 }