Add action hooks to user routes
[oweals/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   let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
312
313   if (videoComment.Account) {
314     const audience = getAudience(videoComment.Account.Actor, isPublic)
315     videoCommentObject = audiencify(videoCommentObject, 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
323   return activityPubResponse(activityPubContextify(videoCommentObject), res)
324 }
325
326 async function videoRedundancyController (req: express.Request, res: express.Response) {
327   const videoRedundancy = res.locals.videoRedundancy
328   if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
329
330   const serverActor = await getServerActor()
331
332   const audience = getAudience(serverActor)
333   const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
334
335   if (req.path.endsWith('/activity')) {
336     const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
337     return activityPubResponse(activityPubContextify(data), res)
338   }
339
340   return activityPubResponse(activityPubContextify(object), res)
341 }
342
343 async function videoPlaylistController (req: express.Request, res: express.Response) {
344   const playlist = res.locals.videoPlaylistFull
345
346   // We need more attributes
347   playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
348
349   const json = await playlist.toActivityPubObject(req.query.page, null)
350   const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
351   const object = audiencify(json, audience)
352
353   return activityPubResponse(activityPubContextify(object), res)
354 }
355
356 async function videoPlaylistElementController (req: express.Request, res: express.Response) {
357   const videoPlaylistElement = res.locals.videoPlaylistElementAP
358
359   const json = videoPlaylistElement.toActivityPubObject()
360   return activityPubResponse(activityPubContextify(json), res)
361 }
362
363 // ---------------------------------------------------------------------------
364
365 async function actorFollowing (req: express.Request, actor: MActorId) {
366   const handler = (start: number, count: number) => {
367     return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
368   }
369
370   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
371 }
372
373 async function actorFollowers (req: express.Request, actor: MActorId) {
374   const handler = (start: number, count: number) => {
375     return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
376   }
377
378   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
379 }
380
381 async function actorPlaylists (req: express.Request, account: MAccountId) {
382   const handler = (start: number, count: number) => {
383     return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
384   }
385
386   return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
387 }
388
389 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) {
390   const handler = async (start: number, count: number) => {
391     const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
392     return {
393       total: result.count,
394       data: result.rows.map(r => r.url)
395     }
396   }
397   return activityPubCollectionPagination(url, handler, req.query.page)
398 }