]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { body, param, query } from 'express-validator' | |
3 | import { arrayify } from '@shared/core-utils' | |
4 | import { FollowState } from '@shared/models' | |
5 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
6 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | |
7 | import { WEBSERVER } from '../../initializers/constants' | |
8 | import { ActorFollowModel } from '../../models/actor/actor-follow' | |
9 | import { areValidationErrors } from './shared' | |
10 | ||
11 | const userSubscriptionListValidator = [ | |
12 | query('search') | |
13 | .optional() | |
14 | .not().isEmpty(), | |
15 | ||
16 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
17 | if (areValidationErrors(req, res)) return | |
18 | ||
19 | return next() | |
20 | } | |
21 | ] | |
22 | ||
23 | const userSubscriptionAddValidator = [ | |
24 | body('uri') | |
25 | .custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), | |
26 | ||
27 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
28 | if (areValidationErrors(req, res)) return | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
34 | const areSubscriptionsExistValidator = [ | |
35 | query('uris') | |
36 | .customSanitizer(arrayify) | |
37 | .custom(areValidActorHandles).withMessage('Should have a valid array of URIs'), | |
38 | ||
39 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
40 | if (areValidationErrors(req, res)) return | |
41 | ||
42 | return next() | |
43 | } | |
44 | ] | |
45 | ||
46 | const userSubscriptionGetValidator = [ | |
47 | param('uri') | |
48 | .custom(isValidActorHandle), | |
49 | ||
50 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
51 | if (areValidationErrors(req, res)) return | |
52 | if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return | |
53 | ||
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
58 | const userSubscriptionDeleteValidator = [ | |
59 | param('uri') | |
60 | .custom(isValidActorHandle), | |
61 | ||
62 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
63 | if (areValidationErrors(req, res)) return | |
64 | if (!await doesSubscriptionExist({ uri: req.params.uri, res })) return | |
65 | ||
66 | return next() | |
67 | } | |
68 | ] | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | export { | |
73 | areSubscriptionsExistValidator, | |
74 | userSubscriptionListValidator, | |
75 | userSubscriptionAddValidator, | |
76 | userSubscriptionGetValidator, | |
77 | userSubscriptionDeleteValidator | |
78 | } | |
79 | ||
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | async function doesSubscriptionExist (options: { | |
83 | uri: string | |
84 | res: express.Response | |
85 | state?: FollowState | |
86 | }) { | |
87 | const { uri, res, state } = options | |
88 | ||
89 | let [ name, host ] = uri.split('@') | |
90 | if (host === WEBSERVER.HOST) host = null | |
91 | ||
92 | const user = res.locals.oauth.token.User | |
93 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({ | |
94 | actorId: user.Account.Actor.id, | |
95 | targetName: name, | |
96 | targetHost: host, | |
97 | state | |
98 | }) | |
99 | ||
100 | if (!subscription?.ActorFollowing.VideoChannel) { | |
101 | res.fail({ | |
102 | status: HttpStatusCode.NOT_FOUND_404, | |
103 | message: `Subscription ${uri} not found.` | |
104 | }) | |
105 | return false | |
106 | } | |
107 | ||
108 | res.locals.subscription = subscription | |
109 | ||
110 | return true | |
111 | } |