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