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