]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Translated using Weblate (Arabic)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
54141398 1import * as express from 'express'
b8f4167f 2import { body, param, query } from 'express-validator'
da854ddd 3import { isTestInstance } from '../../helpers/core-utils'
50d6de9c 4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd 5import { logger } from '../../helpers/logger'
74dc3bca 6import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
50d6de9c 7import { ActorFollowModel } from '../../models/activitypub/actor-follow'
a2431b7d 8import { areValidationErrors } from './utils'
0e9c48c2
C
9import { ActorModel } from '../../models/activitypub/actor'
10import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
97ecddae 11import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
26d6bf65 12import { MActorFollowActorsDefault } from '@server/types/models'
b8f4167f 13import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
8dc8a34e 14import { getServerActor } from '@server/models/application/application'
2d53be02 15import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
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 })
71 .end()
72 }
73
74 res.locals.follow = follow
75 return next()
76 }
77]
78
14893eb7 79const getFollowerValidator = [
0e9c48c2
C
80 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
81
82 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 83 logger.debug('Checking get follower parameters', { parameters: req.params })
0e9c48c2
C
84
85 if (areValidationErrors(req, res)) return
86
453e83ea 87 let follow: MActorFollowActorsDefault
5b9c965d
C
88 try {
89 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
90 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 91
5b9c965d
C
92 const serverActor = await getServerActor()
93 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
94 } catch (err) {
95 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
96 }
0e9c48c2
C
97
98 if (!follow) {
99 return res
2d53be02 100 .status(HttpStatusCode.NOT_FOUND_404)
0e9c48c2
C
101 .json({
102 error: `Follower ${req.params.nameWithHost} not found.`
eec63bbc 103 })
a2431b7d
C
104 .end()
105 }
54141398 106
a2431b7d
C
107 res.locals.follow = follow
108 return next()
54141398
C
109 }
110]
111
14893eb7
C
112const acceptOrRejectFollowerValidator = [
113 (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
115
116 const follow = res.locals.follow
117 if (follow.state !== 'pending') {
2d53be02
RK
118 return res
119 .status(HttpStatusCode.BAD_REQUEST_400)
120 .json({
121 error: 'Follow is not in pending state.'
122 })
123 .end()
14893eb7
C
124 }
125
126 return next()
127 }
128]
129
54141398
C
130// ---------------------------------------------------------------------------
131
132export {
133 followValidator,
0e9c48c2 134 removeFollowingValidator,
14893eb7 135 getFollowerValidator,
b8f4167f
C
136 acceptOrRejectFollowerValidator,
137 listFollowsValidator
54141398 138}