PeerTube/server/core/controllers/activitypub/outbox.ts

87 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import express from 'express'
import { Activity, VideoPrivacy } from '@peertube/peertube-models'
import { activityPubContextify } from '@server/helpers/activity-pub-utils.js'
import { activityPubCollectionPagination } from '@server/lib/activitypub/collection.js'
import { getContextFilter } from '@server/lib/activitypub/context.js'
import { MActorLight } from '@server/types/models/index.js'
import { logger } from '../../helpers/logger.js'
import { buildAudience } from '../../lib/activitypub/audience.js'
import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send/index.js'
2023-07-25 15:18:10 +02:00
import {
activityPubRateLimiter,
asyncMiddleware,
ensureIsLocalChannel,
localAccountValidator,
videoChannelsNameWithHostValidator
} from '../../middlewares/index.js'
import { apPaginationValidator } from '../../middlewares/validators/activitypub/index.js'
import { VideoModel } from '../../models/video/video.js'
import { activityPubResponse } from './utils.js'
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',
2023-07-25 15:18:10 +02:00
activityPubRateLimiter,
2020-01-09 00:43:52 +01:00
apPaginationValidator,
2017-11-21 18:23:10 +01:00
localAccountValidator,
asyncMiddleware(outboxController)
)
outboxRouter.get('/video-channels/:nameWithHost/outbox',
2023-07-25 15:18:10 +02:00
activityPubRateLimiter,
2020-01-09 00:43:52 +01:00
apPaginationValidator,
asyncMiddleware(videoChannelsNameWithHostValidator),
ensureIsLocalChannel,
asyncMiddleware(outboxController)
)
2017-11-21 18:23:10 +01:00
// ---------------------------------------------------------------------------
export {
outboxRouter
}
// ---------------------------------------------------------------------------
2019-02-26 10:55:40 +01:00
async function outboxController (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01: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 18:23:10 +01:00
const handler = (start: number, count: number) => buildActivities(actor, start, count)
2020-01-09 00:43:52 +01:00
const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
}
2017-11-21 18:23:10 +01:00
2019-08-15 11:53:26 +02:00
async function buildActivities (actor: MActorLight, 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[] = []
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]
2018-09-11 16:27:07 +02:00
const announceActivity = buildAnnounceActivity(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 {
// FIXME: only use the video URL to reduce load. Breaks compat with PeerTube < 6.0.0
const videoObject = await video.toActivityPubObject()
2018-09-11 16:27:07 +02:00
const createActivity = buildCreateActivity(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
}
}