diff options
-rw-r--r-- | server/middlewares/validators/user-subscriptions.ts | 63 |
1 files changed, 46 insertions, 17 deletions
diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts index d8d3fc28b..68d83add5 100644 --- a/server/middlewares/validators/user-subscriptions.ts +++ b/server/middlewares/validators/user-subscriptions.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { arrayify } from '@shared/core-utils' | 3 | import { arrayify } from '@shared/core-utils' |
4 | import { FollowState } from '@shared/models' | ||
4 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | 5 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
5 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | 6 | import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
6 | import { WEBSERVER } from '../../initializers/constants' | 7 | import { WEBSERVER } from '../../initializers/constants' |
@@ -48,26 +49,20 @@ const userSubscriptionGetValidator = [ | |||
48 | 49 | ||
49 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 50 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
50 | if (areValidationErrors(req, res)) return | 51 | if (areValidationErrors(req, res)) return |
52 | if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return | ||
51 | 53 | ||
52 | let [ name, host ] = req.params.uri.split('@') | 54 | return next() |
53 | if (host === WEBSERVER.HOST) host = null | 55 | } |
56 | ] | ||
54 | 57 | ||
55 | const user = res.locals.oauth.token.User | 58 | const userSubscriptionDeleteValidator = [ |
56 | const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({ | 59 | param('uri') |
57 | actorId: user.Account.Actor.id, | 60 | .custom(isValidActorHandle), |
58 | targetName: name, | ||
59 | targetHost: host, | ||
60 | state: 'accepted' | ||
61 | }) | ||
62 | 61 | ||
63 | if (!subscription?.ActorFollowing.VideoChannel) { | 62 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
64 | return res.fail({ | 63 | if (areValidationErrors(req, res)) return |
65 | status: HttpStatusCode.NOT_FOUND_404, | 64 | if (!await doesSubscriptionExist({ uri: req.params.uri, res })) return |
66 | message: `Subscription ${req.params.uri} not found.` | ||
67 | }) | ||
68 | } | ||
69 | 65 | ||
70 | res.locals.subscription = subscription | ||
71 | return next() | 66 | return next() |
72 | } | 67 | } |
73 | ] | 68 | ] |
@@ -78,5 +73,39 @@ export { | |||
78 | areSubscriptionsExistValidator, | 73 | areSubscriptionsExistValidator, |
79 | userSubscriptionListValidator, | 74 | userSubscriptionListValidator, |
80 | userSubscriptionAddValidator, | 75 | userSubscriptionAddValidator, |
81 | userSubscriptionGetValidator | 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 | ||
82 | } | 111 | } |