1 import * as express from 'express'
2 import { body, param, query } 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'
14 import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
16 const listFollowsValidator = [
19 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
21 (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 if (areValidationErrors(req, res)) return
28 const followValidator = [
29 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
31 (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 // Force https if the administrator wants to make friends
33 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
34 return res.status(500)
36 error: 'Cannot follow on a non HTTPS web server.'
41 logger.debug('Checking follow parameters', { parameters: req.body })
43 if (areValidationErrors(req, res)) return
49 const removeFollowingValidator = [
50 param('host').custom(isHostValid).withMessage('Should have a valid host'),
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking unfollowing parameters', { parameters: req.params })
55 if (areValidationErrors(req, res)) return
57 const serverActor = await getServerActor()
58 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
64 error: `Following ${req.params.host} not found.`
69 res.locals.follow = follow
74 const getFollowerValidator = [
75 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
77 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
78 logger.debug('Checking get follower parameters', { parameters: req.params })
80 if (areValidationErrors(req, res)) return
82 let follow: MActorFollowActorsDefault
84 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
85 const actor = await ActorModel.loadByUrl(actorUrl)
87 const serverActor = await getServerActor()
88 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
90 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
97 error: `Follower ${req.params.nameWithHost} not found.`
102 res.locals.follow = follow
107 const acceptOrRejectFollowerValidator = [
108 (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
111 const follow = res.locals.follow
112 if (follow.state !== 'pending') {
113 return res.status(400).json({ error: 'Follow is not in pending state.' }).end()
120 // ---------------------------------------------------------------------------
124 removeFollowingValidator,
125 getFollowerValidator,
126 acceptOrRejectFollowerValidator,