aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/user-subscriptions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/user-subscriptions.ts')
-rw-r--r--server/middlewares/validators/user-subscriptions.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts
new file mode 100644
index 000000000..f331b6c34
--- /dev/null
+++ b/server/middlewares/validators/user-subscriptions.ts
@@ -0,0 +1,58 @@
1import * as express from 'express'
2import 'express-validator'
3import { body, param } from 'express-validator/check'
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { ActorFollowModel } from '../../models/activitypub/actor-follow'
7import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
8import { UserModel } from '../../models/account/user'
9import { CONFIG } from '../../initializers'
10
11const userSubscriptionAddValidator = [
12 body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
13
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body })
16
17 if (areValidationErrors(req, res)) return
18
19 return next()
20 }
21]
22
23const userSubscriptionRemoveValidator = [
24 param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking unfollow parameters', { parameters: req.params })
28
29 if (areValidationErrors(req, res)) return
30
31 let [ name, host ] = req.params.uri.split('@')
32 if (host === CONFIG.WEBSERVER.HOST) host = null
33
34 const user: UserModel = res.locals.oauth.token.User
35 const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHost(user.Account.Actor.id, name, host)
36
37 if (!subscription) {
38 return res
39 .status(404)
40 .json({
41 error: `Subscription ${req.params.uri} not found.`
42 })
43 .end()
44 }
45
46 res.locals.subscription = subscription
47 return next()
48 }
49]
50
51// ---------------------------------------------------------------------------
52
53export {
54 userSubscriptionAddValidator,
55 userSubscriptionRemoveValidator
56}
57
58// ---------------------------------------------------------------------------