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

111 lines
3.0 KiB
TypeScript
Raw Normal View History

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