1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isTestInstance } from '../../helpers/core-utils'
4 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
5 import { logger } from '../../helpers/logger'
6 import { getServerActor } from '../../helpers/utils'
7 import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers'
8 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
9 import { areValidationErrors } from './utils'
11 const followValidator = [
12 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 // Force https if the administrator wants to make friends
16 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
17 return res.status(500)
19 error: 'Cannot follow on a non HTTPS web server.'
24 logger.debug('Checking follow parameters', { parameters: req.body })
26 if (areValidationErrors(req, res)) return
32 const removeFollowingValidator = [
33 param('host').custom(isHostValid).withMessage('Should have a valid host'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking unfollow parameters', { parameters: req.params })
38 if (areValidationErrors(req, res)) return
40 const serverActor = await getServerActor()
41 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
47 error: `Follower ${req.params.host} not found.`
52 res.locals.follow = follow
57 // ---------------------------------------------------------------------------
61 removeFollowingValidator