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