1
0
Fork 0
peertube/server/controllers/activitypub/outbox.ts

72 lines
2.8 KiB
TypeScript
Raw Normal View History

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'
import { VideoPrivacy } from '../../../shared/models/videos'
2017-11-22 10:25:03 -05:00
import { activityPubCollectionPagination } from '../../helpers/activitypub'
import { pageToStartAndCount } from '../../helpers/core-utils'
import { ACTIVITY_PUB } from '../../initializers/constants'
2017-12-14 11:38:41 -05:00
import { announceActivityData, createActivityData } from '../../lib/activitypub/send'
import { buildAudience } from '../../lib/activitypub/send/misc'
2017-11-21 12:23:10 -05:00
import { asyncMiddleware, localAccountValidator } from '../../middlewares'
2017-12-12 11:53:50 -05:00
import { AccountModel } from '../../models/account/account'
import { ActorModel } from '../../models/activitypub/actor'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../models/video/video'
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)
)
// ---------------------------------------------------------------------------
export {
outboxRouter
}
// ---------------------------------------------------------------------------
async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 11:53:50 -05:00
const account: AccountModel = res.locals.account
2017-12-14 11:38:41 -05:00
const actor = account.Actor
2017-11-21 12:23:10 -05:00
const page = req.query.page || 1
2017-11-21 12:23:10 -05:00
const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE)
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[] = []
// Avoid too many SQL requests
const actors = data.data.map(v => v.VideoChannel.Account.Actor)
actors.push(actor)
const followersMatrix = await ActorModel.getActorsFollowerSharedInboxUrls(actors, undefined)
2017-11-21 12:23:10 -05:00
for (const video of data.data) {
const byActor = video.VideoChannel.Account.Actor
const createActivityAudience = buildAudience(followersMatrix[byActor.id], video.privacy === VideoPrivacy.PUBLIC)
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) {
const videoShare = video.VideoShares[0]
const announceAudience = buildAudience(followersMatrix[actor.id], video.privacy === VideoPrivacy.PUBLIC)
const announceActivity = await announceActivityData(videoShare.url, actor, video.url, undefined, announceAudience)
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()
const createActivity = await createActivityData(video.url, byActor, videoObject, undefined, 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
}
}
const newResult = {
data: activities,
total: data.total
}
2017-12-14 11:38:41 -05:00
const json = activityPubCollectionPagination(account.Actor.url + '/outbox', page, newResult)
2017-11-21 12:23:10 -05:00
return res.json(json).end()
}