2017-11-21 18:23:10 +01:00
|
|
|
import * as express from 'express'
|
2017-11-22 18:31:40 +01:00
|
|
|
import { Activity } from '../../../shared/models/activitypub/activity'
|
2017-11-22 16:25:03 +01:00
|
|
|
import { activityPubCollectionPagination } from '../../helpers/activitypub'
|
|
|
|
import { pageToStartAndCount } from '../../helpers/core-utils'
|
|
|
|
import { ACTIVITY_PUB } from '../../initializers/constants'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { announceActivityData, createActivityData } from '../../lib/activitypub/send'
|
2017-11-21 18:23:10 +01:00
|
|
|
import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
|
|
|
|
import { asyncMiddleware, localAccountValidator } from '../../middlewares'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
2017-11-21 18:23:10 +01:00
|
|
|
|
|
|
|
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) {
|
2017-12-12 17:53:50 +01:00
|
|
|
const account: AccountModel = res.locals.account
|
2017-12-14 17:38:41 +01:00
|
|
|
const actor = account.Actor
|
2017-11-21 18:23:10 +01:00
|
|
|
|
2017-11-22 11:27:40 +01:00
|
|
|
const page = req.query.page || 1
|
2017-11-21 18:23:10 +01:00
|
|
|
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
|
2017-11-21 18:23:10 +01:00
|
|
|
const activities: Activity[] = []
|
|
|
|
|
|
|
|
for (const video of data.data) {
|
|
|
|
const videoObject = video.toActivityPubObject()
|
|
|
|
|
2017-11-30 13:16:23 +01:00
|
|
|
const videoChannel = video.VideoChannel
|
2017-12-14 17:38:41 +01:00
|
|
|
// This is a shared video
|
2017-11-22 16:25:03 +01:00
|
|
|
if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
|
2017-12-14 17:38:41 +01:00
|
|
|
const createActivity = await createActivityData(video.url, videoChannel.Account.Actor, videoObject, undefined)
|
2017-11-22 16:25:03 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const url = getAnnounceActivityPubUrl(video.url, actor)
|
|
|
|
const announceActivity = await announceActivityData(url, actor, createActivity, undefined)
|
2017-11-22 16:25:03 +01:00
|
|
|
|
2017-11-21 18:23:10 +01:00
|
|
|
activities.push(announceActivity)
|
|
|
|
} else {
|
2017-12-14 17:38:41 +01:00
|
|
|
const createActivity = await createActivityData(video.url, videoChannel.Account.Actor, videoObject, undefined)
|
2017-11-22 16:25:03 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
activities.push(createActivity)
|
2017-11-21 18:23:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const newResult = {
|
|
|
|
data: activities,
|
|
|
|
total: data.total
|
|
|
|
}
|
2017-12-14 17:38:41 +01:00
|
|
|
const json = activityPubCollectionPagination(account.Actor.url + '/outbox', page, newResult)
|
2017-11-21 18:23:10 +01:00
|
|
|
|
|
|
|
return res.json(json).end()
|
|
|
|
}
|