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