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