2017-11-21 11:23:10 -06:00
|
|
|
import * as express from 'express'
|
2017-11-22 11:31:40 -06:00
|
|
|
import { Activity } from '../../../shared/models/activitypub/activity'
|
2017-11-22 09:25:03 -06:00
|
|
|
import { activityPubCollectionPagination } from '../../helpers/activitypub'
|
|
|
|
import { pageToStartAndCount } from '../../helpers/core-utils'
|
2017-11-21 11:23:10 -06:00
|
|
|
import { database as db } from '../../initializers'
|
2017-11-22 09:25:03 -06:00
|
|
|
import { ACTIVITY_PUB } from '../../initializers/constants'
|
2017-11-21 11:23:10 -06:00
|
|
|
import { addActivityData } from '../../lib/activitypub/send/send-add'
|
|
|
|
import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
|
|
|
|
import { announceActivityData } from '../../lib/index'
|
|
|
|
import { asyncMiddleware, localAccountValidator } from '../../middlewares'
|
|
|
|
import { AccountInstance } from '../../models/account/account-interface'
|
|
|
|
|
|
|
|
const outboxRouter = express.Router()
|
|
|
|
|
|
|
|
outboxRouter.get('/account/:name/outbox',
|
|
|
|
localAccountValidator,
|
|
|
|
asyncMiddleware(outboxController)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
outboxRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const account: AccountInstance = res.locals.account
|
|
|
|
|
2017-11-22 04:27:40 -06:00
|
|
|
const page = req.query.page || 1
|
2017-11-21 11:23:10 -06:00
|
|
|
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
|
|
|
|
|
|
|
|
const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count)
|
|
|
|
const activities: Activity[] = []
|
|
|
|
|
|
|
|
for (const video of data.data) {
|
|
|
|
const videoObject = video.toActivityPubObject()
|
|
|
|
|
|
|
|
// This is a shared video
|
2017-11-22 09:25:03 -06:00
|
|
|
if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
|
|
|
|
const addActivity = await addActivityData(video.url, video.VideoChannel.Account, video, video.VideoChannel.url, videoObject)
|
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
const url = getAnnounceActivityPubUrl(video.url, account)
|
|
|
|
const announceActivity = await announceActivityData(url, account, addActivity)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
activities.push(announceActivity)
|
|
|
|
} else {
|
2017-11-22 09:25:03 -06:00
|
|
|
const addActivity = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject)
|
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
activities.push(addActivity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const newResult = {
|
|
|
|
data: activities,
|
|
|
|
total: data.total
|
|
|
|
}
|
|
|
|
const json = activityPubCollectionPagination(account.url + '/outbox', page, newResult)
|
|
|
|
|
|
|
|
return res.json(json).end()
|
|
|
|
}
|