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