]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
b8f4167f | 2 | import { body, param, query } from 'express-validator' |
4d029ef8 | 3 | import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows' |
10363c74 | 4 | import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors' |
4d029ef8 | 5 | import { getRemoteNameAndHost } from '@server/lib/activitypub/follow' |
7d9ba5c0 C |
6 | import { getServerActor } from '@server/models/application/application' |
7 | import { MActorFollowActorsDefault } from '@server/types/models' | |
c0e8b12e | 8 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
da854ddd | 9 | import { isTestInstance } from '../../helpers/core-utils' |
7d9ba5c0 | 10 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
50d6de9c | 11 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
da854ddd | 12 | import { logger } from '../../helpers/logger' |
4d029ef8 | 13 | import { WEBSERVER } from '../../initializers/constants' |
7d9ba5c0 C |
14 | import { ActorModel } from '../../models/actor/actor' |
15 | import { ActorFollowModel } from '../../models/actor/actor-follow' | |
10363c74 | 16 | import { areValidationErrors } from './shared' |
4d029ef8 | 17 | import { ServerFollowCreate } from '@shared/models' |
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 |
6dd9de95 | 45 | if (isTestInstance() === false && 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) | |
84 | const follow = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(serverActor.id, name, host) | |
54141398 | 85 | |
a2431b7d | 86 | if (!follow) { |
76148b27 RK |
87 | return res.fail({ |
88 | status: HttpStatusCode.NOT_FOUND_404, | |
4d029ef8 | 89 | message: `Follow ${req.params.hostOrHandle} not found.` |
76148b27 | 90 | }) |
0e9c48c2 C |
91 | } |
92 | ||
93 | res.locals.follow = follow | |
94 | return next() | |
95 | } | |
96 | ] | |
97 | ||
14893eb7 | 98 | const getFollowerValidator = [ |
0e9c48c2 C |
99 | param('nameWithHost').custom(isValidActorHandle).withMessage('Should have a valid nameWithHost'), |
100 | ||
101 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
14893eb7 | 102 | logger.debug('Checking get follower parameters', { parameters: req.params }) |
0e9c48c2 C |
103 | |
104 | if (areValidationErrors(req, res)) return | |
105 | ||
453e83ea | 106 | let follow: MActorFollowActorsDefault |
5b9c965d C |
107 | try { |
108 | const actorUrl = await loadActorUrlOrGetFromWebfinger(req.params.nameWithHost) | |
109 | const actor = await ActorModel.loadByUrl(actorUrl) | |
0e9c48c2 | 110 | |
5b9c965d C |
111 | const serverActor = await getServerActor() |
112 | follow = await ActorFollowModel.loadByActorAndTarget(actor.id, serverActor.id) | |
113 | } catch (err) { | |
114 | logger.warn('Cannot get actor from handle.', { handle: req.params.nameWithHost, err }) | |
115 | } | |
0e9c48c2 C |
116 | |
117 | if (!follow) { | |
76148b27 RK |
118 | return res.fail({ |
119 | status: HttpStatusCode.NOT_FOUND_404, | |
120 | message: `Follower ${req.params.nameWithHost} not found.` | |
121 | }) | |
a2431b7d | 122 | } |
54141398 | 123 | |
a2431b7d C |
124 | res.locals.follow = follow |
125 | return next() | |
54141398 C |
126 | } |
127 | ] | |
128 | ||
14893eb7 C |
129 | const acceptOrRejectFollowerValidator = [ |
130 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
131 | logger.debug('Checking accept/reject follower parameters', { parameters: req.params }) | |
132 | ||
133 | const follow = res.locals.follow | |
134 | if (follow.state !== 'pending') { | |
76148b27 | 135 | return res.fail({ message: 'Follow is not in pending state.' }) |
14893eb7 C |
136 | } |
137 | ||
138 | return next() | |
139 | } | |
140 | ] | |
141 | ||
54141398 C |
142 | // --------------------------------------------------------------------------- |
143 | ||
144 | export { | |
145 | followValidator, | |
0e9c48c2 | 146 | removeFollowingValidator, |
14893eb7 | 147 | getFollowerValidator, |
b8f4167f C |
148 | acceptOrRejectFollowerValidator, |
149 | listFollowsValidator | |
54141398 | 150 | } |