2021-08-27 08:32:44 -04:00
|
|
|
import express from 'express'
|
2019-07-25 10:23:44 -04:00
|
|
|
import { body, param, query } from 'express-validator'
|
2022-11-15 09:00:19 -05:00
|
|
|
import { arrayify } from '@shared/core-utils'
|
2023-05-17 09:08:40 -04:00
|
|
|
import { FollowState } from '@shared/models'
|
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'
|
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 = [
|
2022-08-17 08:27:04 -04:00
|
|
|
query('search')
|
|
|
|
.optional()
|
|
|
|
.not().isEmpty(),
|
2020-07-23 15:30:04 -04:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-16 09:25:20 -04:00
|
|
|
const userSubscriptionAddValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
body('uri')
|
|
|
|
.custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
|
2018-08-16 09:25:20 -04:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-23 11:58:39 -04:00
|
|
|
const areSubscriptionsExistValidator = [
|
|
|
|
query('uris')
|
2022-08-17 09:44:32 -04:00
|
|
|
.customSanitizer(arrayify)
|
2022-08-17 08:27:04 -04:00
|
|
|
.custom(areValidActorHandles).withMessage('Should have a valid array of URIs'),
|
2018-08-23 11:58:39 -04:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-21 04:34:18 -04:00
|
|
|
const userSubscriptionGetValidator = [
|
2022-08-17 08:27:04 -04:00
|
|
|
param('uri')
|
|
|
|
.custom(isValidActorHandle),
|
2018-08-16 09:25:20 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2023-05-17 09:08:40 -04:00
|
|
|
if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2023-05-17 09:08:40 -04:00
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2023-05-17 09:08:40 -04:00
|
|
|
const userSubscriptionDeleteValidator = [
|
|
|
|
param('uri')
|
|
|
|
.custom(isValidActorHandle),
|
2018-08-16 09:25:20 -04:00
|
|
|
|
2023-05-17 09:08:40 -04: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
|
2018-08-16 09:25:20 -04:00
|
|
|
|
|
|
|
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,
|
2023-05-17 09:08:40 -04: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
|
2018-08-16 09:25:20 -04:00
|
|
|
}
|