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