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