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