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