]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Speed up plugin transcoding tests
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
CommitLineData
670e955c 1import * as cors from 'cors'
053aed43 2import * as express from 'express'
053aed43 3import { getServerActor } from '@server/models/application/application'
55d5fbc5 4import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models'
8fffe21a 5import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
053aed43 6import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
d6e99e53 7import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
74dc3bca 8import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
e251f170 9import { audiencify, getAudience } from '../../lib/activitypub/audience'
053aed43 10import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
c48e82b5 11import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
053aed43
C
12import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
13import {
de94ac86
C
14 getLocalVideoCommentsActivityPubUrl,
15 getLocalVideoDislikesActivityPubUrl,
16 getLocalVideoLikesActivityPubUrl,
17 getLocalVideoSharesActivityPubUrl
053aed43 18} from '../../lib/activitypub/url'
96f29c0f
C
19import {
20 asyncMiddleware,
21 executeIfActivityPub,
22 localAccountValidator,
23 localVideoChannelValidator,
1e7eb25f
C
24 videosCustomGetValidator,
25 videosShareValidator
96f29c0f 26} from '../../middlewares'
053aed43 27import { cacheRoute } from '../../middlewares/cache'
75ba887d 28import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators'
053aed43
C
29import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
30import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
3fd3ab2d 31import { AccountModel } from '../../models/account/account'
053aed43 32import { AccountVideoRateModel } from '../../models/account/account-video-rate'
7d9ba5c0 33import { ActorFollowModel } from '../../models/actor/actor-follow'
053aed43 34import { VideoCaptionModel } from '../../models/video/video-caption'
da854ddd 35import { VideoCommentModel } from '../../models/video/video-comment'
053aed43 36import { VideoPlaylistModel } from '../../models/video/video-playlist'
3fd3ab2d 37import { VideoShareModel } from '../../models/video/video-share'
8fffe21a 38import { activityPubResponse } from './utils'
e4f97bab
C
39
40const activityPubClientRouter = express.Router()
670e955c 41activityPubClientRouter.use(cors())
e4f97bab 42
2c8776fc
C
43// Intercept ActivityPub client requests
44
70fdff3d 45activityPubClientRouter.get(
559985b5 46 [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ],
e65c0c5b
C
47 executeIfActivityPub,
48 asyncMiddleware(localAccountValidator),
49 accountController
e4f97bab 50)
7006bc63 51activityPubClientRouter.get('/accounts?/:name/followers',
e65c0c5b
C
52 executeIfActivityPub,
53 asyncMiddleware(localAccountValidator),
54 asyncMiddleware(accountFollowersController)
e4f97bab 55)
7006bc63 56activityPubClientRouter.get('/accounts?/:name/following',
e65c0c5b
C
57 executeIfActivityPub,
58 asyncMiddleware(localAccountValidator),
59 asyncMiddleware(accountFollowingController)
e4f97bab 60)
418d092a 61activityPubClientRouter.get('/accounts?/:name/playlists',
e65c0c5b
C
62 executeIfActivityPub,
63 asyncMiddleware(localAccountValidator),
64 asyncMiddleware(accountPlaylistsController)
418d092a 65)
5c6d985f 66activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
e65c0c5b 67 executeIfActivityPub,
75ba887d
C
68 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
69 getAccountVideoRateFactory('like')
5c6d985f
C
70)
71activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
e65c0c5b 72 executeIfActivityPub,
75ba887d
C
73 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
74 getAccountVideoRateFactory('dislike')
5c6d985f 75)
e4f97bab 76
559985b5
C
77activityPubClientRouter.get(
78 [ '/videos/watch/:id', '/w/:id' ],
e65c0c5b 79 executeIfActivityPub,
f2f0eda5 80 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
71d4af1e 81 asyncMiddleware(videosCustomGetValidator('all')),
e65c0c5b 82 asyncMiddleware(videoController)
4e50b6a1 83)
296c0905 84activityPubClientRouter.get('/videos/watch/:id/activity',
e65c0c5b 85 executeIfActivityPub,
71d4af1e 86 asyncMiddleware(videosCustomGetValidator('all')),
e65c0c5b 87 asyncMiddleware(videoController)
296c0905 88)
46531a0a 89activityPubClientRouter.get('/videos/watch/:id/announces',
e65c0c5b 90 executeIfActivityPub,
7eba5e1f 91 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
e65c0c5b 92 asyncMiddleware(videoAnnouncesController)
46531a0a 93)
5c6d985f 94activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
e65c0c5b
C
95 executeIfActivityPub,
96 asyncMiddleware(videosShareValidator),
97 asyncMiddleware(videoAnnounceController)
20494f12 98)
46531a0a 99activityPubClientRouter.get('/videos/watch/:id/likes',
e65c0c5b 100 executeIfActivityPub,
7eba5e1f 101 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
e65c0c5b 102 asyncMiddleware(videoLikesController)
46531a0a
C
103)
104activityPubClientRouter.get('/videos/watch/:id/dislikes',
e65c0c5b 105 executeIfActivityPub,
7eba5e1f 106 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
e65c0c5b 107 asyncMiddleware(videoDislikesController)
46531a0a
C
108)
109activityPubClientRouter.get('/videos/watch/:id/comments',
e65c0c5b 110 executeIfActivityPub,
7eba5e1f 111 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
e65c0c5b 112 asyncMiddleware(videoCommentsController)
46531a0a 113)
da854ddd 114activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
e65c0c5b
C
115 executeIfActivityPub,
116 asyncMiddleware(videoCommentGetValidator),
117 asyncMiddleware(videoCommentController)
da854ddd 118)
296c0905 119activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
e65c0c5b
C
120 executeIfActivityPub,
121 asyncMiddleware(videoCommentGetValidator),
122 asyncMiddleware(videoCommentController)
296c0905 123)
da854ddd 124
70fdff3d 125activityPubClientRouter.get(
559985b5 126 [ '/video-channels/:name', '/video-channels/:name/videos', '/c/:name', '/c/:name/videos' ],
e65c0c5b
C
127 executeIfActivityPub,
128 asyncMiddleware(localVideoChannelValidator),
a1587156 129 videoChannelController
20494f12 130)
8a19bee1 131activityPubClientRouter.get('/video-channels/:name/followers',
e65c0c5b
C
132 executeIfActivityPub,
133 asyncMiddleware(localVideoChannelValidator),
134 asyncMiddleware(videoChannelFollowersController)
7006bc63 135)
8a19bee1 136activityPubClientRouter.get('/video-channels/:name/following',
e65c0c5b
C
137 executeIfActivityPub,
138 asyncMiddleware(localVideoChannelValidator),
139 asyncMiddleware(videoChannelFollowingController)
7006bc63 140)
7405b6ba
C
141activityPubClientRouter.get('/video-channels/:name/playlists',
142 executeIfActivityPub,
143 asyncMiddleware(localVideoChannelValidator),
144 asyncMiddleware(videoChannelPlaylistsController)
145)
20494f12 146
c48e82b5 147activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
e65c0c5b
C
148 executeIfActivityPub,
149 asyncMiddleware(videoFileRedundancyGetValidator),
150 asyncMiddleware(videoRedundancyController)
09209296 151)
9c6ca37f 152activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
e65c0c5b
C
153 executeIfActivityPub,
154 asyncMiddleware(videoPlaylistRedundancyGetValidator),
155 asyncMiddleware(videoRedundancyController)
c48e82b5
C
156)
157
37a44fc9
C
158activityPubClientRouter.get(
159 [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ],
e65c0c5b 160 executeIfActivityPub,
453e83ea 161 asyncMiddleware(videoPlaylistsGetValidator('all')),
e65c0c5b 162 asyncMiddleware(videoPlaylistController)
418d092a 163)
37190663 164activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
e65c0c5b
C
165 executeIfActivityPub,
166 asyncMiddleware(videoPlaylistElementAPGetValidator),
a1587156 167 videoPlaylistElementController
418d092a
C
168)
169
e4f97bab
C
170// ---------------------------------------------------------------------------
171
172export {
173 activityPubClientRouter
174}
175
176// ---------------------------------------------------------------------------
177
418d092a 178function accountController (req: express.Request, res: express.Response) {
dae86118 179 const account = res.locals.account
e4f97bab 180
4b8f09fa 181 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
e4f97bab
C
182}
183
418d092a 184async function accountFollowersController (req: express.Request, res: express.Response) {
dae86118 185 const account = res.locals.account
7006bc63 186 const activityPubResult = await actorFollowers(req, account.Actor)
e4f97bab 187
4b8f09fa 188 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab
C
189}
190
418d092a 191async function accountFollowingController (req: express.Request, res: express.Response) {
dae86118 192 const account = res.locals.account
7006bc63 193 const activityPubResult = await actorFollowing(req, account.Actor)
e4f97bab 194
4b8f09fa 195 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab 196}
20494f12 197
418d092a 198async function accountPlaylistsController (req: express.Request, res: express.Response) {
dae86118 199 const account = res.locals.account
7405b6ba
C
200 const activityPubResult = await actorPlaylists(req, { account })
201
202 return activityPubResponse(activityPubContextify(activityPubResult), res)
203}
204
205async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
206 const channel = res.locals.videoChannel
207 const activityPubResult = await actorPlaylists(req, { channel })
418d092a
C
208
209 return activityPubResponse(activityPubContextify(activityPubResult), res)
210}
211
75ba887d 212function getAccountVideoRateFactory (rateType: VideoRateType) {
5c6d985f 213 return (req: express.Request, res: express.Response) => {
dae86118 214 const accountVideoRate = res.locals.accountVideoRate
5c6d985f
C
215
216 const byActor = accountVideoRate.Account.Actor
5c6d985f 217 const APObject = rateType === 'like'
de94ac86
C
218 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
219 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
5c6d985f
C
220
221 return activityPubResponse(activityPubContextify(APObject), res)
222 }
223}
224
1a8dd4da 225async function videoController (req: express.Request, res: express.Response) {
71d4af1e 226 const video = res.locals.videoAll
20494f12 227
de94ac86 228 if (redirectIfNotOwned(video.url, res)) return
8d1fa36a 229
40e87e9e 230 // We need captions to render AP object
453e83ea 231 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
b5fecbf4 232 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
40e87e9e 233
453e83ea
C
234 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
235 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
2ccaeeb3 236
296c0905 237 if (req.path.endsWith('/activity')) {
453e83ea 238 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
4b8f09fa 239 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
240 }
241
4b8f09fa 242 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
243}
244
1a8dd4da 245async function videoAnnounceController (req: express.Request, res: express.Response) {
dae86118 246 const share = res.locals.videoShare
8d1fa36a 247
de94ac86 248 if (redirectIfNotOwned(share.url, res)) return
8d1fa36a 249
453e83ea 250 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
4e50b6a1 251
598edb8a 252 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
4e50b6a1
C
253}
254
1a8dd4da 255async function videoAnnouncesController (req: express.Request, res: express.Response) {
7eba5e1f 256 const video = res.locals.onlyImmutableVideo
46531a0a 257
de94ac86
C
258 if (redirectIfNotOwned(video.url, res)) return
259
8fffe21a
C
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 }
de94ac86 267 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
46531a0a 268
8fffe21a 269 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
270}
271
1a8dd4da 272async function videoLikesController (req: express.Request, res: express.Response) {
7eba5e1f 273 const video = res.locals.onlyImmutableVideo
de94ac86
C
274
275 if (redirectIfNotOwned(video.url, res)) return
276
277 const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
46531a0a 278
8fffe21a 279 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
280}
281
1a8dd4da 282async function videoDislikesController (req: express.Request, res: express.Response) {
7eba5e1f 283 const video = res.locals.onlyImmutableVideo
de94ac86
C
284
285 if (redirectIfNotOwned(video.url, res)) return
286
287 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
46531a0a 288
8fffe21a 289 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
290}
291
1a8dd4da 292async function videoCommentsController (req: express.Request, res: express.Response) {
7eba5e1f 293 const video = res.locals.onlyImmutableVideo
46531a0a 294
de94ac86
C
295 if (redirectIfNotOwned(video.url, res)) return
296
8fffe21a 297 const handler = async (start: number, count: number) => {
696d83fd 298 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
8fffe21a
C
299 return {
300 total: result.count,
301 data: result.rows.map(r => r.url)
302 }
303 }
de94ac86 304 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
46531a0a 305
8fffe21a 306 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
307}
308
a1587156 309function videoChannelController (req: express.Request, res: express.Response) {
dae86118 310 const videoChannel = res.locals.videoChannel
20494f12 311
4b8f09fa 312 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
20494f12 313}
da854ddd 314
1a8dd4da 315async function videoChannelFollowersController (req: express.Request, res: express.Response) {
dae86118 316 const videoChannel = res.locals.videoChannel
7006bc63
C
317 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
318
4b8f09fa 319 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
320}
321
1a8dd4da 322async function videoChannelFollowingController (req: express.Request, res: express.Response) {
dae86118 323 const videoChannel = res.locals.videoChannel
7006bc63
C
324 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
325
4b8f09fa 326 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
327}
328
1a8dd4da 329async function videoCommentController (req: express.Request, res: express.Response) {
453e83ea 330 const videoComment = res.locals.videoCommentFull
da854ddd 331
de94ac86 332 if (redirectIfNotOwned(videoComment.url, res)) return
8d1fa36a 333
d7e70384 334 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53 335 const isPublic = true // Comments are always public
69222afa 336 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
d6e99e53 337
69222afa
JM
338 if (videoComment.Account) {
339 const audience = getAudience(videoComment.Account.Actor, isPublic)
340 videoCommentObject = audiencify(videoCommentObject, audience)
d6e99e53 341
69222afa
JM
342 if (req.path.endsWith('/activity')) {
343 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
344 return activityPubResponse(activityPubContextify(data), res)
345 }
296c0905
C
346 }
347
4b8f09fa 348 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 349}
7006bc63 350
c48e82b5 351async function videoRedundancyController (req: express.Request, res: express.Response) {
dae86118 352 const videoRedundancy = res.locals.videoRedundancy
de94ac86
C
353
354 if (redirectIfNotOwned(videoRedundancy.url, res)) return
8d1fa36a 355
c48e82b5
C
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)
084a2cd0 363 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
c48e82b5
C
364 }
365
084a2cd0 366 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
c48e82b5
C
367}
368
418d092a 369async function videoPlaylistController (req: express.Request, res: express.Response) {
453e83ea 370 const playlist = res.locals.videoPlaylistFull
418d092a 371
de94ac86
C
372 if (redirectIfNotOwned(playlist.url, res)) return
373
df0b219d
C
374 // We need more attributes
375 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
376
377 const json = await playlist.toActivityPubObject(req.query.page, null)
418d092a
C
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
a1587156 384function videoPlaylistElementController (req: express.Request, res: express.Response) {
b5fecbf4 385 const videoPlaylistElement = res.locals.videoPlaylistElementAP
418d092a 386
de94ac86
C
387 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
388
418d092a
C
389 const json = videoPlaylistElement.toActivityPubObject()
390 return activityPubResponse(activityPubContextify(json), res)
391}
392
7006bc63
C
393// ---------------------------------------------------------------------------
394
453e83ea 395async function actorFollowing (req: express.Request, actor: MActorId) {
8fffe21a
C
396 const handler = (start: number, count: number) => {
397 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
398 }
7006bc63 399
6dd9de95 400 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63
C
401}
402
453e83ea 403async function actorFollowers (req: express.Request, actor: MActorId) {
8fffe21a 404 const handler = (start: number, count: number) => {
418d092a
C
405 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
406 }
407
6dd9de95 408 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418d092a
C
409}
410
7405b6ba 411async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
418d092a 412 const handler = (start: number, count: number) => {
7405b6ba 413 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
8fffe21a 414 }
7006bc63 415
6dd9de95 416 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63 417}
4b8f09fa 418
7eba5e1f 419function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
8fffe21a
C
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,
5c6d985f 424 data: result.rows.map(r => r.url)
8fffe21a
C
425 }
426 }
427 return activityPubCollectionPagination(url, handler, req.query.page)
4b8f09fa 428}
de94ac86
C
429
430function 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}