]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/follows.ts
Implement signup approval in server
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / follows.ts
CommitLineData
41fb13c3 1import express from 'express'
b8f4167f 2import { body, param, query } from 'express-validator'
9452d4fd 3import { isProdInstance } from '@server/helpers/core-utils'
4d029ef8 4import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
10363c74 5import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
4d029ef8 6import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
7d9ba5c0
C
7import { getServerActor } from '@server/models/application/application'
8import { MActorFollowActorsDefault } from '@server/types/models'
9452d4fd 9import { ServerFollowCreate } from '@shared/models'
c0e8b12e 10import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
7d9ba5c0 11import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
50d6de9c 12import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
da854ddd 13import { logger } from '../../helpers/logger'
4d029ef8 14import { WEBSERVER } from '../../initializers/constants'
7d9ba5c0
C
15import { ActorModel } from '../../models/actor/actor'
16import { ActorFollowModel } from '../../models/actor/actor-follow'
10363c74 17import { areValidationErrors } from './shared'
b8f4167f
C
18
19const listFollowsValidator = [
20 query('state')
21 .optional()
396f6f01 22 .custom(isFollowStateValid),
97ecddae
C
23 query('actorType')
24 .optional()
396f6f01 25 .custom(isActorTypeValid),
b8f4167f
C
26
27 (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 if (areValidationErrors(req, res)) return
29
30 return next()
31 }
32]
54141398
C
33
34const followValidator = [
4d029ef8
C
35 body('hosts')
36 .toArray()
37 .custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
38
39 body('handles')
40 .toArray()
41 .custom(isEachUniqueHandleValid).withMessage('Should have an array of handles'),
54141398
C
42
43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
4d029ef8 44 // Force https if the administrator wants to follow remote actors
9452d4fd 45 if (isProdInstance() && WEBSERVER.SCHEME === 'http') {
2d53be02
RK
46 return res
47 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
54141398 48 .json({
48dce1c9 49 error: 'Cannot follow on a non HTTPS web server.'
54141398 50 })
54141398
C
51 }
52
a2431b7d
C
53 if (areValidationErrors(req, res)) return
54
4d029ef8
C
55 const body: ServerFollowCreate = req.body
56 if (body.hosts.length === 0 && body.handles.length === 0) {
57
58 return res
59 .status(HttpStatusCode.BAD_REQUEST_400)
60 .json({
61 error: 'You must provide at least one handle or one host.'
62 })
63 }
64
a2431b7d 65 return next()
54141398
C
66 }
67]
68
69const removeFollowingValidator = [
4d029ef8 70 param('hostOrHandle')
396f6f01 71 .custom(value => isHostValid(value) || isRemoteHandleValid(value)),
54141398 72
a2431b7d 73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
a2431b7d 74 if (areValidationErrors(req, res)) return
54141398 75
50d6de9c 76 const serverActor = await getServerActor()
4d029ef8
C
77
78 const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle)
927fa4b1
C
79 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({
80 actorId: serverActor.id,
81 targetName: name,
82 targetHost: host
83 })
54141398 84
a2431b7d 85 if (!follow) {
76148b27
RK
86 return res.fail({
87 status: HttpStatusCode.NOT_FOUND_404,
4d029ef8 88 message: `Follow ${req.params.hostOrHandle} not found.`
76148b27 89 })
0e9c48c2
C
90 }
91
92 res.locals.follow = follow
93 return next()
94 }
95]
96
14893eb7 97const getFollowerValidator = [
396f6f01
C
98 param('nameWithHost')
99 .custom(isValidActorHandle),
0e9c48c2
C
100
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
0e9c48c2
C
102 if (areValidationErrors(req, res)) return
103
453e83ea 104 let follow: MActorFollowActorsDefault
5b9c965d
C
105 try {
106 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
107 const actor = await ActorModel.loadByUrl(actorUrl)
0e9c48c2 108
5b9c965d
C
109 const serverActor = await getServerActor()
110 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
111 } catch (err) {
112 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
113 }
0e9c48c2
C
114
115 if (!follow) {
76148b27
RK
116 return res.fail({
117 status: HttpStatusCode.NOT_FOUND_404,
118 message: `Follower ${req.params.nameWithHost} not found.`
119 })
a2431b7d 120 }
54141398 121
a2431b7d
C
122 res.locals.follow = follow
123 return next()
54141398
C
124 }
125]
126
927fa4b1 127const acceptFollowerValidator = [
14893eb7 128 (req: express.Request, res: express.Response, next: express.NextFunction) => {
14893eb7 129 const follow = res.locals.follow
927fa4b1
C
130 if (follow.state !== 'pending' && follow.state !== 'rejected') {
131 return res.fail({ message: 'Follow is not in pending/rejected state.' })
132 }
133
134 return next()
135 }
136]
137
138const rejectFollowerValidator = [
139 (req: express.Request, res: express.Response, next: express.NextFunction) => {
927fa4b1
C
140 const follow = res.locals.follow
141 if (follow.state !== 'pending' && follow.state !== 'accepted') {
142 return res.fail({ message: 'Follow is not in pending/accepted state.' })
14893eb7
C
143 }
144
145 return next()
146 }
147]
148
54141398
C
149// ---------------------------------------------------------------------------
150
151export {
152 followValidator,
0e9c48c2 153 removeFollowingValidator,
14893eb7 154 getFollowerValidator,
927fa4b1
C
155 acceptFollowerValidator,
156 rejectFollowerValidator,
b8f4167f 157 listFollowsValidator
54141398 158}