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 { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
7 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
8 import { areValidationErrors } from './utils'
9 import { ActorModel } from '../../models/activitypub/actor'
10 import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger'
11 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
12 import { MActorFollowActorsDefault } from '@server/types/models'
13 import { isFollowStateValid } from '@server/helpers/custom-validators/follows'
14 import { getServerActor } from '@server/models/application/application'
15 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
17 const listFollowsValidator = [
20 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
23 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 if (areValidationErrors(req, res)) return
32 const followValidator = [
33 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 // Force https if the administrator wants to make friends
37 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
39 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
41 error: 'Cannot follow on a non HTTPS web server.'
46 logger.debug('Checking follow parameters', { parameters: req.body })
48 if (areValidationErrors(req, res)) return
54 const removeFollowingValidator = [
55 param('host').custom(isHostValid).withMessage('Should have a valid host'),
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking unfollowing parameters', { parameters: req.params })
60 if (areValidationErrors(req, res)) return
62 const serverActor = await getServerActor()
63 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host)
67 .status(HttpStatusCode.NOT_FOUND_404)
69 error: `Following ${req.params.host} not found.`
73 res.locals.follow = follow
78 const getFollowerValidator = [
79 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
81 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
82 logger.debug('Checking get follower parameters', { parameters: req.params })
84 if (areValidationErrors(req, res)) return
86 let follow: MActorFollowActorsDefault
88 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
89 const actor = await ActorModel.loadByUrl(actorUrl)
91 const serverActor = await getServerActor()
92 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
94 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
99 .status(HttpStatusCode.NOT_FOUND_404)
101 error: `Follower ${req.params.nameWithHost} not found.`
106 res.locals.follow = follow
111 const acceptOrRejectFollowerValidator = [
112 (req: express.Request, res: express.Response, next: express.NextFunction) => {
113 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
115 const follow = res.locals.follow
116 if (follow.state !== 'pending') {
118 .status(HttpStatusCode.BAD_REQUEST_400)
120 error: 'Follow is not in pending state.'
129 // ---------------------------------------------------------------------------
133 removeFollowingValidator,
134 getFollowerValidator,
135 acceptOrRejectFollowerValidator,