2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2022-03-23 08:24:50 -05:00
|
|
|
import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
|
|
|
|
import { activityPubContextify } from '@server/lib/activitypub/context'
|
2021-12-13 08:29:13 -06:00
|
|
|
import { MActorLight } from '@server/types/models'
|
2017-11-22 11:31:40 -06:00
|
|
|
import { Activity } from '../../../shared/models/activitypub/activity'
|
2018-02-04 16:17:01 -06:00
|
|
|
import { VideoPrivacy } from '../../../shared/models/videos'
|
2018-02-27 04:06:43 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-05-25 04:32:36 -05:00
|
|
|
import { buildAudience } from '../../lib/activitypub/audience'
|
2021-12-13 08:29:13 -06:00
|
|
|
import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
|
|
|
|
import { asyncMiddleware, ensureIsLocalChannel, localAccountValidator, videoChannelsNameWithHostValidator } from '../../middlewares'
|
|
|
|
import { apPaginationValidator } from '../../middlewares/validators/activitypub'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
2018-05-25 09:21:16 -05:00
|
|
|
import { activityPubResponse } from './utils'
|
2017-11-21 11:23:10 -06:00
|
|
|
|
|
|
|
const outboxRouter = express.Router()
|
|
|
|
|
2018-01-03 04:36:03 -06:00
|
|
|
outboxRouter.get('/accounts/:name/outbox',
|
2020-01-08 17:43:52 -06:00
|
|
|
apPaginationValidator,
|
2017-11-21 11:23:10 -06:00
|
|
|
localAccountValidator,
|
|
|
|
asyncMiddleware(outboxController)
|
|
|
|
)
|
|
|
|
|
2021-12-13 08:29:13 -06:00
|
|
|
outboxRouter.get('/video-channels/:nameWithHost/outbox',
|
2020-01-08 17:43:52 -06:00
|
|
|
apPaginationValidator,
|
2021-12-13 08:29:13 -06:00
|
|
|
asyncMiddleware(videoChannelsNameWithHostValidator),
|
|
|
|
ensureIsLocalChannel,
|
2018-08-16 08:25:20 -05:00
|
|
|
asyncMiddleware(outboxController)
|
|
|
|
)
|
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
outboxRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
async function outboxController (req: express.Request, res: express.Response) {
|
2019-03-19 04:35:15 -05:00
|
|
|
const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
|
2018-08-16 08:25:20 -05:00
|
|
|
const actor = accountOrVideoChannel.Actor
|
|
|
|
const actorOutboxUrl = actor.url + '/outbox'
|
2018-05-25 09:21:16 -05:00
|
|
|
|
|
|
|
logger.info('Receiving outbox request for %s.', actorOutboxUrl)
|
2017-11-21 11:23:10 -06:00
|
|
|
|
2018-05-25 09:21:16 -05:00
|
|
|
const handler = (start: number, count: number) => buildActivities(actor, start, count)
|
2020-01-08 17:43:52 -06:00
|
|
|
const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page, req.query.size)
|
2018-05-25 09:21:16 -05:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return activityPubResponse(activityPubContextify(json, 'Collection'), res)
|
2018-05-25 09:21:16 -05:00
|
|
|
}
|
2017-11-21 11:23:10 -06:00
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function buildActivities (actor: MActorLight, start: number, count: number) {
|
2017-12-14 10:38:41 -06:00
|
|
|
const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
|
2017-11-21 11:23:10 -06:00
|
|
|
const activities: Activity[] = []
|
|
|
|
|
|
|
|
for (const video of data.data) {
|
2018-01-18 07:59:27 -06:00
|
|
|
const byActor = video.VideoChannel.Account.Actor
|
2018-05-28 05:13:00 -05:00
|
|
|
const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
|
2018-01-18 07:59:27 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
// This is a shared video
|
2017-11-22 09:25:03 -06:00
|
|
|
if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
|
2018-01-26 08:49:57 -06:00
|
|
|
const videoShare = video.VideoShares[0]
|
2018-09-11 09:27:07 -05:00
|
|
|
const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-11-21 11:23:10 -06:00
|
|
|
activities.push(announceActivity)
|
|
|
|
} else {
|
2023-06-05 08:51:16 -05:00
|
|
|
// FIXME: only use the video URL to reduce load. Breaks compat with PeerTube < 6.0.0
|
2023-03-10 05:01:21 -06:00
|
|
|
const videoObject = await video.toActivityPubObject()
|
2018-09-11 09:27:07 -05:00
|
|
|
const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
activities.push(createActivity)
|
2017-11-21 11:23:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 09:21:16 -05:00
|
|
|
return {
|
2017-11-21 11:23:10 -06:00
|
|
|
data: activities,
|
|
|
|
total: data.total
|
|
|
|
}
|
|
|
|
}
|