]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/user-subscriptions.ts
f331b6c34c13d1fe55a227911e89af3ee7a22677
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / user-subscriptions.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { logger } from '../../helpers/logger'
5 import { areValidationErrors } from './utils'
6 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
7 import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
8 import { UserModel } from '../../models/account/user'
9 import { CONFIG } from '../../initializers'
10
11 const 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
23 const 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
53 export {
54 userSubscriptionAddValidator,
55 userSubscriptionRemoveValidator
56 }
57
58 // ---------------------------------------------------------------------------