]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
c8861d5d | 2 | import { body, param, query } from 'express-validator' |
c0e8b12e | 3 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
f37dc0dd | 4 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
f37dc0dd | 5 | import { toArray } from '../../helpers/custom-validators/misc' |
7d9ba5c0 | 6 | import { logger } from '../../helpers/logger' |
6dd9de95 | 7 | import { WEBSERVER } from '../../initializers/constants' |
7d9ba5c0 | 8 | import { ActorFollowModel } from '../../models/actor/actor-follow' |
10363c74 | 9 | import { areValidationErrors } from './shared' |
06a05d5f | 10 | |
4f5d0459 | 11 | const userSubscriptionListValidator = [ |
396f6f01 C |
12 | query('search') |
13 | .optional() | |
14 | .not().isEmpty(), | |
4f5d0459 RK |
15 | |
16 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
17 | logger.debug('Checking userSubscriptionListValidator parameters', { parameters: req.query }) | |
18 | ||
19 | if (areValidationErrors(req, res)) return | |
20 | ||
21 | return next() | |
22 | } | |
23 | ] | |
24 | ||
06a05d5f | 25 | const userSubscriptionAddValidator = [ |
396f6f01 C |
26 | body('uri') |
27 | .custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), | |
06a05d5f C |
28 | |
29 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
30 | logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body }) | |
31 | ||
32 | if (areValidationErrors(req, res)) return | |
33 | ||
34 | return next() | |
35 | } | |
36 | ] | |
37 | ||
f37dc0dd C |
38 | const areSubscriptionsExistValidator = [ |
39 | query('uris') | |
40 | .customSanitizer(toArray) | |
396f6f01 | 41 | .custom(areValidActorHandles).withMessage('Should have a valid array of URIs'), |
f37dc0dd C |
42 | |
43 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
44 | logger.debug('Checking areSubscriptionsExistValidator parameters', { parameters: req.query }) | |
45 | ||
46 | if (areValidationErrors(req, res)) return | |
47 | ||
48 | return next() | |
49 | } | |
50 | ] | |
51 | ||
99492dbc | 52 | const userSubscriptionGetValidator = [ |
396f6f01 C |
53 | param('uri') |
54 | .custom(isValidActorHandle), | |
06a05d5f C |
55 | |
56 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
99492dbc | 57 | logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params }) |
06a05d5f C |
58 | |
59 | if (areValidationErrors(req, res)) return | |
60 | ||
61 | let [ name, host ] = req.params.uri.split('@') | |
6dd9de95 | 62 | if (host === WEBSERVER.HOST) host = null |
06a05d5f | 63 | |
dae86118 | 64 | const user = res.locals.oauth.token.User |
927fa4b1 C |
65 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({ |
66 | actorId: user.Account.Actor.id, | |
67 | targetName: name, | |
68 | targetHost: host, | |
69 | state: 'accepted' | |
70 | }) | |
06a05d5f | 71 | |
99492dbc | 72 | if (!subscription || !subscription.ActorFollowing.VideoChannel) { |
76148b27 RK |
73 | return res.fail({ |
74 | status: HttpStatusCode.NOT_FOUND_404, | |
75 | message: `Subscription ${req.params.uri} not found.` | |
76 | }) | |
06a05d5f C |
77 | } |
78 | ||
79 | res.locals.subscription = subscription | |
80 | return next() | |
81 | } | |
82 | ] | |
83 | ||
84 | // --------------------------------------------------------------------------- | |
85 | ||
86 | export { | |
f37dc0dd | 87 | areSubscriptionsExistValidator, |
4f5d0459 | 88 | userSubscriptionListValidator, |
06a05d5f | 89 | userSubscriptionAddValidator, |
99492dbc | 90 | userSubscriptionGetValidator |
06a05d5f | 91 | } |