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