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