]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Stronger model typings
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
54141398 1import * as express from 'express'
c8861d5d 2import { body, param } from 'express-validator'
da854ddd 3import { isTestInstance } from '../../helpers/core-utils'
50d6de9c 4import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd
C
5import { logger } from '../../helpers/logger'
6import { getServerActor } from '../../helpers/utils'
74dc3bca 7import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
50d6de9c 8import { ActorFollowModel } from '../../models/activitypub/actor-follow'
a2431b7d 9import { areValidationErrors } from './utils'
0e9c48c2
C
10import { ActorModel } from '../../models/activitypub/actor'
11import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
0e9c48c2 12import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
453e83ea 13import { MActorFollowActorsDefault } from '@server/typings/models'
54141398
C
14
15const followValidator = [
16 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
17
18 (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 // Force https if the administrator wants to make friends
6dd9de95 20 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
a3c1738e 21 return res.status(500)
54141398 22 .json({
48dce1c9 23 error: 'Cannot follow on a non HTTPS web server.'
54141398
C
24 })
25 .end()
26 }
27
28 logger.debug('Checking follow parameters', { parameters: req.body })
29
a2431b7d
C
30 if (areValidationErrors(req, res)) return
31
32 return next()
54141398
C
33 }
34]
35
36const removeFollowingValidator = [
50d6de9c 37 param('host').custom(isHostValid).withMessage('Should have a valid host'),
54141398 38
a2431b7d 39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0e9c48c2 40 logger.debug('Checking unfollowing parameters', { parameters: req.params })
54141398 41
a2431b7d 42 if (areValidationErrors(req, res)) return
54141398 43
50d6de9c 44 const serverActor = await getServerActor()
f37dc0dd 45 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
54141398 46
a2431b7d 47 if (!follow) {
eec63bbc
C
48 return res
49 .status(404)
50 .json({
0e9c48c2
C
51 error: `Following ${req.params.host} not found.`
52 })
53 .end()
54 }
55
56 res.locals.follow = follow
57 return next()
58 }
59]
60
14893eb7 61const getFollowerValidator = [
0e9c48c2
C
62 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
63
64 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 65 logger.debug('Checking get follower parameters', { parameters: req.params })
0e9c48c2
C
66
67 if (areValidationErrors(req, res)) return
68
453e83ea 69 let follow: MActorFollowActorsDefault
5b9c965d
C
70 try {
71 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
72 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 73
5b9c965d
C
74 const serverActor = await getServerActor()
75 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
76 } catch (err) {
77 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
78 }
0e9c48c2
C
79
80 if (!follow) {
81 return res
82 .status(404)
83 .json({
84 error: `Follower ${req.params.nameWithHost} not found.`
eec63bbc 85 })
a2431b7d
C
86 .end()
87 }
54141398 88
a2431b7d
C
89 res.locals.follow = follow
90 return next()
54141398
C
91 }
92]
93
14893eb7
C
94const acceptOrRejectFollowerValidator = [
95 (req: express.Request, res: express.Response, next: express.NextFunction) => {
96 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
97
98 const follow = res.locals.follow
99 if (follow.state !== 'pending') {
100 return res.status(400).json({ error: 'Follow is not in pending state.' }).end()
101 }
102
103 return next()
104 }
105]
106
54141398
C
107// ---------------------------------------------------------------------------
108
109export {
110 followValidator,
0e9c48c2 111 removeFollowingValidator,
14893eb7
C
112 getFollowerValidator,
113 acceptOrRejectFollowerValidator
54141398 114}