aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/follows.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/follows.ts')
-rw-r--r--server/middlewares/validators/follows.ts39
1 files changed, 30 insertions, 9 deletions
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts
index 205baca48..16abdd096 100644
--- a/server/middlewares/validators/follows.ts
+++ b/server/middlewares/validators/follows.ts
@@ -1,18 +1,20 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param, query } from 'express-validator' 2import { body, param, query } from 'express-validator'
3import { isFollowStateValid } from '@server/helpers/custom-validators/follows' 3import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
4import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors' 4import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
5import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
5import { getServerActor } from '@server/models/application/application' 6import { getServerActor } from '@server/models/application/application'
6import { MActorFollowActorsDefault } from '@server/types/models' 7import { MActorFollowActorsDefault } from '@server/types/models'
7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 8import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
8import { isTestInstance } from '../../helpers/core-utils' 9import { isTestInstance } from '../../helpers/core-utils'
9import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' 10import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
10import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' 11import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
11import { logger } from '../../helpers/logger' 12import { logger } from '../../helpers/logger'
12import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' 13import { WEBSERVER } from '../../initializers/constants'
13import { ActorModel } from '../../models/actor/actor' 14import { ActorModel } from '../../models/actor/actor'
14import { ActorFollowModel } from '../../models/actor/actor-follow' 15import { ActorFollowModel } from '../../models/actor/actor-follow'
15import { areValidationErrors } from './shared' 16import { areValidationErrors } from './shared'
17import { ServerFollowCreate } from '@shared/models'
16 18
17const listFollowsValidator = [ 19const listFollowsValidator = [
18 query('state') 20 query('state')
@@ -30,29 +32,46 @@ const listFollowsValidator = [
30] 32]
31 33
32const followValidator = [ 34const followValidator = [
33 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), 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'),
34 42
35 (req: express.Request, res: express.Response, next: express.NextFunction) => { 43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 // Force https if the administrator wants to make friends 44 // Force https if the administrator wants to follow remote actors
37 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') { 45 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
38 return res 46 return res
39 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500) 47 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
40 .json({ 48 .json({
41 error: 'Cannot follow on a non HTTPS web server.' 49 error: 'Cannot follow on a non HTTPS web server.'
42 }) 50 })
43 .end()
44 } 51 }
45 52
46 logger.debug('Checking follow parameters', { parameters: req.body }) 53 logger.debug('Checking follow parameters', { parameters: req.body })
47 54
48 if (areValidationErrors(req, res)) return 55 if (areValidationErrors(req, res)) return
49 56
57 const body: ServerFollowCreate = req.body
58 if (body.hosts.length === 0 && body.handles.length === 0) {
59
60 return res
61 .status(HttpStatusCode.BAD_REQUEST_400)
62 .json({
63 error: 'You must provide at least one handle or one host.'
64 })
65 }
66
50 return next() 67 return next()
51 } 68 }
52] 69]
53 70
54const removeFollowingValidator = [ 71const removeFollowingValidator = [
55 param('host').custom(isHostValid).withMessage('Should have a valid host'), 72 param('hostOrHandle')
73 .custom(value => isHostValid(value) || isRemoteHandleValid(value))
74 .withMessage('Should have a valid host/handle'),
56 75
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking unfollowing parameters', { parameters: req.params }) 77 logger.debug('Checking unfollowing parameters', { parameters: req.params })
@@ -60,12 +79,14 @@ const removeFollowingValidator = [
60 if (areValidationErrors(req, res)) return 79 if (areValidationErrors(req, res)) return
61 80
62 const serverActor = await getServerActor() 81 const serverActor = await getServerActor()
63 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host) 82
83 const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle)
84 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, name, host)
64 85
65 if (!follow) { 86 if (!follow) {
66 return res.fail({ 87 return res.fail({
67 status: HttpStatusCode.NOT_FOUND_404, 88 status: HttpStatusCode.NOT_FOUND_404,
68 message: `Following ${req.params.host} not found.` 89 message: `Follow ${req.params.hostOrHandle} not found.`
69 }) 90 })
70 } 91 }
71 92