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