]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import 'express-validator' | |
3 | import { body, param, query } 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 { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | |
8 | import { toArray } from '../../helpers/custom-validators/misc' | |
9 | import { WEBSERVER } from '../../initializers/constants' | |
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 areSubscriptionsExistValidator = [ | |
24 | query('uris') | |
25 | .customSanitizer(toArray) | |
26 | .custom(areValidActorHandles).withMessage('Should have a valid uri array'), | |
27 | ||
28 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
29 | logger.debug('Checking areSubscriptionsExistValidator parameters', { parameters: req.query }) | |
30 | ||
31 | if (areValidationErrors(req, res)) return | |
32 | ||
33 | return next() | |
34 | } | |
35 | ] | |
36 | ||
37 | const userSubscriptionGetValidator = [ | |
38 | param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'), | |
39 | ||
40 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
41 | logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params }) | |
42 | ||
43 | if (areValidationErrors(req, res)) return | |
44 | ||
45 | let [ name, host ] = req.params.uri.split('@') | |
46 | if (host === WEBSERVER.HOST) host = null | |
47 | ||
48 | const user = res.locals.oauth.token.User | |
49 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host) | |
50 | ||
51 | if (!subscription || !subscription.ActorFollowing.VideoChannel) { | |
52 | return res | |
53 | .status(404) | |
54 | .json({ | |
55 | error: `Subscription ${req.params.uri} not found.` | |
56 | }) | |
57 | .end() | |
58 | } | |
59 | ||
60 | res.locals.subscription = subscription | |
61 | return next() | |
62 | } | |
63 | ] | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
67 | export { | |
68 | areSubscriptionsExistValidator, | |
69 | userSubscriptionAddValidator, | |
70 | userSubscriptionGetValidator | |
71 | } |