]>
Commit | Line | Data |
---|---|---|
1 | import cors from 'cors' | |
2 | import express from 'express' | |
3 | import { getServerActor } from '@server/models/application/application' | |
4 | import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models' | |
5 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' | |
6 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | |
7 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' | |
8 | import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants' | |
9 | import { audiencify, getAudience } from '../../lib/activitypub/audience' | |
10 | import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send' | |
11 | import { buildCreateActivity } from '../../lib/activitypub/send/send-create' | |
12 | import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' | |
13 | import { | |
14 | getLocalVideoCommentsActivityPubUrl, | |
15 | getLocalVideoDislikesActivityPubUrl, | |
16 | getLocalVideoLikesActivityPubUrl, | |
17 | getLocalVideoSharesActivityPubUrl | |
18 | } from '../../lib/activitypub/url' | |
19 | import { | |
20 | asyncMiddleware, | |
21 | ensureIsLocalChannel, | |
22 | executeIfActivityPub, | |
23 | localAccountValidator, | |
24 | videoChannelsNameWithHostValidator, | |
25 | videosCustomGetValidator, | |
26 | videosShareValidator | |
27 | } from '../../middlewares' | |
28 | import { cacheRoute } from '../../middlewares/cache/cache' | |
29 | import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators' | |
30 | import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy' | |
31 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' | |
32 | import { AccountModel } from '../../models/account/account' | |
33 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
34 | import { ActorFollowModel } from '../../models/actor/actor-follow' | |
35 | import { VideoCaptionModel } from '../../models/video/video-caption' | |
36 | import { VideoCommentModel } from '../../models/video/video-comment' | |
37 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | |
38 | import { VideoShareModel } from '../../models/video/video-share' | |
39 | import { activityPubResponse } from './utils' | |
40 | ||
41 | const activityPubClientRouter = express.Router() | |
42 | activityPubClientRouter.use(cors()) | |
43 | ||
44 | // Intercept ActivityPub client requests | |
45 | ||
46 | activityPubClientRouter.get( | |
47 | [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ], | |
48 | executeIfActivityPub, | |
49 | asyncMiddleware(localAccountValidator), | |
50 | accountController | |
51 | ) | |
52 | activityPubClientRouter.get('/accounts?/:name/followers', | |
53 | executeIfActivityPub, | |
54 | asyncMiddleware(localAccountValidator), | |
55 | asyncMiddleware(accountFollowersController) | |
56 | ) | |
57 | activityPubClientRouter.get('/accounts?/:name/following', | |
58 | executeIfActivityPub, | |
59 | asyncMiddleware(localAccountValidator), | |
60 | asyncMiddleware(accountFollowingController) | |
61 | ) | |
62 | activityPubClientRouter.get('/accounts?/:name/playlists', | |
63 | executeIfActivityPub, | |
64 | asyncMiddleware(localAccountValidator), | |
65 | asyncMiddleware(accountPlaylistsController) | |
66 | ) | |
67 | activityPubClientRouter.get('/accounts?/:name/likes/:videoId', | |
68 | executeIfActivityPub, | |
69 | asyncMiddleware(getAccountVideoRateValidatorFactory('like')), | |
70 | getAccountVideoRateFactory('like') | |
71 | ) | |
72 | activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId', | |
73 | executeIfActivityPub, | |
74 | asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')), | |
75 | getAccountVideoRateFactory('dislike') | |
76 | ) | |
77 | ||
78 | activityPubClientRouter.get( | |
79 | [ '/videos/watch/:id', '/w/:id' ], | |
80 | executeIfActivityPub, | |
81 | cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS), | |
82 | asyncMiddleware(videosCustomGetValidator('all')), | |
83 | asyncMiddleware(videoController) | |
84 | ) | |
85 | activityPubClientRouter.get('/videos/watch/:id/activity', | |
86 | executeIfActivityPub, | |
87 | asyncMiddleware(videosCustomGetValidator('all')), | |
88 | asyncMiddleware(videoController) | |
89 | ) | |
90 | activityPubClientRouter.get('/videos/watch/:id/announces', | |
91 | executeIfActivityPub, | |
92 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), | |
93 | asyncMiddleware(videoAnnouncesController) | |
94 | ) | |
95 | activityPubClientRouter.get('/videos/watch/:id/announces/:actorId', | |
96 | executeIfActivityPub, | |
97 | asyncMiddleware(videosShareValidator), | |
98 | asyncMiddleware(videoAnnounceController) | |
99 | ) | |
100 | activityPubClientRouter.get('/videos/watch/:id/likes', | |
101 | executeIfActivityPub, | |
102 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), | |
103 | asyncMiddleware(videoLikesController) | |
104 | ) | |
105 | activityPubClientRouter.get('/videos/watch/:id/dislikes', | |
106 | executeIfActivityPub, | |
107 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), | |
108 | asyncMiddleware(videoDislikesController) | |
109 | ) | |
110 | activityPubClientRouter.get('/videos/watch/:id/comments', | |
111 | executeIfActivityPub, | |
112 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), | |
113 | asyncMiddleware(videoCommentsController) | |
114 | ) | |
115 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId', | |
116 | executeIfActivityPub, | |
117 | asyncMiddleware(videoCommentGetValidator), | |
118 | asyncMiddleware(videoCommentController) | |
119 | ) | |
120 | activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity', | |
121 | executeIfActivityPub, | |
122 | asyncMiddleware(videoCommentGetValidator), | |
123 | asyncMiddleware(videoCommentController) | |
124 | ) | |
125 | ||
126 | activityPubClientRouter.get( | |
127 | [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ], | |
128 | executeIfActivityPub, | |
129 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
130 | ensureIsLocalChannel, | |
131 | videoChannelController | |
132 | ) | |
133 | activityPubClientRouter.get('/video-channels/:nameWithHost/followers', | |
134 | executeIfActivityPub, | |
135 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
136 | ensureIsLocalChannel, | |
137 | asyncMiddleware(videoChannelFollowersController) | |
138 | ) | |
139 | activityPubClientRouter.get('/video-channels/:nameWithHost/following', | |
140 | executeIfActivityPub, | |
141 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
142 | ensureIsLocalChannel, | |
143 | asyncMiddleware(videoChannelFollowingController) | |
144 | ) | |
145 | activityPubClientRouter.get('/video-channels/:nameWithHost/playlists', | |
146 | executeIfActivityPub, | |
147 | asyncMiddleware(videoChannelsNameWithHostValidator), | |
148 | ensureIsLocalChannel, | |
149 | asyncMiddleware(videoChannelPlaylistsController) | |
150 | ) | |
151 | ||
152 | activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', | |
153 | executeIfActivityPub, | |
154 | asyncMiddleware(videoFileRedundancyGetValidator), | |
155 | asyncMiddleware(videoRedundancyController) | |
156 | ) | |
157 | activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId', | |
158 | executeIfActivityPub, | |
159 | asyncMiddleware(videoPlaylistRedundancyGetValidator), | |
160 | asyncMiddleware(videoRedundancyController) | |
161 | ) | |
162 | ||
163 | activityPubClientRouter.get( | |
164 | [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ], | |
165 | executeIfActivityPub, | |
166 | asyncMiddleware(videoPlaylistsGetValidator('all')), | |
167 | asyncMiddleware(videoPlaylistController) | |
168 | ) | |
169 | activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId', | |
170 | executeIfActivityPub, | |
171 | asyncMiddleware(videoPlaylistElementAPGetValidator), | |
172 | videoPlaylistElementController | |
173 | ) | |
174 | ||
175 | // --------------------------------------------------------------------------- | |
176 | ||
177 | export { | |
178 | activityPubClientRouter | |
179 | } | |
180 | ||
181 | // --------------------------------------------------------------------------- | |
182 | ||
183 | function accountController (req: express.Request, res: express.Response) { | |
184 | const account = res.locals.account | |
185 | ||
186 | return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res) | |
187 | } | |
188 | ||
189 | async function accountFollowersController (req: express.Request, res: express.Response) { | |
190 | const account = res.locals.account | |
191 | const activityPubResult = await actorFollowers(req, account.Actor) | |
192 | ||
193 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
194 | } | |
195 | ||
196 | async function accountFollowingController (req: express.Request, res: express.Response) { | |
197 | const account = res.locals.account | |
198 | const activityPubResult = await actorFollowing(req, account.Actor) | |
199 | ||
200 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
201 | } | |
202 | ||
203 | async function accountPlaylistsController (req: express.Request, res: express.Response) { | |
204 | const account = res.locals.account | |
205 | const activityPubResult = await actorPlaylists(req, { account }) | |
206 | ||
207 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
208 | } | |
209 | ||
210 | async function videoChannelPlaylistsController (req: express.Request, res: express.Response) { | |
211 | const channel = res.locals.videoChannel | |
212 | const activityPubResult = await actorPlaylists(req, { channel }) | |
213 | ||
214 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
215 | } | |
216 | ||
217 | function getAccountVideoRateFactory (rateType: VideoRateType) { | |
218 | return (req: express.Request, res: express.Response) => { | |
219 | const accountVideoRate = res.locals.accountVideoRate | |
220 | ||
221 | const byActor = accountVideoRate.Account.Actor | |
222 | const APObject = rateType === 'like' | |
223 | ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video) | |
224 | : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video) | |
225 | ||
226 | return activityPubResponse(activityPubContextify(APObject), res) | |
227 | } | |
228 | } | |
229 | ||
230 | async function videoController (req: express.Request, res: express.Response) { | |
231 | const video = res.locals.videoAll | |
232 | ||
233 | if (redirectIfNotOwned(video.url, res)) return | |
234 | ||
235 | // We need captions to render AP object | |
236 | const captions = await VideoCaptionModel.listVideoCaptions(video.id) | |
237 | const videoWithCaptions = Object.assign(video, { VideoCaptions: captions }) | |
238 | ||
239 | const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC) | |
240 | const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience) | |
241 | ||
242 | if (req.path.endsWith('/activity')) { | |
243 | const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience) | |
244 | return activityPubResponse(activityPubContextify(data), res) | |
245 | } | |
246 | ||
247 | return activityPubResponse(activityPubContextify(videoObject), res) | |
248 | } | |
249 | ||
250 | async function videoAnnounceController (req: express.Request, res: express.Response) { | |
251 | const share = res.locals.videoShare | |
252 | ||
253 | if (redirectIfNotOwned(share.url, res)) return | |
254 | ||
255 | const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) | |
256 | ||
257 | return activityPubResponse(activityPubContextify(activity, 'Announce'), res) | |
258 | } | |
259 | ||
260 | async function videoAnnouncesController (req: express.Request, res: express.Response) { | |
261 | const video = res.locals.onlyImmutableVideo | |
262 | ||
263 | if (redirectIfNotOwned(video.url, res)) return | |
264 | ||
265 | const handler = async (start: number, count: number) => { | |
266 | const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) | |
267 | return { | |
268 | total: result.total, | |
269 | data: result.data.map(r => r.url) | |
270 | } | |
271 | } | |
272 | const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page) | |
273 | ||
274 | return activityPubResponse(activityPubContextify(json), res) | |
275 | } | |
276 | ||
277 | async function videoLikesController (req: express.Request, res: express.Response) { | |
278 | const video = res.locals.onlyImmutableVideo | |
279 | ||
280 | if (redirectIfNotOwned(video.url, res)) return | |
281 | ||
282 | const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video)) | |
283 | ||
284 | return activityPubResponse(activityPubContextify(json), res) | |
285 | } | |
286 | ||
287 | async function videoDislikesController (req: express.Request, res: express.Response) { | |
288 | const video = res.locals.onlyImmutableVideo | |
289 | ||
290 | if (redirectIfNotOwned(video.url, res)) return | |
291 | ||
292 | const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video)) | |
293 | ||
294 | return activityPubResponse(activityPubContextify(json), res) | |
295 | } | |
296 | ||
297 | async function videoCommentsController (req: express.Request, res: express.Response) { | |
298 | const video = res.locals.onlyImmutableVideo | |
299 | ||
300 | if (redirectIfNotOwned(video.url, res)) return | |
301 | ||
302 | const handler = async (start: number, count: number) => { | |
303 | const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count) | |
304 | ||
305 | return { | |
306 | total: result.total, | |
307 | data: result.data.map(r => r.url) | |
308 | } | |
309 | } | |
310 | const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page) | |
311 | ||
312 | return activityPubResponse(activityPubContextify(json), res) | |
313 | } | |
314 | ||
315 | function videoChannelController (req: express.Request, res: express.Response) { | |
316 | const videoChannel = res.locals.videoChannel | |
317 | ||
318 | return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res) | |
319 | } | |
320 | ||
321 | async function videoChannelFollowersController (req: express.Request, res: express.Response) { | |
322 | const videoChannel = res.locals.videoChannel | |
323 | const activityPubResult = await actorFollowers(req, videoChannel.Actor) | |
324 | ||
325 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
326 | } | |
327 | ||
328 | async function videoChannelFollowingController (req: express.Request, res: express.Response) { | |
329 | const videoChannel = res.locals.videoChannel | |
330 | const activityPubResult = await actorFollowing(req, videoChannel.Actor) | |
331 | ||
332 | return activityPubResponse(activityPubContextify(activityPubResult), res) | |
333 | } | |
334 | ||
335 | async function videoCommentController (req: express.Request, res: express.Response) { | |
336 | const videoComment = res.locals.videoCommentFull | |
337 | ||
338 | if (redirectIfNotOwned(videoComment.url, res)) return | |
339 | ||
340 | const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined) | |
341 | const isPublic = true // Comments are always public | |
342 | let videoCommentObject = videoComment.toActivityPubObject(threadParentComments) | |
343 | ||
344 | if (videoComment.Account) { | |
345 | const audience = getAudience(videoComment.Account.Actor, isPublic) | |
346 | videoCommentObject = audiencify(videoCommentObject, audience) | |
347 | ||
348 | if (req.path.endsWith('/activity')) { | |
349 | const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) | |
350 | return activityPubResponse(activityPubContextify(data), res) | |
351 | } | |
352 | } | |
353 | ||
354 | return activityPubResponse(activityPubContextify(videoCommentObject), res) | |
355 | } | |
356 | ||
357 | async function videoRedundancyController (req: express.Request, res: express.Response) { | |
358 | const videoRedundancy = res.locals.videoRedundancy | |
359 | ||
360 | if (redirectIfNotOwned(videoRedundancy.url, res)) return | |
361 | ||
362 | const serverActor = await getServerActor() | |
363 | ||
364 | const audience = getAudience(serverActor) | |
365 | const object = audiencify(videoRedundancy.toActivityPubObject(), audience) | |
366 | ||
367 | if (req.path.endsWith('/activity')) { | |
368 | const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) | |
369 | return activityPubResponse(activityPubContextify(data, 'CacheFile'), res) | |
370 | } | |
371 | ||
372 | return activityPubResponse(activityPubContextify(object, 'CacheFile'), res) | |
373 | } | |
374 | ||
375 | async function videoPlaylistController (req: express.Request, res: express.Response) { | |
376 | const playlist = res.locals.videoPlaylistFull | |
377 | ||
378 | if (redirectIfNotOwned(playlist.url, res)) return | |
379 | ||
380 | // We need more attributes | |
381 | playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId) | |
382 | ||
383 | const json = await playlist.toActivityPubObject(req.query.page, null) | |
384 | const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC) | |
385 | const object = audiencify(json, audience) | |
386 | ||
387 | return activityPubResponse(activityPubContextify(object), res) | |
388 | } | |
389 | ||
390 | function videoPlaylistElementController (req: express.Request, res: express.Response) { | |
391 | const videoPlaylistElement = res.locals.videoPlaylistElementAP | |
392 | ||
393 | if (redirectIfNotOwned(videoPlaylistElement.url, res)) return | |
394 | ||
395 | const json = videoPlaylistElement.toActivityPubObject() | |
396 | return activityPubResponse(activityPubContextify(json), res) | |
397 | } | |
398 | ||
399 | // --------------------------------------------------------------------------- | |
400 | ||
401 | async function actorFollowing (req: express.Request, actor: MActorId) { | |
402 | const handler = (start: number, count: number) => { | |
403 | return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) | |
404 | } | |
405 | ||
406 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | |
407 | } | |
408 | ||
409 | async function actorFollowers (req: express.Request, actor: MActorId) { | |
410 | const handler = (start: number, count: number) => { | |
411 | return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count) | |
412 | } | |
413 | ||
414 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | |
415 | } | |
416 | ||
417 | async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) { | |
418 | const handler = (start: number, count: number) => { | |
419 | return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count) | |
420 | } | |
421 | ||
422 | return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page) | |
423 | } | |
424 | ||
425 | function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) { | |
426 | const handler = async (start: number, count: number) => { | |
427 | const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) | |
428 | return { | |
429 | total: result.total, | |
430 | data: result.data.map(r => r.url) | |
431 | } | |
432 | } | |
433 | return activityPubCollectionPagination(url, handler, req.query.page) | |
434 | } | |
435 | ||
436 | function redirectIfNotOwned (url: string, res: express.Response) { | |
437 | if (url.startsWith(WEBSERVER.URL) === false) { | |
438 | res.redirect(url) | |
439 | return true | |
440 | } | |
441 | ||
442 | return false | |
443 | } |