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