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