]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/activitypub/client.ts
Regenerate miniature on live save
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / client.ts
... / ...
CommitLineData
1import * as cors from 'cors'
2import * as express from 'express'
3import { getRateUrl } from '@server/lib/activitypub/video-rates'
4import { getServerActor } from '@server/models/application/application'
5import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models'
6import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
7import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
8import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
9import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
10import { audiencify, getAudience } from '../../lib/activitypub/audience'
11import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
12import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
13import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
14import {
15 getVideoCommentsActivityPubUrl,
16 getVideoDislikesActivityPubUrl,
17 getVideoLikesActivityPubUrl,
18 getVideoSharesActivityPubUrl
19} from '../../lib/activitypub/url'
20import {
21 asyncMiddleware,
22 executeIfActivityPub,
23 localAccountValidator,
24 localVideoChannelValidator,
25 videosCustomGetValidator,
26 videosShareValidator
27} from '../../middlewares'
28import { cacheRoute } from '../../middlewares/cache'
29import { getAccountVideoRateValidatorFactory, videoCommentGetValidator } from '../../middlewares/validators'
30import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
31import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
32import { AccountModel } from '../../models/account/account'
33import { AccountVideoRateModel } from '../../models/account/account-video-rate'
34import { ActorFollowModel } from '../../models/activitypub/actor-follow'
35import { VideoModel } from '../../models/video/video'
36import { VideoCaptionModel } from '../../models/video/video-caption'
37import { VideoCommentModel } from '../../models/video/video-comment'
38import { VideoPlaylistModel } from '../../models/video/video-playlist'
39import { VideoShareModel } from '../../models/video/video-share'
40import { activityPubResponse } from './utils'
41
42const activityPubClientRouter = express.Router()
43activityPubClientRouter.use(cors())
44
45// Intercept ActivityPub client requests
46
47activityPubClientRouter.get('/accounts?/:name',
48 executeIfActivityPub,
49 asyncMiddleware(localAccountValidator),
50 accountController
51)
52activityPubClientRouter.get('/accounts?/:name/followers',
53 executeIfActivityPub,
54 asyncMiddleware(localAccountValidator),
55 asyncMiddleware(accountFollowersController)
56)
57activityPubClientRouter.get('/accounts?/:name/following',
58 executeIfActivityPub,
59 asyncMiddleware(localAccountValidator),
60 asyncMiddleware(accountFollowingController)
61)
62activityPubClientRouter.get('/accounts?/:name/playlists',
63 executeIfActivityPub,
64 asyncMiddleware(localAccountValidator),
65 asyncMiddleware(accountPlaylistsController)
66)
67activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
68 executeIfActivityPub,
69 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
70 getAccountVideoRateFactory('like')
71)
72activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
73 executeIfActivityPub,
74 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
75 getAccountVideoRateFactory('dislike')
76)
77
78activityPubClientRouter.get('/videos/watch/:id',
79 executeIfActivityPub,
80 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
81 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
82 asyncMiddleware(videoController)
83)
84activityPubClientRouter.get('/videos/watch/:id/activity',
85 executeIfActivityPub,
86 asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
87 asyncMiddleware(videoController)
88)
89activityPubClientRouter.get('/videos/watch/:id/announces',
90 executeIfActivityPub,
91 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
92 asyncMiddleware(videoAnnouncesController)
93)
94activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
95 executeIfActivityPub,
96 asyncMiddleware(videosShareValidator),
97 asyncMiddleware(videoAnnounceController)
98)
99activityPubClientRouter.get('/videos/watch/:id/likes',
100 executeIfActivityPub,
101 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
102 asyncMiddleware(videoLikesController)
103)
104activityPubClientRouter.get('/videos/watch/:id/dislikes',
105 executeIfActivityPub,
106 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
107 asyncMiddleware(videoDislikesController)
108)
109activityPubClientRouter.get('/videos/watch/:id/comments',
110 executeIfActivityPub,
111 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
112 asyncMiddleware(videoCommentsController)
113)
114activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
115 executeIfActivityPub,
116 asyncMiddleware(videoCommentGetValidator),
117 asyncMiddleware(videoCommentController)
118)
119activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
120 executeIfActivityPub,
121 asyncMiddleware(videoCommentGetValidator),
122 asyncMiddleware(videoCommentController)
123)
124
125activityPubClientRouter.get('/video-channels/:name',
126 executeIfActivityPub,
127 asyncMiddleware(localVideoChannelValidator),
128 videoChannelController
129)
130activityPubClientRouter.get('/video-channels/:name/followers',
131 executeIfActivityPub,
132 asyncMiddleware(localVideoChannelValidator),
133 asyncMiddleware(videoChannelFollowersController)
134)
135activityPubClientRouter.get('/video-channels/:name/following',
136 executeIfActivityPub,
137 asyncMiddleware(localVideoChannelValidator),
138 asyncMiddleware(videoChannelFollowingController)
139)
140activityPubClientRouter.get('/video-channels/:name/playlists',
141 executeIfActivityPub,
142 asyncMiddleware(localVideoChannelValidator),
143 asyncMiddleware(videoChannelPlaylistsController)
144)
145
146activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
147 executeIfActivityPub,
148 asyncMiddleware(videoFileRedundancyGetValidator),
149 asyncMiddleware(videoRedundancyController)
150)
151activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
152 executeIfActivityPub,
153 asyncMiddleware(videoPlaylistRedundancyGetValidator),
154 asyncMiddleware(videoRedundancyController)
155)
156
157activityPubClientRouter.get('/video-playlists/:playlistId',
158 executeIfActivityPub,
159 asyncMiddleware(videoPlaylistsGetValidator('all')),
160 asyncMiddleware(videoPlaylistController)
161)
162activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
163 executeIfActivityPub,
164 asyncMiddleware(videoPlaylistElementAPGetValidator),
165 videoPlaylistElementController
166)
167
168// ---------------------------------------------------------------------------
169
170export {
171 activityPubClientRouter
172}
173
174// ---------------------------------------------------------------------------
175
176function accountController (req: express.Request, res: express.Response) {
177 const account = res.locals.account
178
179 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
180}
181
182async function accountFollowersController (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184 const activityPubResult = await actorFollowers(req, account.Actor)
185
186 return activityPubResponse(activityPubContextify(activityPubResult), res)
187}
188
189async function accountFollowingController (req: express.Request, res: express.Response) {
190 const account = res.locals.account
191 const activityPubResult = await actorFollowing(req, account.Actor)
192
193 return activityPubResponse(activityPubContextify(activityPubResult), res)
194}
195
196async function accountPlaylistsController (req: express.Request, res: express.Response) {
197 const account = res.locals.account
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 })
206
207 return activityPubResponse(activityPubContextify(activityPubResult), res)
208}
209
210function getAccountVideoRateFactory (rateType: VideoRateType) {
211 return (req: express.Request, res: express.Response) => {
212 const accountVideoRate = res.locals.accountVideoRate
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)
218 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
219
220 return activityPubResponse(activityPubContextify(APObject), res)
221 }
222}
223
224async function videoController (req: express.Request, res: express.Response) {
225 // We need more attributes
226 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(res.locals.onlyVideoWithRights.id)
227
228 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
229
230 // We need captions to render AP object
231 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
232 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
233
234 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
235 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
236
237 if (req.path.endsWith('/activity')) {
238 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
239 return activityPubResponse(activityPubContextify(data), res)
240 }
241
242 return activityPubResponse(activityPubContextify(videoObject), res)
243}
244
245async function videoAnnounceController (req: express.Request, res: express.Response) {
246 const share = res.locals.videoShare
247
248 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
249
250 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
251
252 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
253}
254
255async function videoAnnouncesController (req: express.Request, res: express.Response) {
256 const video = res.locals.onlyImmutableVideo
257
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)
266
267 return activityPubResponse(activityPubContextify(json), res)
268}
269
270async function videoLikesController (req: express.Request, res: express.Response) {
271 const video = res.locals.onlyImmutableVideo
272 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
273
274 return activityPubResponse(activityPubContextify(json), res)
275}
276
277async function videoDislikesController (req: express.Request, res: express.Response) {
278 const video = res.locals.onlyImmutableVideo
279 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
280
281 return activityPubResponse(activityPubContextify(json), res)
282}
283
284async function videoCommentsController (req: express.Request, res: express.Response) {
285 const video = res.locals.onlyImmutableVideo
286
287 const handler = async (start: number, count: number) => {
288 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
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)
295
296 return activityPubResponse(activityPubContextify(json), res)
297}
298
299function videoChannelController (req: express.Request, res: express.Response) {
300 const videoChannel = res.locals.videoChannel
301
302 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
303}
304
305async function videoChannelFollowersController (req: express.Request, res: express.Response) {
306 const videoChannel = res.locals.videoChannel
307 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
308
309 return activityPubResponse(activityPubContextify(activityPubResult), res)
310}
311
312async function videoChannelFollowingController (req: express.Request, res: express.Response) {
313 const videoChannel = res.locals.videoChannel
314 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
315
316 return activityPubResponse(activityPubContextify(activityPubResult), res)
317}
318
319async function videoCommentController (req: express.Request, res: express.Response) {
320 const videoComment = res.locals.videoCommentFull
321
322 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
323
324 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
325 const isPublic = true // Comments are always public
326 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
327
328 if (videoComment.Account) {
329 const audience = getAudience(videoComment.Account.Actor, isPublic)
330 videoCommentObject = audiencify(videoCommentObject, audience)
331
332 if (req.path.endsWith('/activity')) {
333 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
334 return activityPubResponse(activityPubContextify(data), res)
335 }
336 }
337
338 return activityPubResponse(activityPubContextify(videoCommentObject), res)
339}
340
341async function videoRedundancyController (req: express.Request, res: express.Response) {
342 const videoRedundancy = res.locals.videoRedundancy
343 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
344
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)
352 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
353 }
354
355 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
356}
357
358async function videoPlaylistController (req: express.Request, res: express.Response) {
359 const playlist = res.locals.videoPlaylistFull
360
361 // We need more attributes
362 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
363
364 const json = await playlist.toActivityPubObject(req.query.page, null)
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
371function videoPlaylistElementController (req: express.Request, res: express.Response) {
372 const videoPlaylistElement = res.locals.videoPlaylistElementAP
373
374 const json = videoPlaylistElement.toActivityPubObject()
375 return activityPubResponse(activityPubContextify(json), res)
376}
377
378// ---------------------------------------------------------------------------
379
380async function actorFollowing (req: express.Request, actor: MActorId) {
381 const handler = (start: number, count: number) => {
382 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
383 }
384
385 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
386}
387
388async function actorFollowers (req: express.Request, actor: MActorId) {
389 const handler = (start: number, count: number) => {
390 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
391 }
392
393 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
394}
395
396async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
397 const handler = (start: number, count: number) => {
398 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
399 }
400
401 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
402}
403
404function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
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,
409 data: result.rows.map(r => r.url)
410 }
411 }
412 return activityPubCollectionPagination(url, handler, req.query.page)
413}