]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
b8f4167f | 2 | import { body, param, query } from 'express-validator' |
9452d4fd | 3 | import { isProdInstance } from '@server/helpers/core-utils' |
4d029ef8 | 4 | import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows' |
10363c74 | 5 | import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors' |
4d029ef8 | 6 | import { getRemoteNameAndHost } from '@server/lib/activitypub/follow' |
7d9ba5c0 C |
7 | import { getServerActor } from '@server/models/application/application' |
8 | import { MActorFollowActorsDefault } from '@server/types/models' | |
9452d4fd | 9 | import { ServerFollowCreate } from '@shared/models' |
c0e8b12e | 10 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
7d9ba5c0 | 11 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
50d6de9c | 12 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
da854ddd | 13 | import { logger } from '../../helpers/logger' |
4d029ef8 | 14 | import { WEBSERVER } from '../../initializers/constants' |
7d9ba5c0 C |
15 | import { ActorModel } from '../../models/actor/actor' |
16 | import { ActorFollowModel } from '../../models/actor/actor-follow' | |
10363c74 | 17 | import { areValidationErrors } from './shared' |
b8f4167f C |
18 | |
19 | const listFollowsValidator = [ | |
20 | query('state') | |
21 | .optional() | |
22 | .custom(isFollowStateValid).withMessage('Should have a valid follow state'), | |
97ecddae C |
23 | query('actorType') |
24 | .optional() | |
25 | .custom(isActorTypeValid).withMessage('Should have a valid actor type'), | |
b8f4167f C |
26 | |
27 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
28 | if (areValidationErrors(req, res)) return | |
29 | ||
30 | return next() | |
31 | } | |
32 | ] | |
54141398 C |
33 | |
34 | const followValidator = [ | |
4d029ef8 C |
35 | body('hosts') |
36 | .toArray() | |
37 | .custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'), | |
38 | ||
39 | body('handles') | |
40 | .toArray() | |
41 | .custom(isEachUniqueHandleValid).withMessage('Should have an array of handles'), | |
54141398 C |
42 | |
43 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
4d029ef8 | 44 | // Force https if the administrator wants to follow remote actors |
9452d4fd | 45 | if (isProdInstance() && WEBSERVER.SCHEME === 'http') { |
2d53be02 RK |
46 | return res |
47 | .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500) | |
54141398 | 48 | .json({ |
48dce1c9 | 49 | error: 'Cannot follow on a non HTTPS web server.' |
54141398 | 50 | }) |
54141398 C |
51 | } |
52 | ||
53 | logger.debug('Checking follow parameters', { parameters: req.body }) | |
54 | ||
a2431b7d C |
55 | if (areValidationErrors(req, res)) return |
56 | ||
4d029ef8 C |
57 | const body: ServerFollowCreate = req.body |
58 | if (body.hosts.length === 0 && body.handles.length === 0) { | |
59 | ||
60 | return res | |
61 | .status(HttpStatusCode.BAD_REQUEST_400) | |
62 | .json({ | |
63 | error: 'You must provide at least one handle or one host.' | |
64 | }) | |
65 | } | |
66 | ||
a2431b7d | 67 | return next() |
54141398 C |
68 | } |
69 | ] | |
70 | ||
71 | const removeFollowingValidator = [ | |
4d029ef8 C |
72 | param('hostOrHandle') |
73 | .custom(value => isHostValid(value) || isRemoteHandleValid(value)) | |
74 | .withMessage('Should have a valid host/handle'), | |
54141398 | 75 | |
a2431b7d | 76 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
0e9c48c2 | 77 | logger.debug('Checking unfollowing parameters', { parameters: req.params }) |
54141398 | 78 | |
a2431b7d | 79 | if (areValidationErrors(req, res)) return |
54141398 | 80 | |
50d6de9c | 81 | const serverActor = await getServerActor() |
4d029ef8 C |
82 | |
83 | const { name, host } = getRemoteNameAndHost(req.params.hostOrHandle) | |
927fa4b1 C |
84 | const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({ |
85 | actorId: serverActor.id, | |
86 | targetName: name, | |
87 | targetHost: host | |
88 | }) | |
54141398 | 89 | |
a2431b7d | 90 | if (!follow) { |
76148b27 RK |
91 | return res.fail({ |
92 | status: HttpStatusCode.NOT_FOUND_404, | |
4d029ef8 | 93 | message: `Follow ${req.params.hostOrHandle} not found.` |
76148b27 | 94 | }) |
0e9c48c2 C |
95 | } |
96 | ||
97 | res.locals.follow = follow | |
98 | return next() | |
99 | } | |
100 | ] | |
101 | ||
14893eb7 | 102 | const getFollowerValidator = [ |
0e9c48c2 C |
103 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), |
104 | ||
105 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14893eb7 | 106 | logger.debug('Checking get follower parameters', { parameters: req.params }) |
0e9c48c2 C |
107 | |
108 | if (areValidationErrors(req, res)) return | |
109 | ||
453e83ea | 110 | let follow: MActorFollowActorsDefault |
5b9c965d C |
111 | try { |
112 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | |
113 | const actor = await ActorModel.loadByUrl(actorUrl) | |
0e9c48c2 | 114 | |
5b9c965d C |
115 | const serverActor = await getServerActor() |
116 | follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | |
117 | } catch (err) { | |
118 | logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err }) | |
119 | } | |
0e9c48c2 C |
120 | |
121 | if (!follow) { | |
76148b27 RK |
122 | return res.fail({ |
123 | status: HttpStatusCode.NOT_FOUND_404, | |
124 | message: `Follower ${req.params.nameWithHost} not found.` | |
125 | }) | |
a2431b7d | 126 | } |
54141398 | 127 | |
a2431b7d C |
128 | res.locals.follow = follow |
129 | return next() | |
54141398 C |
130 | } |
131 | ] | |
132 | ||
927fa4b1 | 133 | const acceptFollowerValidator = [ |
14893eb7 | 134 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
927fa4b1 | 135 | logger.debug('Checking accept follower parameters', { parameters: req.params }) |
14893eb7 C |
136 | |
137 | const follow = res.locals.follow | |
927fa4b1 C |
138 | if (follow.state !== 'pending' && follow.state !== 'rejected') { |
139 | return res.fail({ message: 'Follow is not in pending/rejected state.' }) | |
140 | } | |
141 | ||
142 | return next() | |
143 | } | |
144 | ] | |
145 | ||
146 | const rejectFollowerValidator = [ | |
147 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
148 | logger.debug('Checking reject follower parameters', { parameters: req.params }) | |
149 | ||
150 | const follow = res.locals.follow | |
151 | if (follow.state !== 'pending' && follow.state !== 'accepted') { | |
152 | return res.fail({ message: 'Follow is not in pending/accepted state.' }) | |
14893eb7 C |
153 | } |
154 | ||
155 | return next() | |
156 | } | |
157 | ] | |
158 | ||
54141398 C |
159 | // --------------------------------------------------------------------------- |
160 | ||
161 | export { | |
162 | followValidator, | |
0e9c48c2 | 163 | removeFollowingValidator, |
14893eb7 | 164 | getFollowerValidator, |
927fa4b1 C |
165 | acceptFollowerValidator, |
166 | rejectFollowerValidator, | |
b8f4167f | 167 | listFollowsValidator |
54141398 | 168 | } |