]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import * as express from 'express' |
b8f4167f | 2 | import { body, param, query } from 'express-validator' |
7d9ba5c0 | 3 | import { isFollowStateValid } from '@server/helpers/custom-validators/follows' |
10363c74 | 4 | import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors' |
7d9ba5c0 C |
5 | import { getServerActor } from '@server/models/application/application' |
6 | import { MActorFollowActorsDefault } from '@server/types/models' | |
c0e8b12e | 7 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
da854ddd | 8 | import { isTestInstance } from '../../helpers/core-utils' |
7d9ba5c0 | 9 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
50d6de9c | 10 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
da854ddd | 11 | import { logger } from '../../helpers/logger' |
74dc3bca | 12 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' |
7d9ba5c0 C |
13 | import { ActorModel } from '../../models/actor/actor' |
14 | import { ActorFollowModel } from '../../models/actor/actor-follow' | |
10363c74 | 15 | import { areValidationErrors } from './shared' |
b8f4167f C |
16 | |
17 | const listFollowsValidator = [ | |
18 | query('state') | |
19 | .optional() | |
20 | .custom(isFollowStateValid).withMessage('Should have a valid follow state'), | |
97ecddae C |
21 | query('actorType') |
22 | .optional() | |
23 | .custom(isActorTypeValid).withMessage('Should have a valid actor type'), | |
b8f4167f C |
24 | |
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
26 | if (areValidationErrors(req, res)) return | |
27 | ||
28 | return next() | |
29 | } | |
30 | ] | |
54141398 C |
31 | |
32 | const followValidator = [ | |
33 | body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | |
34 | ||
35 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
36 | // Force https if the administrator wants to make friends | |
6dd9de95 | 37 | if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') { |
2d53be02 RK |
38 | return res |
39 | .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500) | |
54141398 | 40 | .json({ |
48dce1c9 | 41 | error: 'Cannot follow on a non HTTPS web server.' |
54141398 C |
42 | }) |
43 | .end() | |
44 | } | |
45 | ||
46 | logger.debug('Checking follow parameters', { parameters: req.body }) | |
47 | ||
a2431b7d C |
48 | if (areValidationErrors(req, res)) return |
49 | ||
50 | return next() | |
54141398 C |
51 | } |
52 | ] | |
53 | ||
54 | const removeFollowingValidator = [ | |
50d6de9c | 55 | param('host').custom(isHostValid).withMessage('Should have a valid host'), |
54141398 | 56 | |
a2431b7d | 57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
0e9c48c2 | 58 | logger.debug('Checking unfollowing parameters', { parameters: req.params }) |
54141398 | 59 | |
a2431b7d | 60 | if (areValidationErrors(req, res)) return |
54141398 | 61 | |
50d6de9c | 62 | const serverActor = await getServerActor() |
f37dc0dd | 63 | const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host) |
54141398 | 64 | |
a2431b7d | 65 | if (!follow) { |
76148b27 RK |
66 | return res.fail({ |
67 | status: HttpStatusCode.NOT_FOUND_404, | |
68 | message: `Following ${req.params.host} not found.` | |
69 | }) | |
0e9c48c2 C |
70 | } |
71 | ||
72 | res.locals.follow = follow | |
73 | return next() | |
74 | } | |
75 | ] | |
76 | ||
14893eb7 | 77 | const getFollowerValidator = [ |
0e9c48c2 C |
78 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), |
79 | ||
80 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14893eb7 | 81 | logger.debug('Checking get follower parameters', { parameters: req.params }) |
0e9c48c2 C |
82 | |
83 | if (areValidationErrors(req, res)) return | |
84 | ||
453e83ea | 85 | let follow: MActorFollowActorsDefault |
5b9c965d C |
86 | try { |
87 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | |
88 | const actor = await ActorModel.loadByUrl(actorUrl) | |
0e9c48c2 | 89 | |
5b9c965d C |
90 | const serverActor = await getServerActor() |
91 | follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | |
92 | } catch (err) { | |
93 | logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err }) | |
94 | } | |
0e9c48c2 C |
95 | |
96 | if (!follow) { | |
76148b27 RK |
97 | return res.fail({ |
98 | status: HttpStatusCode.NOT_FOUND_404, | |
99 | message: `Follower ${req.params.nameWithHost} not found.` | |
100 | }) | |
a2431b7d | 101 | } |
54141398 | 102 | |
a2431b7d C |
103 | res.locals.follow = follow |
104 | return next() | |
54141398 C |
105 | } |
106 | ] | |
107 | ||
14893eb7 C |
108 | const acceptOrRejectFollowerValidator = [ |
109 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
110 | logger.debug('Checking accept/reject follower parameters', { parameters: req.params }) | |
111 | ||
112 | const follow = res.locals.follow | |
113 | if (follow.state !== 'pending') { | |
76148b27 | 114 | return res.fail({ message: 'Follow is not in pending state.' }) |
14893eb7 C |
115 | } |
116 | ||
117 | return next() | |
118 | } | |
119 | ] | |
120 | ||
54141398 C |
121 | // --------------------------------------------------------------------------- |
122 | ||
123 | export { | |
124 | followValidator, | |
0e9c48c2 | 125 | removeFollowingValidator, |
14893eb7 | 126 | getFollowerValidator, |
b8f4167f C |
127 | acceptOrRejectFollowerValidator, |
128 | listFollowsValidator | |
54141398 | 129 | } |