aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/follows.ts62
-rw-r--r--server/middlewares/validators/index.ts2
-rw-r--r--server/middlewares/validators/servers.ts32
3 files changed, 63 insertions, 33 deletions
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts
new file mode 100644
index 000000000..e22349726
--- /dev/null
+++ b/server/middlewares/validators/follows.ts
@@ -0,0 +1,62 @@
1import * as express from 'express'
2import { body } from 'express-validator/check'
3import { isTestInstance } from '../../helpers/core-utils'
4import { isAccountIdValid } from '../../helpers/custom-validators/activitypub/account'
5import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers'
6import { logger } from '../../helpers/logger'
7import { CONFIG, database as db } from '../../initializers'
8import { checkErrors } from './utils'
9import { getServerAccount } from '../../helpers/utils'
10
11const followValidator = [
12 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
13
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 // Force https if the administrator wants to make friends
16 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
17 return res.status(400)
18 .json({
19 error: 'Cannot follow non HTTPS web server.'
20 })
21 .end()
22 }
23
24 logger.debug('Checking follow parameters', { parameters: req.body })
25
26 checkErrors(req, res, next)
27 }
28]
29
30const removeFollowingValidator = [
31 body('accountId').custom(isAccountIdValid).withMessage('Should have a valid account id'),
32
33 (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking follow parameters', { parameters: req.body })
35
36 checkErrors(req, res, async () => {
37 try {
38 const serverAccount = await getServerAccount()
39 const following = await db.AccountFollow.loadByAccountAndTarget(serverAccount.id, req.params.accountId)
40
41 if (!following) {
42 return res.status(404)
43 .end()
44 }
45
46 res.locals.following = following
47
48 return next()
49 } catch (err) {
50 logger.error('Error in remove following validator.', err)
51 return res.sendStatus(500)
52 }
53 })
54 }
55]
56
57// ---------------------------------------------------------------------------
58
59export {
60 followValidator,
61 removeFollowingValidator
62}
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts
index 3f5afe5b3..9840e8f65 100644
--- a/server/middlewares/validators/index.ts
+++ b/server/middlewares/validators/index.ts
@@ -2,7 +2,7 @@ export * from './account'
2export * from './oembed' 2export * from './oembed'
3export * from './activitypub' 3export * from './activitypub'
4export * from './pagination' 4export * from './pagination'
5export * from './servers' 5export * from './follows'
6export * from './sort' 6export * from './sort'
7export * from './users' 7export * from './users'
8export * from './videos' 8export * from './videos'
diff --git a/server/middlewares/validators/servers.ts b/server/middlewares/validators/servers.ts
deleted file mode 100644
index 95b69b789..000000000
--- a/server/middlewares/validators/servers.ts
+++ /dev/null
@@ -1,32 +0,0 @@
1import * as express from 'express'
2import { body } from 'express-validator/check'
3import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers'
4import { isTestInstance } from '../../helpers/core-utils'
5import { CONFIG } from '../../initializers/constants'
6import { logger } from '../../helpers/logger'
7import { checkErrors } from './utils'
8
9const followValidator = [
10 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
11
12 (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 // Force https if the administrator wants to make friends
14 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
15 return res.status(400)
16 .json({
17 error: 'Cannot follow non HTTPS web server.'
18 })
19 .end()
20 }
21
22 logger.debug('Checking follow parameters', { parameters: req.body })
23
24 checkErrors(req, res, next)
25 }
26]
27
28// ---------------------------------------------------------------------------
29
30export {
31 followValidator
32}