aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/follows.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-20 09:43:39 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:52 +0100
commit54141398354e6e7b94aa3065a705a1251390111c (patch)
tree8d30d1b9ea8acbe04f6d404125b04fc0c9897b70 /server/middlewares/validators/follows.ts
parenteb8b27c93e61a896a08923dc1ca3c87ba8cf4948 (diff)
downloadPeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.gz
PeerTube-54141398354e6e7b94aa3065a705a1251390111c.tar.zst
PeerTube-54141398354e6e7b94aa3065a705a1251390111c.zip
Refractor activity pub lib/helpers
Diffstat (limited to 'server/middlewares/validators/follows.ts')
-rw-r--r--server/middlewares/validators/follows.ts62
1 files changed, 62 insertions, 0 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}