2018-08-16 08:25:20 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import 'express-validator'
|
2018-08-23 10:58:39 -05:00
|
|
|
import { body, param, query } from 'express-validator/check'
|
2018-08-16 08:25:20 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { areValidationErrors } from './utils'
|
|
|
|
import { ActorFollowModel } from '../../models/activitypub/actor-follow'
|
2018-08-23 10:58:39 -05:00
|
|
|
import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
|
2018-08-16 08:25:20 -05:00
|
|
|
import { CONFIG } from '../../initializers'
|
2018-08-23 10:58:39 -05:00
|
|
|
import { toArray } from '../../helpers/custom-validators/misc'
|
2018-08-16 08:25:20 -05:00
|
|
|
|
|
|
|
const userSubscriptionAddValidator = [
|
|
|
|
body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-23 10:58:39 -05:00
|
|
|
const areSubscriptionsExistValidator = [
|
|
|
|
query('uris')
|
|
|
|
.customSanitizer(toArray)
|
|
|
|
.custom(areValidActorHandles).withMessage('Should have a valid uri array'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking areSubscriptionsExistValidator parameters', { parameters: req.query })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-21 03:34:18 -05:00
|
|
|
const userSubscriptionGetValidator = [
|
2018-08-16 08:25:20 -05:00
|
|
|
param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-08-21 03:34:18 -05:00
|
|
|
logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params })
|
2018-08-16 08:25:20 -05:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
let [ name, host ] = req.params.uri.split('@')
|
|
|
|
if (host === CONFIG.WEBSERVER.HOST) host = null
|
|
|
|
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-08-23 10:58:39 -05:00
|
|
|
const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host)
|
2018-08-16 08:25:20 -05:00
|
|
|
|
2018-08-21 03:34:18 -05:00
|
|
|
if (!subscription || !subscription.ActorFollowing.VideoChannel) {
|
2018-08-16 08:25:20 -05:00
|
|
|
return res
|
|
|
|
.status(404)
|
|
|
|
.json({
|
|
|
|
error: `Subscription ${req.params.uri} not found.`
|
|
|
|
})
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.subscription = subscription
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-08-23 10:58:39 -05:00
|
|
|
areSubscriptionsExistValidator,
|
2018-08-16 08:25:20 -05:00
|
|
|
userSubscriptionAddValidator,
|
2018-08-21 03:34:18 -05:00
|
|
|
userSubscriptionGetValidator
|
2018-08-16 08:25:20 -05:00
|
|
|
}
|