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 { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
8 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
9 import { areValidationErrors } from './utils'
10 import { ActorModel } from '../../models/activitypub/actor'
11 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
12 import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
14 const followValidator = [
15 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
17 (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 // Force https if the administrator wants to make friends
19 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
20 return res.status(500)
22 error: 'Cannot follow on a non HTTPS web server.'
27 logger.debug('Checking follow parameters', { parameters: req.body })
29 if (areValidationErrors(req, res)) return
35 const removeFollowingValidator = [
36 param('host').custom(isHostValid).withMessage('Should have a valid host'),
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking unfollowing parameters', { parameters: req.params })
41 if (areValidationErrors(req, res)) return
43 const serverActor = await getServerActor()
44 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
50 error: `Following ${req.params.host} not found.`
55 res.locals.follow = follow
60 const getFollowerValidator = [
61 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
63 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking get follower parameters', { parameters: req.params })
66 if (areValidationErrors(req, res)) return
68 let follow: ActorFollowModel
70 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
71 const actor = await ActorModel.loadByUrl(actorUrl)
73 const serverActor = await getServerActor()
74 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
76 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
83 error: `Follower ${req.params.nameWithHost} not found.`
88 res.locals.follow = follow
93 const acceptOrRejectFollowerValidator = [
94 (req: express.Request, res: express.Response, next: express.NextFunction) => {
95 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
97 const follow = res.locals.follow
98 if (follow.state !== 'pending') {
99 return res.status(400).json({ error: 'Follow is not in pending state.' }).end()
106 // ---------------------------------------------------------------------------
110 removeFollowingValidator,
111 getFollowerValidator,
112 acceptOrRejectFollowerValidator