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