]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/follows.ts
Add ability to remove an instance follower in API
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isTestInstance } from '../../helpers/core-utils'
4 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
5 import { logger } from '../../helpers/logger'
6 import { getServerActor } from '../../helpers/utils'
7 import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers'
8 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
9 import { areValidationErrors } from './utils'
10 import { ActorModel } from '../../models/activitypub/actor'
11 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
12 import { getOrCreateActorAndServerAndModel } from '../../lib/activitypub'
13 import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
14
15 const 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
20 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
21 return res.status(500)
22 .json({
23 error: 'Cannot follow on a non HTTPS web server.'
24 })
25 .end()
26 }
27
28 logger.debug('Checking follow parameters', { parameters: req.body })
29
30 if (areValidationErrors(req, res)) return
31
32 return next()
33 }
34 ]
35
36 const removeFollowingValidator = [
37 param('host').custom(isHostValid).withMessage('Should have a valid host'),
38
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking unfollowing parameters', { parameters: req.params })
41
42 if (areValidationErrors(req, res)) return
43
44 const serverActor = await getServerActor()
45 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
46
47 if (!follow) {
48 return res
49 .status(404)
50 .json({
51 error: `Following ${req.params.host} not found.`
52 })
53 .end()
54 }
55
56 res.locals.follow = follow
57 return next()
58 }
59 ]
60
61 const removeFollowerValidator = [
62 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
63
64 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking remove follower parameters', { parameters: req.params })
66
67 if (areValidationErrors(req, res)) return
68
69 const serverActor = await getServerActor()
70
71 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
72 const actor = await ActorModel.loadByUrl(actorUrl)
73
74 const follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
75
76 if (!follow) {
77 return res
78 .status(404)
79 .json({
80 error: `Follower ${req.params.nameWithHost} not found.`
81 })
82 .end()
83 }
84
85 res.locals.follow = follow
86 return next()
87 }
88 ]
89
90 // ---------------------------------------------------------------------------
91
92 export {
93 followValidator,
94 removeFollowingValidator,
95 removeFollowerValidator
96 }