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

78 lines
3 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import express from 'express'
import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
import { activityPubContextify } from '@server/lib/activitypub/context'
import { MActorLight } from '@server/types/models'
2017-11-22 12:31:40 -05:00
import { Activity } from '../../../shared/models/activitypub/activity'
import { VideoPrivacy } from '../../../shared/models/videos'
2018-02-27 05:06:43 -05:00
import { logger } from '../../helpers/logger'
2018-05-25 05:32:36 -04:00
import { buildAudience } from '../../lib/activitypub/audience'
import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
import { asyncMiddleware, ensureIsLocalChannel, localAccountValidator, videoChannelsNameWithHostValidator } from '../../middlewares'
import { apPaginationValidator } from '../../middlewares/validators/activitypub'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../models/video/video'
import { activityPubResponse } from './utils'
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',
2020-01-08 18:43:52 -05:00
apPaginationValidator,
2017-11-21 12:23:10 -05:00
localAccountValidator,
asyncMiddleware(outboxController)
)
outboxRouter.get('/video-channels/:nameWithHost/outbox',
2020-01-08 18:43:52 -05:00
apPaginationValidator,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureIsLocalChannel,
asyncMiddleware(outboxController)
)
2017-11-21 12:23:10 -05:00
// ---------------------------------------------------------------------------
export {
outboxRouter
}
// ---------------------------------------------------------------------------
2019-02-26 04:55:40 -05:00
async function outboxController (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const accountOrVideoChannel = 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 12:23:10 -05:00
const handler = (start: number, count: number) => buildActivities(actor, start, count)
2020-01-08 18:43:52 -05:00
const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
2022-03-23 11:14:33 -04:00
return activityPubResponse(activityPubContextify(json, 'Collection'), res)
}
2017-11-21 12:23:10 -05:00
2019-08-15 05:53:26 -04:00
async function buildActivities (actor: MActorLight, 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[] = []
for (const video of data.data) {
const byActor = video.VideoChannel.Account.Actor
2018-05-28 06:13:00 -04:00
const createActivityAudience = buildAudience([ byActor.followersUrl ], 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]
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
}
}
return {
2017-11-21 12:23:10 -05:00
data: activities,
total: data.total
}
}