]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Increase fetcher jobs concurrency
[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
418d092a 158activityPubClientRouter.get('/video-playlists/:playlistId',
e65c0c5b 159 executeIfActivityPub,
453e83ea 160 asyncMiddleware(videoPlaylistsGetValidator('all')),
e65c0c5b 161 asyncMiddleware(videoPlaylistController)
418d092a 162)
37190663 163activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
e65c0c5b
C
164 executeIfActivityPub,
165 asyncMiddleware(videoPlaylistElementAPGetValidator),
a1587156 166 videoPlaylistElementController
418d092a
C
167)
168
e4f97bab
C
169// ---------------------------------------------------------------------------
170
171export {
172 activityPubClientRouter
173}
174
175// ---------------------------------------------------------------------------
176
418d092a 177function accountController (req: express.Request, res: express.Response) {
dae86118 178 const account = res.locals.account
e4f97bab 179
4b8f09fa 180 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
e4f97bab
C
181}
182
418d092a 183async function accountFollowersController (req: express.Request, res: express.Response) {
dae86118 184 const account = res.locals.account
7006bc63 185 const activityPubResult = await actorFollowers(req, account.Actor)
e4f97bab 186
4b8f09fa 187 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab
C
188}
189
418d092a 190async function accountFollowingController (req: express.Request, res: express.Response) {
dae86118 191 const account = res.locals.account
7006bc63 192 const activityPubResult = await actorFollowing(req, account.Actor)
e4f97bab 193
4b8f09fa 194 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab 195}
20494f12 196
418d092a 197async function accountPlaylistsController (req: express.Request, res: express.Response) {
dae86118 198 const account = res.locals.account
7405b6ba
C
199 const activityPubResult = await actorPlaylists(req, { account })
200
201 return activityPubResponse(activityPubContextify(activityPubResult), res)
202}
203
204async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
205 const channel = res.locals.videoChannel
206 const activityPubResult = await actorPlaylists(req, { channel })
418d092a
C
207
208 return activityPubResponse(activityPubContextify(activityPubResult), res)
209}
210
75ba887d 211function getAccountVideoRateFactory (rateType: VideoRateType) {
5c6d985f 212 return (req: express.Request, res: express.Response) => {
dae86118 213 const accountVideoRate = res.locals.accountVideoRate
5c6d985f
C
214
215 const byActor = accountVideoRate.Account.Actor
5c6d985f 216 const APObject = rateType === 'like'
de94ac86
C
217 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
218 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
5c6d985f
C
219
220 return activityPubResponse(activityPubContextify(APObject), res)
221 }
222}
223
1a8dd4da 224async function videoController (req: express.Request, res: express.Response) {
71d4af1e 225 const video = res.locals.videoAll
20494f12 226
de94ac86 227 if (redirectIfNotOwned(video.url, res)) return
8d1fa36a 228
40e87e9e 229 // We need captions to render AP object
453e83ea 230 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
b5fecbf4 231 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
40e87e9e 232
453e83ea
C
233 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
234 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
2ccaeeb3 235
296c0905 236 if (req.path.endsWith('/activity')) {
453e83ea 237 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
4b8f09fa 238 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
239 }
240
4b8f09fa 241 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
242}
243
1a8dd4da 244async function videoAnnounceController (req: express.Request, res: express.Response) {
dae86118 245 const share = res.locals.videoShare
8d1fa36a 246
de94ac86 247 if (redirectIfNotOwned(share.url, res)) return
8d1fa36a 248
453e83ea 249 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
4e50b6a1 250
598edb8a 251 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
4e50b6a1
C
252}
253
1a8dd4da 254async function videoAnnouncesController (req: express.Request, res: express.Response) {
7eba5e1f 255 const video = res.locals.onlyImmutableVideo
46531a0a 256
de94ac86
C
257 if (redirectIfNotOwned(video.url, res)) return
258
8fffe21a
C
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 }
de94ac86 266 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
46531a0a 267
8fffe21a 268 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
269}
270
1a8dd4da 271async function videoLikesController (req: express.Request, res: express.Response) {
7eba5e1f 272 const video = res.locals.onlyImmutableVideo
de94ac86
C
273
274 if (redirectIfNotOwned(video.url, res)) return
275
276 const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
46531a0a 277
8fffe21a 278 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
279}
280
1a8dd4da 281async function videoDislikesController (req: express.Request, res: express.Response) {
7eba5e1f 282 const video = res.locals.onlyImmutableVideo
de94ac86
C
283
284 if (redirectIfNotOwned(video.url, res)) return
285
286 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
46531a0a 287
8fffe21a 288 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
289}
290
1a8dd4da 291async function videoCommentsController (req: express.Request, res: express.Response) {
7eba5e1f 292 const video = res.locals.onlyImmutableVideo
46531a0a 293
de94ac86
C
294 if (redirectIfNotOwned(video.url, res)) return
295
8fffe21a 296 const handler = async (start: number, count: number) => {
696d83fd 297 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
8fffe21a
C
298 return {
299 total: result.count,
300 data: result.rows.map(r => r.url)
301 }
302 }
de94ac86 303 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
46531a0a 304
8fffe21a 305 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
306}
307
a1587156 308function videoChannelController (req: express.Request, res: express.Response) {
dae86118 309 const videoChannel = res.locals.videoChannel
20494f12 310
4b8f09fa 311 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
20494f12 312}
da854ddd 313
1a8dd4da 314async function videoChannelFollowersController (req: express.Request, res: express.Response) {
dae86118 315 const videoChannel = res.locals.videoChannel
7006bc63
C
316 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
317
4b8f09fa 318 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
319}
320
1a8dd4da 321async function videoChannelFollowingController (req: express.Request, res: express.Response) {
dae86118 322 const videoChannel = res.locals.videoChannel
7006bc63
C
323 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
324
4b8f09fa 325 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
326}
327
1a8dd4da 328async function videoCommentController (req: express.Request, res: express.Response) {
453e83ea 329 const videoComment = res.locals.videoCommentFull
da854ddd 330
de94ac86 331 if (redirectIfNotOwned(videoComment.url, res)) return
8d1fa36a 332
d7e70384 333 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53 334 const isPublic = true // Comments are always public
69222afa 335 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
d6e99e53 336
69222afa
JM
337 if (videoComment.Account) {
338 const audience = getAudience(videoComment.Account.Actor, isPublic)
339 videoCommentObject = audiencify(videoCommentObject, audience)
d6e99e53 340
69222afa
JM
341 if (req.path.endsWith('/activity')) {
342 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
343 return activityPubResponse(activityPubContextify(data), res)
344 }
296c0905
C
345 }
346
4b8f09fa 347 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 348}
7006bc63 349
c48e82b5 350async function videoRedundancyController (req: express.Request, res: express.Response) {
dae86118 351 const videoRedundancy = res.locals.videoRedundancy
de94ac86
C
352
353 if (redirectIfNotOwned(videoRedundancy.url, res)) return
8d1fa36a 354
c48e82b5
C
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)
084a2cd0 362 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
c48e82b5
C
363 }
364
084a2cd0 365 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
c48e82b5
C
366}
367
418d092a 368async function videoPlaylistController (req: express.Request, res: express.Response) {
453e83ea 369 const playlist = res.locals.videoPlaylistFull
418d092a 370
de94ac86
C
371 if (redirectIfNotOwned(playlist.url, res)) return
372
df0b219d
C
373 // We need more attributes
374 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
375
376 const json = await playlist.toActivityPubObject(req.query.page, null)
418d092a
C
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
a1587156 383function videoPlaylistElementController (req: express.Request, res: express.Response) {
b5fecbf4 384 const videoPlaylistElement = res.locals.videoPlaylistElementAP
418d092a 385
de94ac86
C
386 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
387
418d092a
C
388 const json = videoPlaylistElement.toActivityPubObject()
389 return activityPubResponse(activityPubContextify(json), res)
390}
391
7006bc63
C
392// ---------------------------------------------------------------------------
393
453e83ea 394async function actorFollowing (req: express.Request, actor: MActorId) {
8fffe21a
C
395 const handler = (start: number, count: number) => {
396 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
397 }
7006bc63 398
6dd9de95 399 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63
C
400}
401
453e83ea 402async function actorFollowers (req: express.Request, actor: MActorId) {
8fffe21a 403 const handler = (start: number, count: number) => {
418d092a
C
404 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
405 }
406
6dd9de95 407 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418d092a
C
408}
409
7405b6ba 410async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
418d092a 411 const handler = (start: number, count: number) => {
7405b6ba 412 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
8fffe21a 413 }
7006bc63 414
6dd9de95 415 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63 416}
4b8f09fa 417
7eba5e1f 418function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
8fffe21a
C
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,
5c6d985f 423 data: result.rows.map(r => r.url)
8fffe21a
C
424 }
425 }
426 return activityPubCollectionPagination(url, handler, req.query.page)
4b8f09fa 427}
de94ac86
C
428
429function 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}