]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
1d18de8cd9281fa1b787218a0021319519acbbb3
[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 { getServerActor } from '@server/models/application/application'
5 import { MActorFollowActorsDefault } from '@server/types/models'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 import { isTestInstance } from '../../helpers/core-utils'
8 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
9 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
10 import { logger } from '../../helpers/logger'
11 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
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 './utils'
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
67 .status(HttpStatusCode.NOT_FOUND_404)
68 .json({
69 error: `Following ${req.params.host} not found.`
70 })
71 }
72
73 res.locals.follow = follow
74 return next()
75 }
76 ]
77
78 const getFollowerValidator = [
79 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
80
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking get follower parameters', { parameters: req.params })
83
84 if (areValidationErrors(req, res)) return
85
86 let follow: MActorFollowActorsDefault
87 try {
88 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
89 const actor = await ActorModel.loadByUrl(actorUrl)
90
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 }
96
97 if (!follow) {
98 return res
99 .status(HttpStatusCode.NOT_FOUND_404)
100 .json({
101 error: `Follower ${req.params.nameWithHost} not found.`
102 })
103 .end()
104 }
105
106 res.locals.follow = follow
107 return next()
108 }
109 ]
110
111 const 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') {
117 return res
118 .status(HttpStatusCode.BAD_REQUEST_400)
119 .json({
120 error: 'Follow is not in pending state.'
121 })
122 .end()
123 }
124
125 return next()
126 }
127 ]
128
129 // ---------------------------------------------------------------------------
130
131 export {
132 followValidator,
133 removeFollowingValidator,
134 getFollowerValidator,
135 acceptOrRejectFollowerValidator,
136 listFollowsValidator
137 }