]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Refactor AP context builder
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
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, 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 // ---------------------------------------------------------------------------
179
180 export {
181 activityPubClientRouter
182 }
183
184 // ---------------------------------------------------------------------------
185
186 function accountController (req: express.Request, res: express.Response) {
187 const account = res.locals.account
188
189 return activityPubResponse(activityPubContextify(account.toActivityPubObject(), 'Actor'), res)
190 }
191
192 async function accountFollowersController (req: express.Request, res: express.Response) {
193 const account = res.locals.account
194 const activityPubResult = await actorFollowers(req, account.Actor)
195
196 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
197 }
198
199 async function accountFollowingController (req: express.Request, res: express.Response) {
200 const account = res.locals.account
201 const activityPubResult = await actorFollowing(req, account.Actor)
202
203 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
204 }
205
206 async function accountPlaylistsController (req: express.Request, res: express.Response) {
207 const account = res.locals.account
208 const activityPubResult = await actorPlaylists(req, { account })
209
210 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
211 }
212
213 async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
214 const channel = res.locals.videoChannel
215 const activityPubResult = await actorPlaylists(req, { channel })
216
217 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
218 }
219
220 function getAccountVideoRateFactory (rateType: VideoRateType) {
221 return (req: express.Request, res: express.Response) => {
222 const accountVideoRate = res.locals.accountVideoRate
223
224 const byActor = accountVideoRate.Account.Actor
225 const APObject = rateType === 'like'
226 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
227 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
228
229 return activityPubResponse(activityPubContextify(APObject, 'Rate'), res)
230 }
231 }
232
233 async function videoController (req: express.Request, res: express.Response) {
234 const video = res.locals.videoAll
235
236 if (redirectIfNotOwned(video.url, res)) return
237
238 // We need captions to render AP object
239 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
240 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
241
242 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
243 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
244
245 if (req.path.endsWith('/activity')) {
246 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
247 return activityPubResponse(activityPubContextify(data, 'Video'), res)
248 }
249
250 return activityPubResponse(activityPubContextify(videoObject, 'Video'), res)
251 }
252
253 async function videoAnnounceController (req: express.Request, res: express.Response) {
254 const share = res.locals.videoShare
255
256 if (redirectIfNotOwned(share.url, res)) return
257
258 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
259
260 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
261 }
262
263 async function videoAnnouncesController (req: express.Request, res: express.Response) {
264 const video = res.locals.onlyImmutableVideo
265
266 if (redirectIfNotOwned(video.url, res)) return
267
268 const handler = async (start: number, count: number) => {
269 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
270 return {
271 total: result.total,
272 data: result.data.map(r => r.url)
273 }
274 }
275 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
276
277 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
278 }
279
280 async function videoLikesController (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, 'like', video, getLocalVideoLikesActivityPubUrl(video))
286
287 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
288 }
289
290 async function videoDislikesController (req: express.Request, res: express.Response) {
291 const video = res.locals.onlyImmutableVideo
292
293 if (redirectIfNotOwned(video.url, res)) return
294
295 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
296
297 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
298 }
299
300 async function videoCommentsController (req: express.Request, res: express.Response) {
301 const video = res.locals.onlyImmutableVideo
302
303 if (redirectIfNotOwned(video.url, res)) return
304
305 const handler = async (start: number, count: number) => {
306 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
307
308 return {
309 total: result.total,
310 data: result.data.map(r => r.url)
311 }
312 }
313 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
314
315 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
316 }
317
318 function videoChannelController (req: express.Request, res: express.Response) {
319 const videoChannel = res.locals.videoChannel
320
321 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject(), 'Actor'), res)
322 }
323
324 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
325 const videoChannel = res.locals.videoChannel
326 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
327
328 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
329 }
330
331 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
332 const videoChannel = res.locals.videoChannel
333 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
334
335 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
336 }
337
338 async function videoCommentController (req: express.Request, res: express.Response) {
339 const videoComment = res.locals.videoCommentFull
340
341 if (redirectIfNotOwned(videoComment.url, res)) return
342
343 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
344 const isPublic = true // Comments are always public
345 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
346
347 if (videoComment.Account) {
348 const audience = getAudience(videoComment.Account.Actor, isPublic)
349 videoCommentObject = audiencify(videoCommentObject, audience)
350
351 if (req.path.endsWith('/activity')) {
352 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
353 return activityPubResponse(activityPubContextify(data, 'Comment'), res)
354 }
355 }
356
357 return activityPubResponse(activityPubContextify(videoCommentObject, 'Comment'), res)
358 }
359
360 async function videoRedundancyController (req: express.Request, res: express.Response) {
361 const videoRedundancy = res.locals.videoRedundancy
362
363 if (redirectIfNotOwned(videoRedundancy.url, res)) return
364
365 const serverActor = await getServerActor()
366
367 const audience = getAudience(serverActor)
368 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
369
370 if (req.path.endsWith('/activity')) {
371 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
372 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
373 }
374
375 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
376 }
377
378 async function videoPlaylistController (req: express.Request, res: express.Response) {
379 const playlist = res.locals.videoPlaylistFull
380
381 if (redirectIfNotOwned(playlist.url, res)) return
382
383 // We need more attributes
384 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
385
386 const json = await playlist.toActivityPubObject(req.query.page, null)
387 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
388 const object = audiencify(json, audience)
389
390 return activityPubResponse(activityPubContextify(object, 'Playlist'), res)
391 }
392
393 function videoPlaylistElementController (req: express.Request, res: express.Response) {
394 const videoPlaylistElement = res.locals.videoPlaylistElementAP
395
396 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
397
398 const json = videoPlaylistElement.toActivityPubObject()
399 return activityPubResponse(activityPubContextify(json, 'Playlist'), res)
400 }
401
402 // ---------------------------------------------------------------------------
403
404 function actorFollowing (req: express.Request, actor: MActorId) {
405 const handler = (start: number, count: number) => {
406 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
407 }
408
409 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
410 }
411
412 function actorFollowers (req: express.Request, actor: MActorId) {
413 const handler = (start: number, count: number) => {
414 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
415 }
416
417 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418 }
419
420 function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
421 const handler = (start: number, count: number) => {
422 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
423 }
424
425 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
426 }
427
428 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
429 const handler = async (start: number, count: number) => {
430 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
431 return {
432 total: result.total,
433 data: result.data.map(r => r.url)
434 }
435 }
436 return activityPubCollectionPagination(url, handler, req.query.page)
437 }
438
439 function redirectIfNotOwned (url: string, res: express.Response) {
440 if (url.startsWith(WEBSERVER.URL) === false) {
441 res.redirect(url)
442 return true
443 }
444
445 return false
446 }