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