]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
c8861d5d | 2 | import { body, param, query } from 'express-validator' |
99b75748 | 3 | import { arrayify } from '@shared/core-utils' |
d3fcf1c5 | 4 | import { FollowState } from '@shared/models' |
c0e8b12e | 5 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
f37dc0dd | 6 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
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) => { | |
4f5d0459 RK |
17 | if (areValidationErrors(req, res)) return |
18 | ||
19 | return next() | |
20 | } | |
21 | ] | |
22 | ||
06a05d5f | 23 | const userSubscriptionAddValidator = [ |
396f6f01 C |
24 | body('uri') |
25 | .custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), | |
06a05d5f C |
26 | |
27 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
06a05d5f C |
28 | if (areValidationErrors(req, res)) return |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
33 | ||
f37dc0dd C |
34 | const areSubscriptionsExistValidator = [ |
35 | query('uris') | |
86347717 | 36 | .customSanitizer(arrayify) |
396f6f01 | 37 | .custom(areValidActorHandles).withMessage('Should have a valid array of URIs'), |
f37dc0dd C |
38 | |
39 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
f37dc0dd C |
40 | if (areValidationErrors(req, res)) return |
41 | ||
42 | return next() | |
43 | } | |
44 | ] | |
45 | ||
99492dbc | 46 | const userSubscriptionGetValidator = [ |
396f6f01 C |
47 | param('uri') |
48 | .custom(isValidActorHandle), | |
06a05d5f C |
49 | |
50 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
06a05d5f | 51 | if (areValidationErrors(req, res)) return |
d3fcf1c5 | 52 | if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return |
06a05d5f | 53 | |
d3fcf1c5 C |
54 | return next() |
55 | } | |
56 | ] | |
06a05d5f | 57 | |
d3fcf1c5 C |
58 | const userSubscriptionDeleteValidator = [ |
59 | param('uri') | |
60 | .custom(isValidActorHandle), | |
06a05d5f | 61 | |
d3fcf1c5 C |
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 | |
06a05d5f | 65 | |
06a05d5f C |
66 | return next() |
67 | } | |
68 | ] | |
69 | ||
70 | // --------------------------------------------------------------------------- | |
71 | ||
72 | export { | |
f37dc0dd | 73 | areSubscriptionsExistValidator, |
4f5d0459 | 74 | userSubscriptionListValidator, |
06a05d5f | 75 | userSubscriptionAddValidator, |
d3fcf1c5 C |
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 | |
06a05d5f | 111 | } |