]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param, query } from 'express-validator' | |
3 | import { logger } from '../../helpers/logger' | |
4 | import { areValidationErrors } from './utils' | |
5 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | |
6 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | |
7 | import { toArray } from '../../helpers/custom-validators/misc' | |
8 | import { WEBSERVER } from '../../initializers/constants' | |
9 | ||
10 | const userSubscriptionListValidator = [ | |
11 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), | |
12 | ||
13 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14 | logger.debug('Checking userSubscriptionListValidator parameters', { parameters: req.query }) | |
15 | ||
16 | if (areValidationErrors(req, res)) return | |
17 | ||
18 | return next() | |
19 | } | |
20 | ] | |
21 | ||
22 | const userSubscriptionAddValidator = [ | |
23 | body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), | |
24 | ||
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
26 | logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body }) | |
27 | ||
28 | if (areValidationErrors(req, res)) return | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
34 | const areSubscriptionsExistValidator = [ | |
35 | query('uris') | |
36 | .customSanitizer(toArray) | |
37 | .custom(areValidActorHandles).withMessage('Should have a valid uri array'), | |
38 | ||
39 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
40 | logger.debug('Checking areSubscriptionsExistValidator parameters', { parameters: req.query }) | |
41 | ||
42 | if (areValidationErrors(req, res)) return | |
43 | ||
44 | return next() | |
45 | } | |
46 | ] | |
47 | ||
48 | const userSubscriptionGetValidator = [ | |
49 | param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'), | |
50 | ||
51 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
52 | logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params }) | |
53 | ||
54 | if (areValidationErrors(req, res)) return | |
55 | ||
56 | let [ name, host ] = req.params.uri.split('@') | |
57 | if (host === WEBSERVER.HOST) host = null | |
58 | ||
59 | const user = res.locals.oauth.token.User | |
60 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host) | |
61 | ||
62 | if (!subscription || !subscription.ActorFollowing.VideoChannel) { | |
63 | return res | |
64 | .status(404) | |
65 | .json({ | |
66 | error: `Subscription ${req.params.uri} not found.` | |
67 | }) | |
68 | } | |
69 | ||
70 | res.locals.subscription = subscription | |
71 | return next() | |
72 | } | |
73 | ] | |
74 | ||
75 | // --------------------------------------------------------------------------- | |
76 | ||
77 | export { | |
78 | areSubscriptionsExistValidator, | |
79 | userSubscriptionListValidator, | |
80 | userSubscriptionAddValidator, | |
81 | userSubscriptionGetValidator | |
82 | } |