]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Translated using Weblate (Gaelic)
[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'
50d6de9c 19import { ActorFollowModel } from '../../models/activitypub/actor-follow'
3fd3ab2d 20import { VideoModel } from '../../models/video/video'
da854ddd 21import { VideoCommentModel } from '../../models/video/video-comment'
3fd3ab2d 22import { VideoShareModel } from '../../models/video/video-share'
98d3324d 23import { cacheRoute } from '../../middlewares/cache'
8fffe21a
C
24import { activityPubResponse } from './utils'
25import { AccountVideoRateModel } from '../../models/account/account-video-rate'
26import {
5c6d985f 27 getRateUrl,
8fffe21a
C
28 getVideoCommentsActivityPubUrl,
29 getVideoDislikesActivityPubUrl,
30 getVideoLikesActivityPubUrl,
31 getVideoSharesActivityPubUrl
32} from '../../lib/activitypub'
40e87e9e 33import { VideoCaptionModel } from '../../models/video/video-caption'
09209296 34import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
c48e82b5 35import { getServerActor } from '../../helpers/utils'
1e7eb25f 36import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
418d092a
C
37import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
38import { VideoPlaylistModel } from '../../models/video/video-playlist'
418d092a 39import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
453e83ea 40import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption } from '@server/typings/models'
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 150 executeIfActivityPub,
453e83ea 151 asyncMiddleware(videoPlaylistsGetValidator('all')),
e65c0c5b 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
453e83ea 211 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
20494f12 212
6dd9de95 213 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
8d1fa36a 214
40e87e9e 215 // We need captions to render AP object
453e83ea 216 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
b5fecbf4 217 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
40e87e9e 218
453e83ea
C
219 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
220 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
2ccaeeb3 221
296c0905 222 if (req.path.endsWith('/activity')) {
453e83ea 223 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
4b8f09fa 224 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
225 }
226
4b8f09fa 227 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
228}
229
1a8dd4da 230async function videoAnnounceController (req: express.Request, res: express.Response) {
dae86118 231 const share = res.locals.videoShare
8d1fa36a 232
6dd9de95 233 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
8d1fa36a 234
453e83ea 235 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
4e50b6a1 236
c48e82b5 237 return activityPubResponse(activityPubContextify(activity), res)
4e50b6a1
C
238}
239
1a8dd4da 240async function videoAnnouncesController (req: express.Request, res: express.Response) {
453e83ea 241 const video = res.locals.onlyVideo
46531a0a 242
8fffe21a
C
243 const handler = async (start: number, count: number) => {
244 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
245 return {
246 total: result.count,
247 data: result.rows.map(r => r.url)
248 }
249 }
250 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
46531a0a 251
8fffe21a 252 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
253}
254
1a8dd4da 255async function videoLikesController (req: express.Request, res: express.Response) {
453e83ea 256 const video = res.locals.onlyVideo
8fffe21a 257 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
46531a0a 258
8fffe21a 259 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
260}
261
1a8dd4da 262async function videoDislikesController (req: express.Request, res: express.Response) {
453e83ea 263 const video = res.locals.onlyVideo
8fffe21a 264 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
46531a0a 265
8fffe21a 266 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
267}
268
1a8dd4da 269async function videoCommentsController (req: express.Request, res: express.Response) {
453e83ea 270 const video = res.locals.onlyVideo
46531a0a 271
8fffe21a
C
272 const handler = async (start: number, count: number) => {
273 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
274 return {
275 total: result.count,
276 data: result.rows.map(r => r.url)
277 }
278 }
279 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
46531a0a 280
8fffe21a 281 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
282}
283
1a8dd4da 284async function videoChannelController (req: express.Request, res: express.Response) {
dae86118 285 const videoChannel = res.locals.videoChannel
20494f12 286
4b8f09fa 287 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
20494f12 288}
da854ddd 289
1a8dd4da 290async function videoChannelFollowersController (req: express.Request, res: express.Response) {
dae86118 291 const videoChannel = res.locals.videoChannel
7006bc63
C
292 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
293
4b8f09fa 294 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
295}
296
1a8dd4da 297async function videoChannelFollowingController (req: express.Request, res: express.Response) {
dae86118 298 const videoChannel = res.locals.videoChannel
7006bc63
C
299 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
300
4b8f09fa 301 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
302}
303
1a8dd4da 304async function videoCommentController (req: express.Request, res: express.Response) {
453e83ea 305 const videoComment = res.locals.videoCommentFull
da854ddd 306
6dd9de95 307 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
8d1fa36a 308
d7e70384 309 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53 310 const isPublic = true // Comments are always public
69222afa 311 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
d6e99e53 312
69222afa
JM
313 if (videoComment.Account) {
314 const audience = getAudience(videoComment.Account.Actor, isPublic)
315 videoCommentObject = audiencify(videoCommentObject, audience)
d6e99e53 316
69222afa
JM
317 if (req.path.endsWith('/activity')) {
318 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
319 return activityPubResponse(activityPubContextify(data), res)
320 }
296c0905
C
321 }
322
4b8f09fa 323 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 324}
7006bc63 325
c48e82b5 326async function videoRedundancyController (req: express.Request, res: express.Response) {
dae86118 327 const videoRedundancy = res.locals.videoRedundancy
6dd9de95 328 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
8d1fa36a 329
c48e82b5
C
330 const serverActor = await getServerActor()
331
332 const audience = getAudience(serverActor)
333 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
334
335 if (req.path.endsWith('/activity')) {
336 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
337 return activityPubResponse(activityPubContextify(data), res)
338 }
339
340 return activityPubResponse(activityPubContextify(object), res)
341}
342
418d092a 343async function videoPlaylistController (req: express.Request, res: express.Response) {
453e83ea 344 const playlist = res.locals.videoPlaylistFull
418d092a 345
df0b219d
C
346 // We need more attributes
347 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
348
349 const json = await playlist.toActivityPubObject(req.query.page, null)
418d092a
C
350 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
351 const object = audiencify(json, audience)
352
353 return activityPubResponse(activityPubContextify(object), res)
354}
355
356async function videoPlaylistElementController (req: express.Request, res: express.Response) {
b5fecbf4 357 const videoPlaylistElement = res.locals.videoPlaylistElementAP
418d092a
C
358
359 const json = videoPlaylistElement.toActivityPubObject()
360 return activityPubResponse(activityPubContextify(json), res)
361}
362
7006bc63
C
363// ---------------------------------------------------------------------------
364
453e83ea 365async function actorFollowing (req: express.Request, actor: MActorId) {
8fffe21a
C
366 const handler = (start: number, count: number) => {
367 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
368 }
7006bc63 369
6dd9de95 370 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63
C
371}
372
453e83ea 373async function actorFollowers (req: express.Request, actor: MActorId) {
8fffe21a 374 const handler = (start: number, count: number) => {
418d092a
C
375 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
376 }
377
6dd9de95 378 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418d092a
C
379}
380
453e83ea 381async function actorPlaylists (req: express.Request, account: MAccountId) {
418d092a 382 const handler = (start: number, count: number) => {
0b16f5f2 383 return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
8fffe21a 384 }
7006bc63 385
6dd9de95 386 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63 387}
4b8f09fa 388
453e83ea 389function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) {
8fffe21a
C
390 const handler = async (start: number, count: number) => {
391 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
392 return {
393 total: result.count,
5c6d985f 394 data: result.rows.map(r => r.url)
8fffe21a
C
395 }
396 }
397 return activityPubCollectionPagination(url, handler, req.query.page)
4b8f09fa 398}