]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/client.ts
Stronger model typings
[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
C
216 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
217 const videoWithCaptions: MVideoAPWithoutCaption = 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
2186386c 311 const audience = getAudience(videoComment.Account.Actor, isPublic)
d6e99e53
C
312
313 const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
314
296c0905 315 if (req.path.endsWith('/activity')) {
c48e82b5 316 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
4b8f09fa 317 return activityPubResponse(activityPubContextify(data), res)
296c0905
C
318 }
319
4b8f09fa 320 return activityPubResponse(activityPubContextify(videoCommentObject), res)
da854ddd 321}
7006bc63 322
c48e82b5 323async function videoRedundancyController (req: express.Request, res: express.Response) {
dae86118 324 const videoRedundancy = res.locals.videoRedundancy
6dd9de95 325 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
8d1fa36a 326
c48e82b5
C
327 const serverActor = await getServerActor()
328
329 const audience = getAudience(serverActor)
330 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
331
332 if (req.path.endsWith('/activity')) {
333 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
334 return activityPubResponse(activityPubContextify(data), res)
335 }
336
337 return activityPubResponse(activityPubContextify(object), res)
338}
339
418d092a 340async function videoPlaylistController (req: express.Request, res: express.Response) {
453e83ea 341 const playlist = res.locals.videoPlaylistFull
418d092a 342
df0b219d
C
343 // We need more attributes
344 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
345
346 const json = await playlist.toActivityPubObject(req.query.page, null)
418d092a
C
347 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
348 const object = audiencify(json, audience)
349
350 return activityPubResponse(activityPubContextify(object), res)
351}
352
353async function videoPlaylistElementController (req: express.Request, res: express.Response) {
dae86118 354 const videoPlaylistElement = res.locals.videoPlaylistElement
418d092a
C
355
356 const json = videoPlaylistElement.toActivityPubObject()
357 return activityPubResponse(activityPubContextify(json), res)
358}
359
7006bc63
C
360// ---------------------------------------------------------------------------
361
453e83ea 362async function actorFollowing (req: express.Request, actor: MActorId) {
8fffe21a
C
363 const handler = (start: number, count: number) => {
364 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
365 }
7006bc63 366
6dd9de95 367 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63
C
368}
369
453e83ea 370async function actorFollowers (req: express.Request, actor: MActorId) {
8fffe21a 371 const handler = (start: number, count: number) => {
418d092a
C
372 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
373 }
374
6dd9de95 375 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
418d092a
C
376}
377
453e83ea 378async function actorPlaylists (req: express.Request, account: MAccountId) {
418d092a 379 const handler = (start: number, count: number) => {
0b16f5f2 380 return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
8fffe21a 381 }
7006bc63 382
6dd9de95 383 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
7006bc63 384}
4b8f09fa 385
453e83ea 386function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) {
8fffe21a
C
387 const handler = async (start: number, count: number) => {
388 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
389 return {
390 total: result.count,
5c6d985f 391 data: result.rows.map(r => r.url)
8fffe21a
C
392 }
393 }
394 return activityPubCollectionPagination(url, handler, req.query.page)
4b8f09fa 395}