PeerTube/server/controllers/activitypub/outbox.ts

79 lines
3.0 KiB
TypeScript
Raw Normal View History

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'
import { VideoPrivacy } from '../../../shared/models/videos'
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
2018-02-27 11:06:43 +01:00
import { logger } from '../../helpers/logger'
2017-12-14 17:38:41 +01:00
import { announceActivityData, createActivityData } from '../../lib/activitypub/send'
2018-05-25 11:32:36 +02:00
import { buildAudience } from '../../lib/activitypub/audience'
import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
2017-12-12 17:53:50 +01:00
import { AccountModel } from '../../models/account/account'
import { ActorModel } from '../../models/activitypub/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../models/video/video'
import { activityPubResponse } from './utils'
import { VideoChannelModel } from '../../models/video/video-channel'
2017-11-21 18:23:10 +01:00
const outboxRouter = express.Router()
2018-01-03 11:36:03 +01:00
outboxRouter.get('/accounts/:name/outbox',
2017-11-21 18:23:10 +01:00
localAccountValidator,
asyncMiddleware(outboxController)
)
outboxRouter.get('/video-channels/:name/outbox',
localVideoChannelValidator,
asyncMiddleware(outboxController)
)
2017-11-21 18:23:10 +01:00
// ---------------------------------------------------------------------------
export {
outboxRouter
}
// ---------------------------------------------------------------------------
async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
const accountOrVideoChannel: AccountModel | VideoChannelModel = res.locals.account || res.locals.videoChannel
const actor = accountOrVideoChannel.Actor
const actorOutboxUrl = actor.url + '/outbox'
logger.info('Receiving outbox request for %s.', actorOutboxUrl)
2017-11-21 18:23:10 +01:00
const handler = (start: number, count: number) => buildActivities(actor, start, count)
const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
return activityPubResponse(activityPubContextify(json), res)
}
2017-11-21 18:23:10 +01:00
async function buildActivities (actor: ActorModel, start: number, count: number) {
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[] = []
// Avoid too many SQL requests
const actors = data.data.map(v => v.VideoChannel.Account.Actor)
actors.push(actor)
2017-11-21 18:23:10 +01:00
for (const video of data.data) {
const byActor = video.VideoChannel.Account.Actor
2018-05-28 12:13:00 +02:00
const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
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) {
const videoShare = video.VideoShares[0]
const announceActivity = announceActivityData(videoShare.url, actor, video.url, createActivityAudience)
2017-11-22 16:25:03 +01:00
2017-11-21 18:23:10 +01:00
activities.push(announceActivity)
} else {
2018-01-26 12:02:18 +01:00
const videoObject = video.toActivityPubObject()
const createActivity = createActivityData(video.url, byActor, videoObject, createActivityAudience)
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
}
}
return {
2017-11-21 18:23:10 +01:00
data: activities,
total: data.total
}
}