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'
14 const outboxRouter = express.Router()
16 outboxRouter.get('/accounts/:name/outbox',
17 apPaginationValidator,
18 localAccountValidator,
19 asyncMiddleware(outboxController)
22 outboxRouter.get('/video-channels/:nameWithHost/outbox',
23 apPaginationValidator,
24 asyncMiddleware(videoChannelsNameWithHostValidator),
26 asyncMiddleware(outboxController)
29 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
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'
42 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
44 const handler = (start: number, count: number) => buildActivities(actor, start, count)
45 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
47 return activityPubResponse(activityPubContextify(json), res)
50 async function buildActivities (actor: MActorLight, start: number, count: number) {
51 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
52 const activities: Activity[] = []
54 for (const video of data.data) {
55 const byActor = video.VideoChannel.Account.Actor
56 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
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)
63 activities.push(announceActivity)
65 const videoObject = video.toActivityPubObject()
66 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
68 activities.push(createActivity)