]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/client.ts
Block comments from muted accounts/servers
[github/Chocobozzz/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, MChannelId } 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 activityPubClientRouter.get('/video-channels/:name/playlists',
141 executeIfActivityPub,
142 asyncMiddleware(localVideoChannelValidator),
143 asyncMiddleware(videoChannelPlaylistsController)
144 )
145
146 activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
147 executeIfActivityPub,
148 asyncMiddleware(videoFileRedundancyGetValidator),
149 asyncMiddleware(videoRedundancyController)
150 )
151 activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
152 executeIfActivityPub,
153 asyncMiddleware(videoPlaylistRedundancyGetValidator),
154 asyncMiddleware(videoRedundancyController)
155 )
156
157 activityPubClientRouter.get('/video-playlists/:playlistId',
158 executeIfActivityPub,
159 asyncMiddleware(videoPlaylistsGetValidator('all')),
160 asyncMiddleware(videoPlaylistController)
161 )
162 activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
163 executeIfActivityPub,
164 asyncMiddleware(videoPlaylistElementAPGetValidator),
165 videoPlaylistElementController
166 )
167
168 // ---------------------------------------------------------------------------
169
170 export {
171 activityPubClientRouter
172 }
173
174 // ---------------------------------------------------------------------------
175
176 function accountController (req: express.Request, res: express.Response) {
177 const account = res.locals.account
178
179 return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
180 }
181
182 async function accountFollowersController (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184 const activityPubResult = await actorFollowers(req, account.Actor)
185
186 return activityPubResponse(activityPubContextify(activityPubResult), res)
187 }
188
189 async function accountFollowingController (req: express.Request, res: express.Response) {
190 const account = res.locals.account
191 const activityPubResult = await actorFollowing(req, account.Actor)
192
193 return activityPubResponse(activityPubContextify(activityPubResult), res)
194 }
195
196 async function accountPlaylistsController (req: express.Request, res: express.Response) {
197 const account = res.locals.account
198 const activityPubResult = await actorPlaylists(req, { account })
199
200 return activityPubResponse(activityPubContextify(activityPubResult), res)
201 }
202
203 async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
204 const channel = res.locals.videoChannel
205 const activityPubResult = await actorPlaylists(req, { channel })
206
207 return activityPubResponse(activityPubContextify(activityPubResult), res)
208 }
209
210 function getAccountVideoRateFactory (rateType: VideoRateType) {
211 return (req: express.Request, res: express.Response) => {
212 const accountVideoRate = res.locals.accountVideoRate
213
214 const byActor = accountVideoRate.Account.Actor
215 const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
216 const APObject = rateType === 'like'
217 ? buildLikeActivity(url, byActor, accountVideoRate.Video)
218 : buildDislikeActivity(url, byActor, accountVideoRate.Video)
219
220 return activityPubResponse(activityPubContextify(APObject), res)
221 }
222 }
223
224 async function videoController (req: express.Request, res: express.Response) {
225 // We need more attributes
226 const video = await VideoModel.loadForGetAPI({ id: res.locals.onlyVideoWithRights.id }) as MVideoAPWithoutCaption
227
228 if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
229
230 // We need captions to render AP object
231 const captions = await VideoCaptionModel.listVideoCaptions(video.id)
232 const videoWithCaptions = Object.assign(video, { VideoCaptions: captions })
233
234 const audience = getAudience(videoWithCaptions.VideoChannel.Account.Actor, videoWithCaptions.privacy === VideoPrivacy.PUBLIC)
235 const videoObject = audiencify(videoWithCaptions.toActivityPubObject(), audience)
236
237 if (req.path.endsWith('/activity')) {
238 const data = buildCreateActivity(videoWithCaptions.url, video.VideoChannel.Account.Actor, videoObject, audience)
239 return activityPubResponse(activityPubContextify(data), res)
240 }
241
242 return activityPubResponse(activityPubContextify(videoObject), res)
243 }
244
245 async function videoAnnounceController (req: express.Request, res: express.Response) {
246 const share = res.locals.videoShare
247
248 if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
249
250 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
251
252 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
253 }
254
255 async function videoAnnouncesController (req: express.Request, res: express.Response) {
256 const video = res.locals.onlyImmutableVideo
257
258 const handler = async (start: number, count: number) => {
259 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
260 return {
261 total: result.count,
262 data: result.rows.map(r => r.url)
263 }
264 }
265 const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
266
267 return activityPubResponse(activityPubContextify(json), res)
268 }
269
270 async function videoLikesController (req: express.Request, res: express.Response) {
271 const video = res.locals.onlyImmutableVideo
272 const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
273
274 return activityPubResponse(activityPubContextify(json), res)
275 }
276
277 async function videoDislikesController (req: express.Request, res: express.Response) {
278 const video = res.locals.onlyImmutableVideo
279 const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
280
281 return activityPubResponse(activityPubContextify(json), res)
282 }
283
284 async function videoCommentsController (req: express.Request, res: express.Response) {
285 const video = res.locals.onlyImmutableVideo
286
287 const handler = async (start: number, count: number) => {
288 const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
289 return {
290 total: result.count,
291 data: result.rows.map(r => r.url)
292 }
293 }
294 const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
295
296 return activityPubResponse(activityPubContextify(json), res)
297 }
298
299 function videoChannelController (req: express.Request, res: express.Response) {
300 const videoChannel = res.locals.videoChannel
301
302 return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
303 }
304
305 async function videoChannelFollowersController (req: express.Request, res: express.Response) {
306 const videoChannel = res.locals.videoChannel
307 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
308
309 return activityPubResponse(activityPubContextify(activityPubResult), res)
310 }
311
312 async function videoChannelFollowingController (req: express.Request, res: express.Response) {
313 const videoChannel = res.locals.videoChannel
314 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
315
316 return activityPubResponse(activityPubContextify(activityPubResult), res)
317 }
318
319 async function videoCommentController (req: express.Request, res: express.Response) {
320 const videoComment = res.locals.videoCommentFull
321
322 if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
323
324 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
325 const isPublic = true // Comments are always public
326 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
327
328 if (videoComment.Account) {
329 const audience = getAudience(videoComment.Account.Actor, isPublic)
330 videoCommentObject = audiencify(videoCommentObject, audience)
331
332 if (req.path.endsWith('/activity')) {
333 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
334 return activityPubResponse(activityPubContextify(data), res)
335 }
336 }
337
338 return activityPubResponse(activityPubContextify(videoCommentObject), res)
339 }
340
341 async function videoRedundancyController (req: express.Request, res: express.Response) {
342 const videoRedundancy = res.locals.videoRedundancy
343 if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
344
345 const serverActor = await getServerActor()
346
347 const audience = getAudience(serverActor)
348 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
349
350 if (req.path.endsWith('/activity')) {
351 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
352 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
353 }
354
355 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
356 }
357
358 async function videoPlaylistController (req: express.Request, res: express.Response) {
359 const playlist = res.locals.videoPlaylistFull
360
361 // We need more attributes
362 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
363
364 const json = await playlist.toActivityPubObject(req.query.page, null)
365 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
366 const object = audiencify(json, audience)
367
368 return activityPubResponse(activityPubContextify(object), res)
369 }
370
371 function videoPlaylistElementController (req: express.Request, res: express.Response) {
372 const videoPlaylistElement = res.locals.videoPlaylistElementAP
373
374 const json = videoPlaylistElement.toActivityPubObject()
375 return activityPubResponse(activityPubContextify(json), res)
376 }
377
378 // ---------------------------------------------------------------------------
379
380 async function actorFollowing (req: express.Request, actor: MActorId) {
381 const handler = (start: number, count: number) => {
382 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
383 }
384
385 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
386 }
387
388 async function actorFollowers (req: express.Request, actor: MActorId) {
389 const handler = (start: number, count: number) => {
390 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
391 }
392
393 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
394 }
395
396 async function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
397 const handler = (start: number, count: number) => {
398 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
399 }
400
401 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
402 }
403
404 function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
405 const handler = async (start: number, count: number) => {
406 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
407 return {
408 total: result.count,
409 data: result.rows.map(r => r.url)
410 }
411 }
412 return activityPubCollectionPagination(url, handler, req.query.page)
413 }