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