]>
Commit | Line | Data |
---|---|---|
54141398 | 1 | import * as express from 'express' |
b8f4167f | 2 | import { body, param, query } from 'express-validator' |
da854ddd | 3 | import { isTestInstance } from '../../helpers/core-utils' |
50d6de9c | 4 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
da854ddd | 5 | import { logger } from '../../helpers/logger' |
74dc3bca | 6 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' |
50d6de9c | 7 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' |
a2431b7d | 8 | import { areValidationErrors } from './utils' |
0e9c48c2 C |
9 | import { ActorModel } from '../../models/activitypub/actor' |
10 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | |
97ecddae | 11 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
26d6bf65 | 12 | import { MActorFollowActorsDefault } from '@server/types/models' |
b8f4167f | 13 | import { isFollowStateValid } from '@server/helpers/custom-validators/follows' |
8dc8a34e | 14 | import { getServerActor } from '@server/models/application/application' |
2d53be02 | 15 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
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) { |
eec63bbc | 66 | return res |
2d53be02 | 67 | .status(HttpStatusCode.NOT_FOUND_404) |
eec63bbc | 68 | .json({ |
0e9c48c2 C |
69 | error: `Following ${req.params.host} not found.` |
70 | }) | |
0e9c48c2 C |
71 | } |
72 | ||
73 | res.locals.follow = follow | |
74 | return next() | |
75 | } | |
76 | ] | |
77 | ||
14893eb7 | 78 | const getFollowerValidator = [ |
0e9c48c2 C |
79 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), |
80 | ||
81 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14893eb7 | 82 | logger.debug('Checking get follower parameters', { parameters: req.params }) |
0e9c48c2 C |
83 | |
84 | if (areValidationErrors(req, res)) return | |
85 | ||
453e83ea | 86 | let follow: MActorFollowActorsDefault |
5b9c965d C |
87 | try { |
88 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | |
89 | const actor = await ActorModel.loadByUrl(actorUrl) | |
0e9c48c2 | 90 | |
5b9c965d C |
91 | const serverActor = await getServerActor() |
92 | follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | |
93 | } catch (err) { | |
94 | logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err }) | |
95 | } | |
0e9c48c2 C |
96 | |
97 | if (!follow) { | |
98 | return res | |
2d53be02 | 99 | .status(HttpStatusCode.NOT_FOUND_404) |
0e9c48c2 C |
100 | .json({ |
101 | error: `Follower ${req.params.nameWithHost} not found.` | |
eec63bbc | 102 | }) |
a2431b7d C |
103 | .end() |
104 | } | |
54141398 | 105 | |
a2431b7d C |
106 | res.locals.follow = follow |
107 | return next() | |
54141398 C |
108 | } |
109 | ] | |
110 | ||
14893eb7 C |
111 | const acceptOrRejectFollowerValidator = [ |
112 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
113 | logger.debug('Checking accept/reject follower parameters', { parameters: req.params }) | |
114 | ||
115 | const follow = res.locals.follow | |
116 | if (follow.state !== 'pending') { | |
2d53be02 RK |
117 | return res |
118 | .status(HttpStatusCode.BAD_REQUEST_400) | |
119 | .json({ | |
120 | error: 'Follow is not in pending state.' | |
121 | }) | |
122 | .end() | |
14893eb7 C |
123 | } |
124 | ||
125 | return next() | |
126 | } | |
127 | ] | |
128 | ||
54141398 C |
129 | // --------------------------------------------------------------------------- |
130 | ||
131 | export { | |
132 | followValidator, | |
0e9c48c2 | 133 | removeFollowingValidator, |
14893eb7 | 134 | getFollowerValidator, |
b8f4167f C |
135 | acceptOrRejectFollowerValidator, |
136 | listFollowsValidator | |
54141398 | 137 | } |