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