2018-08-16 09:25:20 -04:00
|
|
|
import * as express from 'express'
|
2019-07-25 10:23:44 -04:00
|
|
|
import { body, param, query } from 'express-validator'
|
2021-07-16 04:42:24 -04:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2018-08-23 11:58:39 -04:00
|
|
|
import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
|
|
|
|
import { toArray } from '../../helpers/custom-validators/misc'
|
2021-05-11 05:15:29 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-04-11 05:33:44 -04:00
|
|
|
import { WEBSERVER } from '../../initializers/constants'
|
2021-05-11 05:15:29 -04:00
|
|
|
import { ActorFollowModel } from '../../models/actor/actor-follow'
|
2021-06-03 11:33:44 -04:00
|
|
|
import { areValidationErrors } from './shared'
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2020-07-23 15:30:04 -04:00
|
|
|
const userSubscriptionListValidator = [
|
|
|
|
query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
|
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking userSubscriptionListValidator parameters', { parameters: req.query })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-16 09:25:20 -04: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 11:58:39 -04: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 04:34:18 -04:00
|
|
|
const userSubscriptionGetValidator = [
|
2018-08-16 09:25:20 -04: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 04:34:18 -04:00
|
|
|
logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params })
|
2018-08-16 09:25:20 -04:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
let [ name, host ] = req.params.uri.split('@')
|
2019-04-11 05:33:44 -04:00
|
|
|
if (host === WEBSERVER.HOST) host = null
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-08-23 11:58:39 -04:00
|
|
|
const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host)
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2018-08-21 04:34:18 -04:00
|
|
|
if (!subscription || !subscription.ActorFollowing.VideoChannel) {
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: `Subscription ${req.params.uri} not found.`
|
|
|
|
})
|
2018-08-16 09:25:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.subscription = subscription
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-08-23 11:58:39 -04:00
|
|
|
areSubscriptionsExistValidator,
|
2020-07-23 15:30:04 -04:00
|
|
|
userSubscriptionListValidator,
|
2018-08-16 09:25:20 -04:00
|
|
|
userSubscriptionAddValidator,
|
2018-08-21 04:34:18 -04:00
|
|
|
userSubscriptionGetValidator
|
2018-08-16 09:25:20 -04:00
|
|
|
}
|