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.`
74 res.locals.follow = follow
79 const getFollowerValidator = [
80 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
82 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
83 logger.debug('Checking get follower parameters', { parameters: req.params })
85 if (areValidationErrors(req, res)) return
87 let follow: MActorFollowActorsDefault
89 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
90 const actor = await ActorModel.loadByUrl(actorUrl)
92 const serverActor = await getServerActor()
93 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
95 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
100 .status(HttpStatusCode.NOT_FOUND_404)
102 error: `Follower ${req.params.nameWithHost} not found.`
107 res.locals.follow = follow
112 const acceptOrRejectFollowerValidator = [
113 (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
116 const follow = res.locals.follow
117 if (follow.state !== 'pending') {
119 .status(HttpStatusCode.BAD_REQUEST_400)
121 error: 'Follow is not in pending state.'
130 // ---------------------------------------------------------------------------
134 removeFollowingValidator,
135 getFollowerValidator,
136 acceptOrRejectFollowerValidator,