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