PeerTube/server/middlewares/validators/user-subscriptions.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

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