e44f1c6ab8440ac742c4bb768bcc8be4a2bd1660
[oweals/peertube.git] / server / controllers / activitypub / client.ts
1 import * as express from 'express'
2 import * as cors from 'cors'
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 { getAccountVideoRateValidatorFactory, 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   getVideoCommentsActivityPubUrl,
28   getVideoDislikesActivityPubUrl,
29   getVideoLikesActivityPubUrl,
30   getVideoSharesActivityPubUrl
31 } from '../../lib/activitypub/url'
32 import { VideoCaptionModel } from '../../models/video/video-caption'
33 import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
34 import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
35 import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
36 import { VideoPlaylistModel } from '../../models/video/video-playlist'
37 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
38 import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models'
39 import { getServerActor } from '@server/models/application/application'
40 import { getRateUrl } from '@server/lib/activitypub/video-rates'
41
42 const activityPubClientRouter = express.Router()
43 activityPubClientRouter.use(cors())
44
45 // Intercept ActivityPub client requests
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(getAccountVideoRateValidatorFactory('like')),
70   getAccountVideoRateFactory('like')
71 )
72 activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
73   executeIfActivityPub,
74   asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
75   getAccountVideoRateFactory('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-immutable-attributes')),
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-immutable-attributes')),
102   asyncMiddleware(videoLikesController)
103 )
104 activityPubClientRouter.get('/videos/watch/:id/dislikes',
105   executeIfActivityPub,
106   asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
107   asyncMiddleware(videoDislikesController)
108 )
109 activityPubClientRouter.get('/videos/watch/:id/comments',
110   executeIfActivityPub,
111   asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
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   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('all')),
155   asyncMiddleware(videoPlaylistController)
156 )
157 activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
158   executeIfActivityPub,
159   asyncMiddleware(videoPlaylistElementAPGetValidator),
160   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 getAccountVideoRateFactory (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({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
215
216   if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
217
218   // We need captions to render AP object
219   const captions = await VideoCaptionModel.listVideoCaptions(video.id)
220   const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
221
222   const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
223   const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
224
225   if (req.path.endsWith('/activity')) {
226     const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
227     return activityPubResponse(activityPubContextify(data), res)
228   }
229
230   return activityPubResponse(activityPubContextify(videoObject), res)
231 }
232
233 async function videoAnnounceController (req: express.Request, res: express.Response) {
234   const share = res.locals.videoShare
235
236   if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
237
238   const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
239
240   return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
241 }
242
243 async function videoAnnouncesController (req: express.Request, res: express.Response) {
244   const video = res.locals.onlyImmutableVideo
245
246   const handler = async (start: number, count: number) => {
247     const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
248     return {
249       total: result.count,
250       data: result.rows.map(r => r.url)
251     }
252   }
253   const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
254
255   return activityPubResponse(activityPubContextify(json), res)
256 }
257
258 async function videoLikesController (req: express.Request, res: express.Response) {
259   const video = res.locals.onlyImmutableVideo
260   const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
261
262   return activityPubResponse(activityPubContextify(json), res)
263 }
264
265 async function videoDislikesController (req: express.Request, res: express.Response) {
266   const video = res.locals.onlyImmutableVideo
267   const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
268
269   return activityPubResponse(activityPubContextify(json), res)
270 }
271
272 async function videoCommentsController (req: express.Request, res: express.Response) {
273   const video = res.locals.onlyImmutableVideo
274
275   const handler = async (start: number, count: number) => {
276     const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
277     return {
278       total: result.count,
279       data: result.rows.map(r => r.url)
280     }
281   }
282   const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
283
284   return activityPubResponse(activityPubContextify(json), res)
285 }
286
287 function videoChannelController (req: express.Request, res: express.Response) {
288   const videoChannel = res.locals.videoChannel
289
290   return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
291 }
292
293 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
294   const videoChannel = res.locals.videoChannel
295   const activityPubResult = await actorFollowers(req, videoChannel.Actor)
296
297   return activityPubResponse(activityPubContextify(activityPubResult), res)
298 }
299
300 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
301   const videoChannel = res.locals.videoChannel
302   const activityPubResult = await actorFollowing(req, videoChannel.Actor)
303
304   return activityPubResponse(activityPubContextify(activityPubResult), res)
305 }
306
307 async function videoCommentController (req: express.Request, res: express.Response) {
308   const videoComment = res.locals.videoCommentFull
309
310   if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
311
312   const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
313   const isPublic = true // Comments are always public
314   let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
315
316   if (videoComment.Account) {
317     const audience = getAudience(videoComment.Account.Actor, isPublic)
318     videoCommentObject = audiencify(videoCommentObject, audience)
319
320     if (req.path.endsWith('/activity')) {
321       const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
322       return activityPubResponse(activityPubContextify(data), res)
323     }
324   }
325
326   return activityPubResponse(activityPubContextify(videoCommentObject), res)
327 }
328
329 async function videoRedundancyController (req: express.Request, res: express.Response) {
330   const videoRedundancy = res.locals.videoRedundancy
331   if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
332
333   const serverActor = await getServerActor()
334
335   const audience = getAudience(serverActor)
336   const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
337
338   if (req.path.endsWith('/activity')) {
339     const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
340     return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
341   }
342
343   return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
344 }
345
346 async function videoPlaylistController (req: express.Request, res: express.Response) {
347   const playlist = res.locals.videoPlaylistFull
348
349   // We need more attributes
350   playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
351
352   const json = await playlist.toActivityPubObject(req.query.page, null)
353   const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
354   const object = audiencify(json, audience)
355
356   return activityPubResponse(activityPubContextify(object), res)
357 }
358
359 function videoPlaylistElementController (req: express.Request, res: express.Response) {
360   const videoPlaylistElement = res.locals.videoPlaylistElementAP
361
362   const json = videoPlaylistElement.toActivityPubObject()
363   return activityPubResponse(activityPubContextify(json), res)
364 }
365
366 // ---------------------------------------------------------------------------
367
368 async function actorFollowing (req: express.Request, actor: MActorId) {
369   const handler = (start: number, count: number) => {
370     return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
371   }
372
373   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
374 }
375
376 async function actorFollowers (req: express.Request, actor: MActorId) {
377   const handler = (start: number, count: number) => {
378     return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
379   }
380
381   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
382 }
383
384 async function actorPlaylists (req: express.Request, account: MAccountId) {
385   const handler = (start: number, count: number) => {
386     return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
387   }
388
389   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
390 }
391
392 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
393   const handler = async (start: number, count: number) => {
394     const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
395     return {
396       total: result.count,
397       data: result.rows.map(r => r.url)
398     }
399   }
400   return activityPubCollectionPagination(url, handler, req.query.page)
401 }