]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/outbox.ts
Give moderators access to edit channels (#4608)
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / outbox.ts
1 import express from 'express'
2 import { MActorLight } from '@server/types/models'
3 import { Activity } from '../../../shared/models/activitypub/activity'
4 import { VideoPrivacy } from '../../../shared/models/videos'
5 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
6 import { logger } from '../../helpers/logger'
7 import { buildAudience } from '../../lib/activitypub/audience'
8 import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
9 import { asyncMiddleware, ensureIsLocalChannel, localAccountValidator, videoChannelsNameWithHostValidator } from '../../middlewares'
10 import { apPaginationValidator } from '../../middlewares/validators/activitypub'
11 import { VideoModel } from '../../models/video/video'
12 import { activityPubResponse } from './utils'
13
14 const outboxRouter = express.Router()
15
16 outboxRouter.get('/accounts/:name/outbox',
17 apPaginationValidator,
18 localAccountValidator,
19 asyncMiddleware(outboxController)
20 )
21
22 outboxRouter.get('/video-channels/:nameWithHost/outbox',
23 apPaginationValidator,
24 asyncMiddleware(videoChannelsNameWithHostValidator),
25 ensureIsLocalChannel,
26 asyncMiddleware(outboxController)
27 )
28
29 // ---------------------------------------------------------------------------
30
31 export {
32 outboxRouter
33 }
34
35 // ---------------------------------------------------------------------------
36
37 async function outboxController (req: express.Request, res: express.Response) {
38 const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
39 const actor = accountOrVideoChannel.Actor
40 const actorOutboxUrl = actor.url + '/outbox'
41
42 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
43
44 const handler = (start: number, count: number) => buildActivities(actor, start, count)
45 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
46
47 return activityPubResponse(activityPubContextify(json), res)
48 }
49
50 async function buildActivities (actor: MActorLight, start: number, count: number) {
51 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
52 const activities: Activity[] = []
53
54 for (const video of data.data) {
55 const byActor = video.VideoChannel.Account.Actor
56 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
57
58 // This is a shared video
59 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
60 const videoShare = video.VideoShares[0]
61 const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
62
63 activities.push(announceActivity)
64 } else {
65 const videoObject = video.toActivityPubObject()
66 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
67
68 activities.push(createActivity)
69 }
70 }
71
72 return {
73 data: activities,
74 total: data.total
75 }
76 }