1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows'
4 import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors'
5 import { getRemoteNameAndHost } from '@server/lib/activitypub/follow'
6 import { getServerActor } from '@server/models/application/application'
7 import { MActorFollowActorsDefault } from '@server/types/models'
8 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
9 import { isTestInstance } from '../../helpers/core-utils'
10 import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
11 import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers'
12 import { logger } from '../../helpers/logger'
13 import { WEBSERVER } from '../../initializers/constants'
14 import { ActorModel } from '../../models/actor/actor'
15 import { ActorFollowModel } from '../../models/actor/actor-follow'
16 import { areValidationErrors } from './shared'
17 import { ServerFollowCreate } from '@shared/models'
19 const listFollowsValidator = [
22 .custom(isFollowStateValid).withMessage('Should have a valid follow state'),
25 .custom(isActorTypeValid).withMessage('Should have a valid actor type'),
27 (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 if (areValidationErrors(req, res)) return
34 const followValidator = [
37 .custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
41 .custom(isEachUniqueHandleValid).withMessage('Should have an array of handles'),
43 (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 // Force https if the administrator wants to follow remote actors
45 if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') {
47 .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
49 error: 'Cannot follow on a non HTTPS web server.'
53 logger.debug('Checking follow parameters', { parameters: req.body })
55 if (areValidationErrors(req, res)) return
57 const body: ServerFollowCreate = req.body
58 if (body.hosts.length === 0 && body.handles.length === 0) {
61 .status(HttpStatusCode.BAD_REQUEST_400)
63 error: 'You must provide at least one handle or one host.'
71 const removeFollowingValidator = [
73 .custom(value => isHostValid(value) || isRemoteHandleValid(value))
74 .withMessage('Should have a valid host/handle'),
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking unfollowing parameters', { parameters: req.params })
79 if (areValidationErrors(req, res)) return
81 const serverActor = await getServerActor()
83 const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle)
84 const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, name, host)
88 status: HttpStatusCode.NOT_FOUND_404,
89 message: `Follow ${req.params.hostOrHandle} not found.`
93 res.locals.follow = follow
98 const getFollowerValidator = [
99 param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'),
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
102 logger.debug('Checking get follower parameters', { parameters: req.params })
104 if (areValidationErrors(req, res)) return
106 let follow: MActorFollowActorsDefault
108 const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost)
109 const actor = await ActorModel.loadByUrl(actorUrl)
111 const serverActor = await getServerActor()
112 follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id)
114 logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err })
119 status: HttpStatusCode.NOT_FOUND_404,
120 message: `Follower ${req.params.nameWithHost} not found.`
124 res.locals.follow = follow
129 const acceptOrRejectFollowerValidator = [
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
131 logger.debug('Checking accept/reject follower parameters', { parameters: req.params })
133 const follow = res.locals.follow
134 if (follow.state !== 'pending') {
135 return res.fail({ message: 'Follow is not in pending state.' })
142 // ---------------------------------------------------------------------------
146 removeFollowingValidator,
147 getFollowerValidator,
148 acceptOrRejectFollowerValidator,