]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Implemented configurable minimum signup age
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
54141398 1import * as express from 'express'
b8f4167f 2import { body, param, query } from 'express-validator'
7d9ba5c0
C
3import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
4import { getServerActor } from '@server/models/application/application'
5import { MActorFollowActorsDefault } from '@server/types/models'
6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
da854ddd 7import { isTestInstance } from '../../helpers/core-utils'
7d9ba5c0 8import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
50d6de9c 9import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd 10import { logger } from '../../helpers/logger'
7d9ba5c0 11import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
74dc3bca 12import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
7d9ba5c0
C
13import { ActorModel } from '../../models/actor/actor'
14import { ActorFollowModel } from '../../models/actor/actor-follow'
a2431b7d 15import { areValidationErrors } from './utils'
b8f4167f
C
16
17const listFollowsValidator = [
18 query('state')
19 .optional()
20 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
97ecddae
C
21 query('actorType')
22 .optional()
23 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
b8f4167f
C
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 if (areValidationErrors(req, res)) return
27
28 return next()
29 }
30]
54141398
C
31
32const 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
6dd9de95 37 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
2d53be02
RK
38 return res
39 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
54141398 40 .json({
48dce1c9 41 error: 'Cannot follow on a non HTTPS web server.'
54141398
C
42 })
43 .end()
44 }
45
46 logger.debug('Checking follow parameters', { parameters: req.body })
47
a2431b7d
C
48 if (areValidationErrors(req, res)) return
49
50 return next()
54141398
C
51 }
52]
53
54const removeFollowingValidator = [
50d6de9c 55 param('host').custom(isHostValid).withMessage('Should have a valid host'),
54141398 56
a2431b7d 57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0e9c48c2 58 logger.debug('Checking unfollowing parameters', { parameters: req.params })
54141398 59
a2431b7d 60 if (areValidationErrors(req, res)) return
54141398 61
50d6de9c 62 const serverActor = await getServerActor()
f37dc0dd 63 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
54141398 64
a2431b7d 65 if (!follow) {
eec63bbc 66 return res
2d53be02 67 .status(HttpStatusCode.NOT_FOUND_404)
eec63bbc 68 .json({
0e9c48c2
C
69 error: `Following ${req.params.host} not found.`
70 })
0e9c48c2
C
71 }
72
73 res.locals.follow = follow
74 return next()
75 }
76]
77
14893eb7 78const getFollowerValidator = [
0e9c48c2
C
79 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
80
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 82 logger.debug('Checking get follower parameters', { parameters: req.params })
0e9c48c2
C
83
84 if (areValidationErrors(req, res)) return
85
453e83ea 86 let follow: MActorFollowActorsDefault
5b9c965d
C
87 try {
88 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
89 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 90
5b9c965d
C
91 const serverActor = await getServerActor()
92 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
93 } catch (err) {
94 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
95 }
0e9c48c2
C
96
97 if (!follow) {
98 return res
2d53be02 99 .status(HttpStatusCode.NOT_FOUND_404)
0e9c48c2
C
100 .json({
101 error: `Follower ${req.params.nameWithHost} not found.`
eec63bbc 102 })
a2431b7d
C
103 .end()
104 }
54141398 105
a2431b7d
C
106 res.locals.follow = follow
107 return next()
54141398
C
108 }
109]
110
14893eb7
C
111const acceptOrRejectFollowerValidator = [
112 (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
114
115 const follow = res.locals.follow
116 if (follow.state !== 'pending') {
2d53be02
RK
117 return res
118 .status(HttpStatusCode.BAD_REQUEST_400)
119 .json({
120 error: 'Follow is not in pending state.'
121 })
122 .end()
14893eb7
C
123 }
124
125 return next()
126 }
127]
128
54141398
C
129// ---------------------------------------------------------------------------
130
131export {
132 followValidator,
0e9c48c2 133 removeFollowingValidator,
14893eb7 134 getFollowerValidator,
b8f4167f
C
135 acceptOrRejectFollowerValidator,
136 listFollowsValidator
54141398 137}