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