1 import * as express from 'express'
2 import { Activity } from '../../../shared/models/activitypub/activity'
3 import { VideoPrivacy } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { logger } from '../../helpers/logger'
6 import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
7 import { buildAudience } from '../../lib/activitypub/audience'
8 import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
9 import { VideoModel } from '../../models/video/video'
10 import { activityPubResponse } from './utils'
11 import { MActorLight } from '@server/typings/models'
13 const outboxRouter = express.Router()
15 outboxRouter.get('/accounts/:name/outbox',
16 localAccountValidator,
17 asyncMiddleware(outboxController)
20 outboxRouter.get('/video-channels/:name/outbox',
21 localVideoChannelValidator,
22 asyncMiddleware(outboxController)
25 // ---------------------------------------------------------------------------
31 // ---------------------------------------------------------------------------
33 async function outboxController (req: express.Request, res: express.Response) {
34 const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
35 const actor = accountOrVideoChannel.Actor
36 const actorOutboxUrl = actor.url + '/outbox'
38 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
40 const handler = (start: number, count: number) => buildActivities(actor, start, count)
41 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
43 return activityPubResponse(activityPubContextify(json), res)
46 async function buildActivities (actor: MActorLight, start: number, count: number) {
47 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
48 const activities: Activity[] = []
50 for (const video of data.data) {
51 const byActor = video.VideoChannel.Account.Actor
52 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
54 // This is a shared video
55 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
56 const videoShare = video.VideoShares[0]
57 const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
59 activities.push(announceActivity)
61 const videoObject = video.toActivityPubObject()
62 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
64 activities.push(createActivity)