diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-16 15:25:20 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-27 09:41:54 +0200 |
commit | 06a05d5f4784a7cbb27aa1188385b5679845dad8 (patch) | |
tree | ac197f3ed0768529456225ad76c912f22bc55e29 /server/middlewares/validators/user-subscriptions.ts | |
parent | 4bda2e47bbc937c401ddcf14c1be53c70481a294 (diff) | |
download | PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.gz PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.tar.zst PeerTube-06a05d5f4784a7cbb27aa1188385b5679845dad8.zip |
Add subscriptions endpoints to REST API
Diffstat (limited to 'server/middlewares/validators/user-subscriptions.ts')
-rw-r--r-- | server/middlewares/validators/user-subscriptions.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts new file mode 100644 index 000000000..f331b6c34 --- /dev/null +++ b/server/middlewares/validators/user-subscriptions.ts | |||
@@ -0,0 +1,58 @@ | |||
1 | import * as express from 'express' | ||
2 | import 'express-validator' | ||
3 | import { body, param } from 'express-validator/check' | ||
4 | import { logger } from '../../helpers/logger' | ||
5 | import { areValidationErrors } from './utils' | ||
6 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | ||
7 | import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | ||
8 | import { UserModel } from '../../models/account/user' | ||
9 | import { CONFIG } from '../../initializers' | ||
10 | |||
11 | const userSubscriptionAddValidator = [ | ||
12 | body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), | ||
13 | |||
14 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
15 | logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body }) | ||
16 | |||
17 | if (areValidationErrors(req, res)) return | ||
18 | |||
19 | return next() | ||
20 | } | ||
21 | ] | ||
22 | |||
23 | const userSubscriptionRemoveValidator = [ | ||
24 | param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'), | ||
25 | |||
26 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
27 | logger.debug('Checking unfollow parameters', { parameters: req.params }) | ||
28 | |||
29 | if (areValidationErrors(req, res)) return | ||
30 | |||
31 | let [ name, host ] = req.params.uri.split('@') | ||
32 | if (host === CONFIG.WEBSERVER.HOST) host = null | ||
33 | |||
34 | const user: UserModel = res.locals.oauth.token.User | ||
35 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHost(user.Account.Actor.id, name, host) | ||
36 | |||
37 | if (!subscription) { | ||
38 | return res | ||
39 | .status(404) | ||
40 | .json({ | ||
41 | error: `Subscription ${req.params.uri} not found.` | ||
42 | }) | ||
43 | .end() | ||
44 | } | ||
45 | |||
46 | res.locals.subscription = subscription | ||
47 | return next() | ||
48 | } | ||
49 | ] | ||
50 | |||
51 | // --------------------------------------------------------------------------- | ||
52 | |||
53 | export { | ||
54 | userSubscriptionAddValidator, | ||
55 | userSubscriptionRemoveValidator | ||
56 | } | ||
57 | |||
58 | // --------------------------------------------------------------------------- | ||