]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
/!\ Use a dedicated config file for development
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
41fb13c3 1import express from 'express'
b8f4167f 2import { body, param, query } from 'express-validator'
9452d4fd 3import { isProdInstance } from '@server/helpers/core-utils'
4d029ef8 4import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
10363c74 5import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
4d029ef8 6import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
7d9ba5c0
C
7import { getServerActor } from '@server/models/application/application'
8import { MActorFollowActorsDefault } from '@server/types/models'
9452d4fd 9import { ServerFollowCreate } from '@shared/models'
c0e8b12e 10import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
7d9ba5c0 11import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
50d6de9c 12import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd 13import { logger } from '../../helpers/logger'
4d029ef8 14import { WEBSERVER } from '../../initializers/constants'
7d9ba5c0
C
15import { ActorModel } from '../../models/actor/actor'
16import { ActorFollowModel } from '../../models/actor/actor-follow'
10363c74 17import { areValidationErrors } from './shared'
b8f4167f
C
18
19const listFollowsValidator = [
20 query('state')
21 .optional()
22 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
97ecddae
C
23 query('actorType')
24 .optional()
25 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
b8f4167f
C
26
27 (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 if (areValidationErrors(req, res)) return
29
30 return next()
31 }
32]
54141398
C
33
34const followValidator = [
4d029ef8
C
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'),
54141398
C
42
43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
4d029ef8 44 // Force https if the administrator wants to follow remote actors
9452d4fd 45 if (isProdInstance() && WEBSERVER.SCHEME === 'http') {
2d53be02
RK
46 return res
47 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
54141398 48 .json({
48dce1c9 49 error: 'Cannot follow on a non HTTPS web server.'
54141398 50 })
54141398
C
51 }
52
53 logger.debug('Checking follow parameters', { parameters: req.body })
54
a2431b7d
C
55 if (areValidationErrors(req, res)) return
56
4d029ef8
C
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
a2431b7d 67 return next()
54141398
C
68 }
69]
70
71const removeFollowingValidator = [
4d029ef8
C
72 param('hostOrHandle')
73 .custom(value => isHostValid(value) || isRemoteHandleValid(value))
74 .withMessage('Should have a valid host/handle'),
54141398 75
a2431b7d 76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0e9c48c2 77 logger.debug('Checking unfollowing parameters', { parameters: req.params })
54141398 78
a2431b7d 79 if (areValidationErrors(req, res)) return
54141398 80
50d6de9c 81 const serverActor = await getServerActor()
4d029ef8
C
82
83 const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle)
84 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, name, host)
54141398 85
a2431b7d 86 if (!follow) {
76148b27
RK
87 return res.fail({
88 status: HttpStatusCode.NOT_FOUND_404,
4d029ef8 89 message: `Follow ${req.params.hostOrHandle} not found.`
76148b27 90 })
0e9c48c2
C
91 }
92
93 res.locals.follow = follow
94 return next()
95 }
96]
97
14893eb7 98const getFollowerValidator = [
0e9c48c2
C
99 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
100
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 102 logger.debug('Checking get follower parameters', { parameters: req.params })
0e9c48c2
C
103
104 if (areValidationErrors(req, res)) return
105
453e83ea 106 let follow: MActorFollowActorsDefault
5b9c965d
C
107 try {
108 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
109 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 110
5b9c965d
C
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 }
0e9c48c2
C
116
117 if (!follow) {
76148b27
RK
118 return res.fail({
119 status: HttpStatusCode.NOT_FOUND_404,
120 message: `Follower ${req.params.nameWithHost} not found.`
121 })
a2431b7d 122 }
54141398 123
a2431b7d
C
124 res.locals.follow = follow
125 return next()
54141398
C
126 }
127]
128
14893eb7
C
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') {
76148b27 135 return res.fail({ message: 'Follow is not in pending state.' })
14893eb7
C
136 }
137
138 return next()
139 }
140]
141
54141398
C
142// ---------------------------------------------------------------------------
143
144export {
145 followValidator,
0e9c48c2 146 removeFollowingValidator,
14893eb7 147 getFollowerValidator,
b8f4167f
C
148 acceptOrRejectFollowerValidator,
149 listFollowsValidator
54141398 150}