]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Merge branch 'master' into develop
[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'
2c8776fc 38import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/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)
20494f12 140
c48e82b5 141activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
e65c0c5b
C
142 executeIfActivityPub,
143 asyncMiddleware(videoFileRedundancyGetValidator),
144 asyncMiddleware(videoRedundancyController)
09209296 145)
9c6ca37f 146activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
e65c0c5b
C
147 executeIfActivityPub,
148 asyncMiddleware(videoPlaylistRedundancyGetValidator),
149 asyncMiddleware(videoRedundancyController)
c48e82b5
C
150)
151
418d092a 152activityPubClientRouter.get('/video-playlists/:playlistId',
e65c0c5b 153 executeIfActivityPub,
453e83ea 154 asyncMiddleware(videoPlaylistsGetValidator('all')),
e65c0c5b 155 asyncMiddleware(videoPlaylistController)
418d092a
C
156)
157activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
e65c0c5b
C
158 executeIfActivityPub,
159 asyncMiddleware(videoPlaylistElementAPGetValidator),
a1587156 160 videoPlaylistElementController
418d092a
C
161)
162
e4f97bab
C
163// ---------------------------------------------------------------------------
164
165export {
166 activityPubClientRouter
167}
168
169// ---------------------------------------------------------------------------
170
418d092a 171function accountController (req: express.Request, res: express.Response) {
dae86118 172 const account = res.locals.account
e4f97bab 173
4b8f09fa 174 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
e4f97bab
C
175}
176
418d092a 177async function accountFollowersController (req: express.Request, res: express.Response) {
dae86118 178 const account = res.locals.account
7006bc63 179 const activityPubResult = await actorFollowers(req, account.Actor)
e4f97bab 180
4b8f09fa 181 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab
C
182}
183
418d092a 184async function accountFollowingController (req: express.Request, res: express.Response) {
dae86118 185 const account = res.locals.account
7006bc63 186 const activityPubResult = await actorFollowing(req, account.Actor)
e4f97bab 187
4b8f09fa 188 return activityPubResponse(activityPubContextify(activityPubResult), res)
e4f97bab 189}
20494f12 190
418d092a 191async function accountPlaylistsController (req: express.Request, res: express.Response) {
dae86118 192 const account = res.locals.account
418d092a
C
193 const activityPubResult = await actorPlaylists(req, account)
194
195 return activityPubResponse(activityPubContextify(activityPubResult), res)
196}
197
75ba887d 198function getAccountVideoRateFactory (rateType: VideoRateType) {
5c6d985f 199 return (req: express.Request, res: express.Response) => {
dae86118 200 const accountVideoRate = res.locals.accountVideoRate
5c6d985f
C
201
202 const byActor = accountVideoRate.Account.Actor
203 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
204 const APObject = rateType === 'like'
205 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
1e7eb25f 206 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
5c6d985f
C
207
208 return activityPubResponse(activityPubContextify(APObject), res)
209 }
210}
211
1a8dd4da 212async function videoController (req: express.Request, res: express.Response) {
09209296 213 // We need more attributes
453e83ea 214 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
20494f12 215
6dd9de95 216 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
8d1fa36a 217
40e87e9e 218 // We need captions to render AP object
453e83ea 219 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
b5fecbf4 220 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
40e87e9e 221
453e83ea
C
222 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
223 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
2ccaeeb3 224
296c0905 225 if (req.path.endsWith('/activity')) {
453e83ea 226 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
4b8f09fa 227 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
228 }
229
4b8f09fa 230 return activityPubResponse(activityPubContextify(videoObject), res)
20494f12
C
231}
232
1a8dd4da 233async function videoAnnounceController (req: express.Request, res: express.Response) {
dae86118 234 const share = res.locals.videoShare
8d1fa36a 235
6dd9de95 236 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
8d1fa36a 237
453e83ea 238 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
4e50b6a1 239
598edb8a 240 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
4e50b6a1
C
241}
242
1a8dd4da 243async function videoAnnouncesController (req: express.Request, res: express.Response) {
7eba5e1f 244 const video = res.locals.onlyImmutableVideo
46531a0a 245
8fffe21a
C
246 const handler = async (start: number, count: number) => {
247 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
248 return {
249 total: result.count,
250 data: result.rows.map(r => r.url)
251 }
252 }
253 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
46531a0a 254
8fffe21a 255 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
256}
257
1a8dd4da 258async function videoLikesController (req: express.Request, res: express.Response) {
7eba5e1f 259 const video = res.locals.onlyImmutableVideo
8fffe21a 260 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
46531a0a 261
8fffe21a 262 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
263}
264
1a8dd4da 265async function videoDislikesController (req: express.Request, res: express.Response) {
7eba5e1f 266 const video = res.locals.onlyImmutableVideo
8fffe21a 267 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
46531a0a 268
8fffe21a 269 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
270}
271
1a8dd4da 272async function videoCommentsController (req: express.Request, res: express.Response) {
7eba5e1f 273 const video = res.locals.onlyImmutableVideo
46531a0a 274
8fffe21a
C
275 const handler = async (start: number, count: number) => {
276 const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
277 return {
278 total: result.count,
279 data: result.rows.map(r => r.url)
280 }
281 }
282 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
46531a0a 283
8fffe21a 284 return activityPubResponse(activityPubContextify(json), res)
46531a0a
C
285}
286
a1587156 287function videoChannelController (req: express.Request, res: express.Response) {
dae86118 288 const videoChannel = res.locals.videoChannel
20494f12 289
4b8f09fa 290 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
20494f12 291}
da854ddd 292
1a8dd4da 293async function videoChannelFollowersController (req: express.Request, res: express.Response) {
dae86118 294 const videoChannel = res.locals.videoChannel
7006bc63
C
295 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
296
4b8f09fa 297 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
298}
299
1a8dd4da 300async function videoChannelFollowingController (req: express.Request, res: express.Response) {
dae86118 301 const videoChannel = res.locals.videoChannel
7006bc63
C
302 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
303
4b8f09fa 304 return activityPubResponse(activityPubContextify(activityPubResult), res)
7006bc63
C
305}
306
1a8dd4da 307async function videoCommentController (req: express.Request, res: express.Response) {
453e83ea 308 const videoComment = res.locals.videoCommentFull
da854ddd 309
6dd9de95 310 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
8d1fa36a 311
d7e70384 312 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
d6e99e53 313 const isPublic = true // Comments are always public
69222afa 314 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
d6e99e53 315
69222afa
JM
316 if (videoComment.Account) {
317 const audience = getAudience(videoComment.Account.Actor, isPublic)
318 videoCommentObject = audiencify(videoCommentObject, audience)
d6e99e53 319
69222afa
JM
320 if (req.path.endsWith('/activity')) {
321 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
322 return activityPubResponse(activityPubContextify(data), res)
323 }
296c0905
C
324 }
325
4b8f09fa 326 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 327}
7006bc63 328
c48e82b5 329async function videoRedundancyController (req: express.Request, res: express.Response) {
dae86118 330 const videoRedundancy = res.locals.videoRedundancy
6dd9de95 331 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
8d1fa36a 332
c48e82b5
C
333 const serverActor = await getServerActor()
334
335 const audience = getAudience(serverActor)
336 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
337
338 if (req.path.endsWith('/activity')) {
339 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
084a2cd0 340 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
c48e82b5
C
341 }
342
084a2cd0 343 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
c48e82b5
C
344}
345
418d092a 346async function videoPlaylistController (req: express.Request, res: express.Response) {
453e83ea 347 const playlist = res.locals.videoPlaylistFull
418d092a 348
df0b219d
C
349 // We need more attributes
350 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
351
352 const json = await playlist.toActivityPubObject(req.query.page, null)
418d092a
C
353 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
354 const object = audiencify(json, audience)
355
356 return activityPubResponse(activityPubContextify(object), res)
357}
358
a1587156 359function videoPlaylistElementController (req: express.Request, res: express.Response) {
b5fecbf4 360 const videoPlaylistElement = res.locals.videoPlaylistElementAP
418d092a
C
361
362 const json = videoPlaylistElement.toActivityPubObject()
363 return activityPubResponse(activityPubContextify(json), res)
364}
365
7006bc63
C
366// ---------------------------------------------------------------------------
367
453e83ea 368async function actorFollowing (req: express.Request, actor: MActorId) {
8fffe21a
C
369 const handler = (start: number, count: number) => {
370 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
371 }
7006bc63 372
6dd9de95 373 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63
C
374}
375
453e83ea 376async function actorFollowers (req: express.Request, actor: MActorId) {
8fffe21a 377 const handler = (start: number, count: number) => {
418d092a
C
378 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
379 }
380
6dd9de95 381 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418d092a
C
382}
383
453e83ea 384async function actorPlaylists (req: express.Request, account: MAccountId) {
418d092a 385 const handler = (start: number, count: number) => {
0b16f5f2 386 return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
8fffe21a 387 }
7006bc63 388
6dd9de95 389 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63 390}
4b8f09fa 391
7eba5e1f 392function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
8fffe21a
C
393 const handler = async (start: number, count: number) => {
394 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
395 return {
396 total: result.count,
5c6d985f 397 data: result.rows.map(r => r.url)
8fffe21a
C
398 }
399 }
400 return activityPubCollectionPagination(url, handler, req.query.page)
4b8f09fa 401}