1 import * as express from 'express'
2 import { Activity } from '../../../shared/models/activitypub/activity'
3 import { activityPubCollectionPagination } from '../../helpers/activitypub'
4 import { pageToStartAndCount } from '../../helpers/core-utils'
5 import { database as db } from '../../initializers'
6 import { ACTIVITY_PUB } from '../../initializers/constants'
7 import { addActivityData } from '../../lib/activitypub/send/send-add'
8 import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
9 import { announceActivityData } from '../../lib/index'
10 import { asyncMiddleware, localAccountValidator } from '../../middlewares'
11 import { AccountInstance } from '../../models/account/account-interface'
13 const outboxRouter = express.Router()
15 outboxRouter.get('/account/:name/outbox',
16 localAccountValidator,
17 asyncMiddleware(outboxController)
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
29 const account: AccountInstance = res.locals.account
31 const page = req.query.page || 1
32 const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
34 const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count)
35 const activities: Activity[] = []
37 for (const video of data.data) {
38 const videoObject = video.toActivityPubObject()
40 // This is a shared video
41 const videoChannel = video.VideoChannel
42 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
43 const addActivity = await addActivityData(video.url, videoChannel.Account, video, videoChannel.url, videoObject, undefined)
45 const url = getAnnounceActivityPubUrl(video.url, account)
46 const announceActivity = await announceActivityData(url, account, addActivity, undefined)
48 activities.push(announceActivity)
50 const addActivity = await addActivityData(video.url, account, video, videoChannel.url, videoObject, undefined)
52 activities.push(addActivity)
60 const json = activityPubCollectionPagination(account.url + '/outbox', page, newResult)
62 return res.json(json).end()