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'
11 const userSubscriptionAddValidator = [
12 body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body })
17 if (areValidationErrors(req, res)) return
23 const userSubscriptionRemoveValidator = [
24 param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking unfollow parameters', { parameters: req.params })
29 if (areValidationErrors(req, res)) return
31 let [ name, host ] = req.params.uri.split('@')
32 if (host === CONFIG.WEBSERVER.HOST) host = null
34 const user: UserModel = res.locals.oauth.token.User
35 const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHost(user.Account.Actor.id, name, host)
41 error: `Subscription ${req.params.uri} not found.`
46 res.locals.subscription = subscription
51 // ---------------------------------------------------------------------------
54 userSubscriptionAddValidator,
55 userSubscriptionRemoveValidator
58 // ---------------------------------------------------------------------------