]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/outbox.ts
Better AP route checker
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / outbox.ts
CommitLineData
e71bcc0f 1import * as express from 'express'
d52eb8f6 2import { Activity } from '../../../shared/models/activitypub/activity'
0bc22f8d 3import { VideoPrivacy } from '../../../shared/models/videos'
8fffe21a 4import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
0a8ae684 5import { logger } from '../../helpers/logger'
c48e82b5 6import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
e251f170 7import { buildAudience } from '../../lib/activitypub/audience'
06a05d5f 8import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
3fd3ab2d 9import { AccountModel } from '../../models/account/account'
54e74059 10import { ActorModel } from '../../models/activitypub/actor'
3fd3ab2d 11import { VideoModel } from '../../models/video/video'
8fffe21a 12import { activityPubResponse } from './utils'
06a05d5f 13import { VideoChannelModel } from '../../models/video/video-channel'
e71bcc0f
C
14
15const outboxRouter = express.Router()
16
e8e12200 17outboxRouter.get('/accounts/:name/outbox',
e71bcc0f
C
18 localAccountValidator,
19 asyncMiddleware(outboxController)
20)
21
06a05d5f
C
22outboxRouter.get('/video-channels/:name/outbox',
23 localVideoChannelValidator,
24 asyncMiddleware(outboxController)
25)
26
e71bcc0f
C
27// ---------------------------------------------------------------------------
28
29export {
30 outboxRouter
31}
32
33// ---------------------------------------------------------------------------
34
418d092a 35async function outboxController (req: express.Request, res: express.Response) {
06a05d5f
C
36 const accountOrVideoChannel: AccountModel | VideoChannelModel = res.locals.account || res.locals.videoChannel
37 const actor = accountOrVideoChannel.Actor
38 const actorOutboxUrl = actor.url + '/outbox'
8fffe21a
C
39
40 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
e71bcc0f 41
8fffe21a
C
42 const handler = (start: number, count: number) => buildActivities(actor, start, count)
43 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
44
45 return activityPubResponse(activityPubContextify(json), res)
46}
e71bcc0f 47
8fffe21a 48async function buildActivities (actor: ActorModel, start: number, count: number) {
50d6de9c 49 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
e71bcc0f
C
50 const activities: Activity[] = []
51
54e74059
C
52 // Avoid too many SQL requests
53 const actors = data.data.map(v => v.VideoChannel.Account.Actor)
54 actors.push(actor)
55
e71bcc0f 56 for (const video of data.data) {
54e74059 57 const byActor = video.VideoChannel.Account.Actor
e3d5ea4f 58 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
54e74059 59
50d6de9c 60 // This is a shared video
40ff5707 61 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
4ba3b8ea 62 const videoShare = video.VideoShares[0]
c48e82b5 63 const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
40ff5707 64
e71bcc0f
C
65 activities.push(announceActivity)
66 } else {
7acee6f1 67 const videoObject = video.toActivityPubObject()
c48e82b5 68 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
40ff5707 69
50d6de9c 70 activities.push(createActivity)
e71bcc0f
C
71 }
72 }
73
8fffe21a 74 return {
e71bcc0f
C
75 data: activities,
76 total: data.total
77 }
e71bcc0f 78}