aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/activitypub
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/activitypub')
-rw-r--r--server/controllers/activitypub/client.ts482
-rw-r--r--server/controllers/activitypub/inbox.ts85
-rw-r--r--server/controllers/activitypub/index.ts17
-rw-r--r--server/controllers/activitypub/outbox.ts86
-rw-r--r--server/controllers/activitypub/utils.ts12
5 files changed, 0 insertions, 682 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts
deleted file mode 100644
index be52f1662..000000000
--- a/server/controllers/activitypub/client.ts
+++ /dev/null
@@ -1,482 +0,0 @@
1import cors from 'cors'
2import express from 'express'
3import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
4import { activityPubContextify } from '@server/lib/activitypub/context'
5import { getServerActor } from '@server/models/application/application'
6import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models'
7import { VideoCommentObject } from '@shared/models'
8import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
9import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
10import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
11import { audiencify, getAudience } from '../../lib/activitypub/audience'
12import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
13import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
14import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
15import {
16 getLocalVideoCommentsActivityPubUrl,
17 getLocalVideoDislikesActivityPubUrl,
18 getLocalVideoLikesActivityPubUrl,
19 getLocalVideoSharesActivityPubUrl
20} from '../../lib/activitypub/url'
21import {
22 activityPubRateLimiter,
23 asyncMiddleware,
24 ensureIsLocalChannel,
25 executeIfActivityPub,
26 localAccountValidator,
27 videoChannelsNameWithHostValidator,
28 videosCustomGetValidator,
29 videosShareValidator
30} from '../../middlewares'
31import { cacheRoute } from '../../middlewares/cache/cache'
32import { getAccountVideoRateValidatorFactory, getVideoLocalViewerValidator, videoCommentGetValidator } from '../../middlewares/validators'
33import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
34import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
35import { AccountModel } from '../../models/account/account'
36import { AccountVideoRateModel } from '../../models/account/account-video-rate'
37import { ActorFollowModel } from '../../models/actor/actor-follow'
38import { VideoCommentModel } from '../../models/video/video-comment'
39import { VideoPlaylistModel } from '../../models/video/video-playlist'
40import { VideoShareModel } from '../../models/video/video-share'
41import { activityPubResponse } from './utils'
42
43const activityPubClientRouter = express.Router()
44activityPubClientRouter.use(cors())
45
46// Intercept ActivityPub client requests
47
48activityPubClientRouter.get(
49 [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ],
50 executeIfActivityPub,
51 activityPubRateLimiter,
52 asyncMiddleware(localAccountValidator),
53 asyncMiddleware(accountController)
54)
55activityPubClientRouter.get('/accounts?/:name/followers',
56 executeIfActivityPub,
57 activityPubRateLimiter,
58 asyncMiddleware(localAccountValidator),
59 asyncMiddleware(accountFollowersController)
60)
61activityPubClientRouter.get('/accounts?/:name/following',
62 executeIfActivityPub,
63 activityPubRateLimiter,
64 asyncMiddleware(localAccountValidator),
65 asyncMiddleware(accountFollowingController)
66)
67activityPubClientRouter.get('/accounts?/:name/playlists',
68 executeIfActivityPub,
69 activityPubRateLimiter,
70 asyncMiddleware(localAccountValidator),
71 asyncMiddleware(accountPlaylistsController)
72)
73activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
74 executeIfActivityPub,
75 activityPubRateLimiter,
76 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
77 asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
78 asyncMiddleware(getAccountVideoRateFactory('like'))
79)
80activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
81 executeIfActivityPub,
82 activityPubRateLimiter,
83 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
84 asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
85 asyncMiddleware(getAccountVideoRateFactory('dislike'))
86)
87
88activityPubClientRouter.get(
89 [ '/videos/watch/:id', '/w/:id' ],
90 executeIfActivityPub,
91 activityPubRateLimiter,
92 cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
93 asyncMiddleware(videosCustomGetValidator('all')),
94 asyncMiddleware(videoController)
95)
96activityPubClientRouter.get('/videos/watch/:id/activity',
97 executeIfActivityPub,
98 activityPubRateLimiter,
99 asyncMiddleware(videosCustomGetValidator('all')),
100 asyncMiddleware(videoController)
101)
102activityPubClientRouter.get('/videos/watch/:id/announces',
103 executeIfActivityPub,
104 activityPubRateLimiter,
105 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
106 asyncMiddleware(videoAnnouncesController)
107)
108activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
109 executeIfActivityPub,
110 activityPubRateLimiter,
111 asyncMiddleware(videosShareValidator),
112 asyncMiddleware(videoAnnounceController)
113)
114activityPubClientRouter.get('/videos/watch/:id/likes',
115 executeIfActivityPub,
116 activityPubRateLimiter,
117 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
118 asyncMiddleware(videoLikesController)
119)
120activityPubClientRouter.get('/videos/watch/:id/dislikes',
121 executeIfActivityPub,
122 activityPubRateLimiter,
123 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
124 asyncMiddleware(videoDislikesController)
125)
126activityPubClientRouter.get('/videos/watch/:id/comments',
127 executeIfActivityPub,
128 activityPubRateLimiter,
129 asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
130 asyncMiddleware(videoCommentsController)
131)
132activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
133 executeIfActivityPub,
134 activityPubRateLimiter,
135 asyncMiddleware(videoCommentGetValidator),
136 asyncMiddleware(videoCommentController)
137)
138activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
139 executeIfActivityPub,
140 activityPubRateLimiter,
141 asyncMiddleware(videoCommentGetValidator),
142 asyncMiddleware(videoCommentController)
143)
144
145activityPubClientRouter.get(
146 [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ],
147 executeIfActivityPub,
148 activityPubRateLimiter,
149 asyncMiddleware(videoChannelsNameWithHostValidator),
150 ensureIsLocalChannel,
151 asyncMiddleware(videoChannelController)
152)
153activityPubClientRouter.get('/video-channels/:nameWithHost/followers',
154 executeIfActivityPub,
155 activityPubRateLimiter,
156 asyncMiddleware(videoChannelsNameWithHostValidator),
157 ensureIsLocalChannel,
158 asyncMiddleware(videoChannelFollowersController)
159)
160activityPubClientRouter.get('/video-channels/:nameWithHost/following',
161 executeIfActivityPub,
162 activityPubRateLimiter,
163 asyncMiddleware(videoChannelsNameWithHostValidator),
164 ensureIsLocalChannel,
165 asyncMiddleware(videoChannelFollowingController)
166)
167activityPubClientRouter.get('/video-channels/:nameWithHost/playlists',
168 executeIfActivityPub,
169 activityPubRateLimiter,
170 asyncMiddleware(videoChannelsNameWithHostValidator),
171 ensureIsLocalChannel,
172 asyncMiddleware(videoChannelPlaylistsController)
173)
174
175activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
176 executeIfActivityPub,
177 activityPubRateLimiter,
178 asyncMiddleware(videoFileRedundancyGetValidator),
179 asyncMiddleware(videoRedundancyController)
180)
181activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
182 executeIfActivityPub,
183 activityPubRateLimiter,
184 asyncMiddleware(videoPlaylistRedundancyGetValidator),
185 asyncMiddleware(videoRedundancyController)
186)
187
188activityPubClientRouter.get(
189 [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ],
190 executeIfActivityPub,
191 activityPubRateLimiter,
192 asyncMiddleware(videoPlaylistsGetValidator('all')),
193 asyncMiddleware(videoPlaylistController)
194)
195activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
196 executeIfActivityPub,
197 activityPubRateLimiter,
198 asyncMiddleware(videoPlaylistElementAPGetValidator),
199 asyncMiddleware(videoPlaylistElementController)
200)
201
202activityPubClientRouter.get('/videos/local-viewer/:localViewerId',
203 executeIfActivityPub,
204 activityPubRateLimiter,
205 asyncMiddleware(getVideoLocalViewerValidator),
206 asyncMiddleware(getVideoLocalViewerController)
207)
208
209// ---------------------------------------------------------------------------
210
211export {
212 activityPubClientRouter
213}
214
215// ---------------------------------------------------------------------------
216
217async function accountController (req: express.Request, res: express.Response) {
218 const account = res.locals.account
219
220 return activityPubResponse(activityPubContextify(await account.toActivityPubObject(), 'Actor'), res)
221}
222
223async function accountFollowersController (req: express.Request, res: express.Response) {
224 const account = res.locals.account
225 const activityPubResult = await actorFollowers(req, account.Actor)
226
227 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
228}
229
230async function accountFollowingController (req: express.Request, res: express.Response) {
231 const account = res.locals.account
232 const activityPubResult = await actorFollowing(req, account.Actor)
233
234 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
235}
236
237async function accountPlaylistsController (req: express.Request, res: express.Response) {
238 const account = res.locals.account
239 const activityPubResult = await actorPlaylists(req, { account })
240
241 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
242}
243
244async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
245 const channel = res.locals.videoChannel
246 const activityPubResult = await actorPlaylists(req, { channel })
247
248 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
249}
250
251function getAccountVideoRateFactory (rateType: VideoRateType) {
252 return (req: express.Request, res: express.Response) => {
253 const accountVideoRate = res.locals.accountVideoRate
254
255 const byActor = accountVideoRate.Account.Actor
256 const APObject = rateType === 'like'
257 ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
258 : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
259
260 return activityPubResponse(activityPubContextify(APObject, 'Rate'), res)
261 }
262}
263
264async function videoController (req: express.Request, res: express.Response) {
265 const video = res.locals.videoAll
266
267 if (redirectIfNotOwned(video.url, res)) return
268
269 // We need captions to render AP object
270 const videoAP = await video.lightAPToFullAP(undefined)
271
272 const audience = getAudience(videoAP.VideoChannel.Account.Actor, videoAP.privacy === VideoPrivacy.PUBLIC)
273 const videoObject = audiencify(await videoAP.toActivityPubObject(), audience)
274
275 if (req.path.endsWith('/activity')) {
276 const data = buildCreateActivity(videoAP.url, video.VideoChannel.Account.Actor, videoObject, audience)
277 return activityPubResponse(activityPubContextify(data, 'Video'), res)
278 }
279
280 return activityPubResponse(activityPubContextify(videoObject, 'Video'), res)
281}
282
283async function videoAnnounceController (req: express.Request, res: express.Response) {
284 const share = res.locals.videoShare
285
286 if (redirectIfNotOwned(share.url, res)) return
287
288 const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
289
290 return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
291}
292
293async function videoAnnouncesController (req: express.Request, res: express.Response) {
294 const video = res.locals.onlyImmutableVideo
295
296 if (redirectIfNotOwned(video.url, res)) return
297
298 const handler = async (start: number, count: number) => {
299 const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
300 return {
301 total: result.total,
302 data: result.data.map(r => r.url)
303 }
304 }
305 const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
306
307 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
308}
309
310async function videoLikesController (req: express.Request, res: express.Response) {
311 const video = res.locals.onlyImmutableVideo
312
313 if (redirectIfNotOwned(video.url, res)) return
314
315 const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
316
317 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
318}
319
320async function videoDislikesController (req: express.Request, res: express.Response) {
321 const video = res.locals.onlyImmutableVideo
322
323 if (redirectIfNotOwned(video.url, res)) return
324
325 const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
326
327 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
328}
329
330async function videoCommentsController (req: express.Request, res: express.Response) {
331 const video = res.locals.onlyImmutableVideo
332
333 if (redirectIfNotOwned(video.url, res)) return
334
335 const handler = async (start: number, count: number) => {
336 const result = await VideoCommentModel.listAndCountByVideoForAP({ video, start, count })
337
338 return {
339 total: result.total,
340 data: result.data.map(r => r.url)
341 }
342 }
343 const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
344
345 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
346}
347
348async function videoChannelController (req: express.Request, res: express.Response) {
349 const videoChannel = res.locals.videoChannel
350
351 return activityPubResponse(activityPubContextify(await videoChannel.toActivityPubObject(), 'Actor'), res)
352}
353
354async function videoChannelFollowersController (req: express.Request, res: express.Response) {
355 const videoChannel = res.locals.videoChannel
356 const activityPubResult = await actorFollowers(req, videoChannel.Actor)
357
358 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
359}
360
361async function videoChannelFollowingController (req: express.Request, res: express.Response) {
362 const videoChannel = res.locals.videoChannel
363 const activityPubResult = await actorFollowing(req, videoChannel.Actor)
364
365 return activityPubResponse(activityPubContextify(activityPubResult, 'Collection'), res)
366}
367
368async function videoCommentController (req: express.Request, res: express.Response) {
369 const videoComment = res.locals.videoCommentFull
370
371 if (redirectIfNotOwned(videoComment.url, res)) return
372
373 const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
374 const isPublic = true // Comments are always public
375 let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
376
377 if (videoComment.Account) {
378 const audience = getAudience(videoComment.Account.Actor, isPublic)
379 videoCommentObject = audiencify(videoCommentObject, audience)
380
381 if (req.path.endsWith('/activity')) {
382 const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject as VideoCommentObject, audience)
383 return activityPubResponse(activityPubContextify(data, 'Comment'), res)
384 }
385 }
386
387 return activityPubResponse(activityPubContextify(videoCommentObject, 'Comment'), res)
388}
389
390async function videoRedundancyController (req: express.Request, res: express.Response) {
391 const videoRedundancy = res.locals.videoRedundancy
392
393 if (redirectIfNotOwned(videoRedundancy.url, res)) return
394
395 const serverActor = await getServerActor()
396
397 const audience = getAudience(serverActor)
398 const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
399
400 if (req.path.endsWith('/activity')) {
401 const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
402 return activityPubResponse(activityPubContextify(data, 'CacheFile'), res)
403 }
404
405 return activityPubResponse(activityPubContextify(object, 'CacheFile'), res)
406}
407
408async function videoPlaylistController (req: express.Request, res: express.Response) {
409 const playlist = res.locals.videoPlaylistFull
410
411 if (redirectIfNotOwned(playlist.url, res)) return
412
413 // We need more attributes
414 playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
415
416 const json = await playlist.toActivityPubObject(req.query.page, null)
417 const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
418 const object = audiencify(json, audience)
419
420 return activityPubResponse(activityPubContextify(object, 'Playlist'), res)
421}
422
423function videoPlaylistElementController (req: express.Request, res: express.Response) {
424 const videoPlaylistElement = res.locals.videoPlaylistElementAP
425
426 if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
427
428 const json = videoPlaylistElement.toActivityPubObject()
429 return activityPubResponse(activityPubContextify(json, 'Playlist'), res)
430}
431
432function getVideoLocalViewerController (req: express.Request, res: express.Response) {
433 const localViewer = res.locals.localViewerFull
434
435 return activityPubResponse(activityPubContextify(localViewer.toActivityPubObject(), 'WatchAction'), res)
436}
437
438// ---------------------------------------------------------------------------
439
440function actorFollowing (req: express.Request, actor: MActorId) {
441 const handler = (start: number, count: number) => {
442 return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
443 }
444
445 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
446}
447
448function actorFollowers (req: express.Request, actor: MActorId) {
449 const handler = (start: number, count: number) => {
450 return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
451 }
452
453 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
454}
455
456function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
457 const handler = (start: number, count: number) => {
458 return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
459 }
460
461 return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
462}
463
464function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
465 const handler = async (start: number, count: number) => {
466 const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
467 return {
468 total: result.total,
469 data: result.data.map(r => r.url)
470 }
471 }
472 return activityPubCollectionPagination(url, handler, req.query.page)
473}
474
475function redirectIfNotOwned (url: string, res: express.Response) {
476 if (url.startsWith(WEBSERVER.URL) === false) {
477 res.redirect(url)
478 return true
479 }
480
481 return false
482}
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts
deleted file mode 100644
index 862c7baf1..000000000
--- a/server/controllers/activitypub/inbox.ts
+++ /dev/null
@@ -1,85 +0,0 @@
1import express from 'express'
2import { InboxManager } from '@server/lib/activitypub/inbox-manager'
3import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '@shared/models'
4import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
5import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
6import { logger } from '../../helpers/logger'
7import {
8 activityPubRateLimiter,
9 asyncMiddleware,
10 checkSignature,
11 ensureIsLocalChannel,
12 localAccountValidator,
13 signatureValidator,
14 videoChannelsNameWithHostValidator
15} from '../../middlewares'
16import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
17
18const inboxRouter = express.Router()
19
20inboxRouter.post('/inbox',
21 activityPubRateLimiter,
22 signatureValidator,
23 asyncMiddleware(checkSignature),
24 asyncMiddleware(activityPubValidator),
25 inboxController
26)
27
28inboxRouter.post('/accounts/:name/inbox',
29 activityPubRateLimiter,
30 signatureValidator,
31 asyncMiddleware(checkSignature),
32 asyncMiddleware(localAccountValidator),
33 asyncMiddleware(activityPubValidator),
34 inboxController
35)
36
37inboxRouter.post('/video-channels/:nameWithHost/inbox',
38 activityPubRateLimiter,
39 signatureValidator,
40 asyncMiddleware(checkSignature),
41 asyncMiddleware(videoChannelsNameWithHostValidator),
42 ensureIsLocalChannel,
43 asyncMiddleware(activityPubValidator),
44 inboxController
45)
46
47// ---------------------------------------------------------------------------
48
49export {
50 inboxRouter
51}
52
53// ---------------------------------------------------------------------------
54
55function inboxController (req: express.Request, res: express.Response) {
56 const rootActivity: RootActivity = req.body
57 let activities: Activity[]
58
59 if ([ 'Collection', 'CollectionPage' ].includes(rootActivity.type)) {
60 activities = (rootActivity as ActivityPubCollection).items
61 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].includes(rootActivity.type)) {
62 activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
63 } else {
64 activities = [ rootActivity as Activity ]
65 }
66
67 // Only keep activities we are able to process
68 logger.debug('Filtering %d activities...', activities.length)
69 activities = activities.filter(a => isActivityValid(a))
70 logger.debug('We keep %d activities.', activities.length, { activities })
71
72 const accountOrChannel = res.locals.account || res.locals.videoChannel
73
74 logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
75
76 InboxManager.Instance.addInboxMessage({
77 activities,
78 signatureActor: res.locals.signature.actor,
79 inboxActor: accountOrChannel
80 ? accountOrChannel.Actor
81 : undefined
82 })
83
84 return res.status(HttpStatusCode.NO_CONTENT_204).end()
85}
diff --git a/server/controllers/activitypub/index.ts b/server/controllers/activitypub/index.ts
deleted file mode 100644
index c14d95108..000000000
--- a/server/controllers/activitypub/index.ts
+++ /dev/null
@@ -1,17 +0,0 @@
1import express from 'express'
2
3import { activityPubClientRouter } from './client'
4import { inboxRouter } from './inbox'
5import { outboxRouter } from './outbox'
6
7const activityPubRouter = express.Router()
8
9activityPubRouter.use('/', inboxRouter)
10activityPubRouter.use('/', outboxRouter)
11activityPubRouter.use('/', activityPubClientRouter)
12
13// ---------------------------------------------------------------------------
14
15export {
16 activityPubRouter
17}
diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts
deleted file mode 100644
index 8c88b6971..000000000
--- a/server/controllers/activitypub/outbox.ts
+++ /dev/null
@@ -1,86 +0,0 @@
1import express from 'express'
2import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
3import { activityPubContextify } from '@server/lib/activitypub/context'
4import { MActorLight } from '@server/types/models'
5import { Activity } from '../../../shared/models/activitypub/activity'
6import { VideoPrivacy } from '../../../shared/models/videos'
7import { logger } from '../../helpers/logger'
8import { buildAudience } from '../../lib/activitypub/audience'
9import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
10import {
11 activityPubRateLimiter,
12 asyncMiddleware,
13 ensureIsLocalChannel,
14 localAccountValidator,
15 videoChannelsNameWithHostValidator
16} from '../../middlewares'
17import { apPaginationValidator } from '../../middlewares/validators/activitypub'
18import { VideoModel } from '../../models/video/video'
19import { activityPubResponse } from './utils'
20
21const outboxRouter = express.Router()
22
23outboxRouter.get('/accounts/:name/outbox',
24 activityPubRateLimiter,
25 apPaginationValidator,
26 localAccountValidator,
27 asyncMiddleware(outboxController)
28)
29
30outboxRouter.get('/video-channels/:nameWithHost/outbox',
31 activityPubRateLimiter,
32 apPaginationValidator,
33 asyncMiddleware(videoChannelsNameWithHostValidator),
34 ensureIsLocalChannel,
35 asyncMiddleware(outboxController)
36)
37
38// ---------------------------------------------------------------------------
39
40export {
41 outboxRouter
42}
43
44// ---------------------------------------------------------------------------
45
46async function outboxController (req: express.Request, res: express.Response) {
47 const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
48 const actor = accountOrVideoChannel.Actor
49 const actorOutboxUrl = actor.url + '/outbox'
50
51 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
52
53 const handler = (start: number, count: number) => buildActivities(actor, start, count)
54 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
55
56 return activityPubResponse(activityPubContextify(json, 'Collection'), res)
57}
58
59async function buildActivities (actor: MActorLight, start: number, count: number) {
60 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
61 const activities: Activity[] = []
62
63 for (const video of data.data) {
64 const byActor = video.VideoChannel.Account.Actor
65 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
66
67 // This is a shared video
68 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
69 const videoShare = video.VideoShares[0]
70 const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
71
72 activities.push(announceActivity)
73 } else {
74 // FIXME: only use the video URL to reduce load. Breaks compat with PeerTube < 6.0.0
75 const videoObject = await video.toActivityPubObject()
76 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
77
78 activities.push(createActivity)
79 }
80 }
81
82 return {
83 data: activities,
84 total: data.total
85 }
86}
diff --git a/server/controllers/activitypub/utils.ts b/server/controllers/activitypub/utils.ts
deleted file mode 100644
index 5de38eb43..000000000
--- a/server/controllers/activitypub/utils.ts
+++ /dev/null
@@ -1,12 +0,0 @@
1import express from 'express'
2
3async function activityPubResponse (promise: Promise<any>, res: express.Response) {
4 const data = await promise
5
6 return res.type('application/activity+json; charset=utf-8')
7 .json(data)
8}
9
10export {
11 activityPubResponse
12}