]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param, query } from 'express-validator' | |
3 | import { isTestInstance } from '../../helpers/core-utils' | |
4 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' | |
5 | import { logger } from '../../helpers/logger' | |
6 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' | |
7 | import { ActorFollowModel } from '../../models/activitypub/actor-follow' | |
8 | import { areValidationErrors } from './utils' | |
9 | import { ActorModel } from '../../models/activitypub/actor' | |
10 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | |
11 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | |
12 | import { MActorFollowActorsDefault } from '@server/types/models' | |
13 | import { isFollowStateValid } from '@server/helpers/custom-validators/follows' | |
14 | import { getServerActor } from '@server/models/application/application' | |
15 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
16 | ||
17 | const listFollowsValidator = [ | |
18 | query('state') | |
19 | .optional() | |
20 | .custom(isFollowStateValid).withMessage('Should have a valid follow state'), | |
21 | query('actorType') | |
22 | .optional() | |
23 | .custom(isActorTypeValid).withMessage('Should have a valid actor type'), | |
24 | ||
25 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
26 | if (areValidationErrors(req, res)) return | |
27 | ||
28 | return next() | |
29 | } | |
30 | ] | |
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 | |
37 | if (isTestInstance() === false && WEBSERVER.SCHEME === 'http') { | |
38 | return res | |
39 | .status(HttpStatusCode.INTERNAL_SERVER_ERROR_500) | |
40 | .json({ | |
41 | error: 'Cannot follow on a non HTTPS web server.' | |
42 | }) | |
43 | .end() | |
44 | } | |
45 | ||
46 | logger.debug('Checking follow parameters', { parameters: req.body }) | |
47 | ||
48 | if (areValidationErrors(req, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
54 | const removeFollowingValidator = [ | |
55 | param('host').custom(isHostValid).withMessage('Should have a valid host'), | |
56 | ||
57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
58 | logger.debug('Checking unfollowing parameters', { parameters: req.params }) | |
59 | ||
60 | if (areValidationErrors(req, res)) return | |
61 | ||
62 | const serverActor = await getServerActor() | |
63 | const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, SERVER_ACTOR_NAME, req.params.host) | |
64 | ||
65 | if (!follow) { | |
66 | return res | |
67 | .status(HttpStatusCode.NOT_FOUND_404) | |
68 | .json({ | |
69 | error: `Following ${req.params.host} not found.` | |
70 | }) | |
71 | .end() | |
72 | } | |
73 | ||
74 | res.locals.follow = follow | |
75 | return next() | |
76 | } | |
77 | ] | |
78 | ||
79 | const getFollowerValidator = [ | |
80 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), | |
81 | ||
82 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
83 | logger.debug('Checking get follower parameters', { parameters: req.params }) | |
84 | ||
85 | if (areValidationErrors(req, res)) return | |
86 | ||
87 | let follow: MActorFollowActorsDefault | |
88 | try { | |
89 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | |
90 | const actor = await ActorModel.loadByUrl(actorUrl) | |
91 | ||
92 | const serverActor = await getServerActor() | |
93 | follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | |
94 | } catch (err) { | |
95 | logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err }) | |
96 | } | |
97 | ||
98 | if (!follow) { | |
99 | return res | |
100 | .status(HttpStatusCode.NOT_FOUND_404) | |
101 | .json({ | |
102 | error: `Follower ${req.params.nameWithHost} not found.` | |
103 | }) | |
104 | .end() | |
105 | } | |
106 | ||
107 | res.locals.follow = follow | |
108 | return next() | |
109 | } | |
110 | ] | |
111 | ||
112 | const acceptOrRejectFollowerValidator = [ | |
113 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
114 | logger.debug('Checking accept/reject follower parameters', { parameters: req.params }) | |
115 | ||
116 | const follow = res.locals.follow | |
117 | if (follow.state !== 'pending') { | |
118 | return res | |
119 | .status(HttpStatusCode.BAD_REQUEST_400) | |
120 | .json({ | |
121 | error: 'Follow is not in pending state.' | |
122 | }) | |
123 | .end() | |
124 | } | |
125 | ||
126 | return next() | |
127 | } | |
128 | ] | |
129 | ||
130 | // --------------------------------------------------------------------------- | |
131 | ||
132 | export { | |
133 | followValidator, | |
134 | removeFollowingValidator, | |
135 | getFollowerValidator, | |
136 | acceptOrRejectFollowerValidator, | |
137 | listFollowsValidator | |
138 | } |