1 import * as express from 'express'
2 import { body, param } from 'express-validator'
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'
13 import { MActorFollowActorsDefault } from '@server/typings/models'
15 const followValidator = [
16 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
18 (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 // Force https if the administrator wants to make friends
20 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
21 return res.status(500)
23 error: 'Cannot follow on a non HTTPS web server.'
28 logger.debug('Checking follow parameters', { parameters: req.body })
30 if (areValidationErrors(req, res)) return
36 const removeFollowingValidator = [
37 param('host').custom(isHostValid).withMessage('Should have a valid host'),
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking unfollowing parameters', { parameters: req.params })
42 if (areValidationErrors(req, res)) return
44 const serverActor = await getServerActor()
45 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
51 error: `Following ${req.params.host} not found.`
56 res.locals.follow = follow
61 const getFollowerValidator = [
62 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
64 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking get follower parameters', { parameters: req.params })
67 if (areValidationErrors(req, res)) return
69 let follow: MActorFollowActorsDefault
71 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
72 const actor = await ActorModel.loadByUrl(actorUrl)
74 const serverActor = await getServerActor()
75 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
77 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
84 error: `Follower ${req.params.nameWithHost} not found.`
89 res.locals.follow = follow
94 const acceptOrRejectFollowerValidator = [
95 (req: express.Request, res: express.Response, next: express.NextFunction) => {
96 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
98 const follow = res.locals.follow
99 if (follow.state !== 'pending') {
100 return res.status(400).json({ error: 'Follow is not in pending state.' }).end()
107 // ---------------------------------------------------------------------------
111 removeFollowingValidator,
112 getFollowerValidator,
113 acceptOrRejectFollowerValidator