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