import * as express from 'express'
import { Activity } from '../../../shared/models/activitypub/activity'
-import { activityPubCollectionPagination } from '../../helpers/activitypub'
-import { pageToStartAndCount } from '../../helpers/core-utils'
-import { ACTIVITY_PUB } from '../../initializers/constants'
-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 { VideoPrivacy } from '../../../shared/models/videos'
+import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
+import { logger } from '../../helpers/logger'
+import { announceActivityData, createActivityData } from '../../lib/activitypub/send'
+import { buildAudience } from '../../lib/activitypub/audience'
+import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
import { AccountModel } from '../../models/account/account'
+import { ActorModel } from '../../models/activitypub/actor'
import { VideoModel } from '../../models/video/video'
+import { activityPubResponse } from './utils'
+import { VideoChannelModel } from '../../models/video/video-channel'
const outboxRouter = express.Router()
-outboxRouter.get('/account/:name/outbox',
+outboxRouter.get('/accounts/:name/outbox',
localAccountValidator,
asyncMiddleware(outboxController)
)
+outboxRouter.get('/video-channels/:name/outbox',
+ localVideoChannelValidator,
+ asyncMiddleware(outboxController)
+)
+
// ---------------------------------------------------------------------------
export {
// ---------------------------------------------------------------------------
async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
- const account: AccountModel = res.locals.account
+ const accountOrVideoChannel: AccountModel | VideoChannelModel = res.locals.account || res.locals.videoChannel
+ const actor = accountOrVideoChannel.Actor
+ const actorOutboxUrl = actor.url + '/outbox'
- const page = req.query.page || 1
- const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
+ logger.info('Receiving outbox request for %s.', actorOutboxUrl)
- const data = await VideoModel.listAllAndSharedByAccountForOutbox(account.id, start, count)
+ const handler = (start: number, count: number) => buildActivities(actor, start, count)
+ const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
+
+ return activityPubResponse(activityPubContextify(json), res)
+}
+
+async function buildActivities (actor: ActorModel, start: number, count: number) {
+ const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
const activities: Activity[] = []
+ // Avoid too many SQL requests
+ const actors = data.data.map(v => v.VideoChannel.Account.Actor)
+ actors.push(actor)
+
for (const video of data.data) {
- const videoObject = video.toActivityPubObject()
+ const byActor = video.VideoChannel.Account.Actor
+ const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
// This is a shared video
- const videoChannel = video.VideoChannel
if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
- const addActivity = await addActivityData(video.url, videoChannel.Account, video, videoChannel.url, videoObject, undefined)
-
- const url = getAnnounceActivityPubUrl(video.url, account)
- const announceActivity = await announceActivityData(url, account, addActivity, undefined)
+ const videoShare = video.VideoShares[0]
+ const announceActivity = announceActivityData(videoShare.url, actor, video.url, createActivityAudience)
activities.push(announceActivity)
} else {
- const addActivity = await addActivityData(video.url, account, video, videoChannel.url, videoObject, undefined)
+ const videoObject = video.toActivityPubObject()
+ const createActivity = createActivityData(video.url, byActor, videoObject, createActivityAudience)
- activities.push(addActivity)
+ activities.push(createActivity)
}
}
- const newResult = {
+ return {
data: activities,
total: data.total
}
- const json = activityPubCollectionPagination(account.url + '/outbox', page, newResult)
-
- return res.json(json).end()
}