]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/activitypub/outbox.ts
Translated using Weblate (Gaelic)
[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 { VideoModel } from '../../models/video/video'
8fffe21a 10import { activityPubResponse } from './utils'
453e83ea 11import { MActorLight } from '@server/typings/models'
e71bcc0f
C
12
13const outboxRouter = express.Router()
14
e8e12200 15outboxRouter.get('/accounts/:name/outbox',
e71bcc0f
C
16 localAccountValidator,
17 asyncMiddleware(outboxController)
18)
19
06a05d5f
C
20outboxRouter.get('/video-channels/:name/outbox',
21 localVideoChannelValidator,
22 asyncMiddleware(outboxController)
23)
24
e71bcc0f
C
25// ---------------------------------------------------------------------------
26
27export {
28 outboxRouter
29}
30
31// ---------------------------------------------------------------------------
32
418d092a 33async function outboxController (req: express.Request, res: express.Response) {
dae86118 34 const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
06a05d5f
C
35 const actor = accountOrVideoChannel.Actor
36 const actorOutboxUrl = actor.url + '/outbox'
8fffe21a
C
37
38 logger.info('Receiving outbox request for %s.', actorOutboxUrl)
e71bcc0f 39
8fffe21a
C
40 const handler = (start: number, count: number) => buildActivities(actor, start, count)
41 const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page)
42
43 return activityPubResponse(activityPubContextify(json), res)
44}
e71bcc0f 45
453e83ea 46async function buildActivities (actor: MActorLight, start: number, count: number) {
50d6de9c 47 const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
e71bcc0f
C
48 const activities: Activity[] = []
49
e71bcc0f 50 for (const video of data.data) {
54e74059 51 const byActor = video.VideoChannel.Account.Actor
e3d5ea4f 52 const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
54e74059 53
50d6de9c 54 // This is a shared video
40ff5707 55 if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
4ba3b8ea 56 const videoShare = video.VideoShares[0]
c48e82b5 57 const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
40ff5707 58
e71bcc0f
C
59 activities.push(announceActivity)
60 } else {
7acee6f1 61 const videoObject = video.toActivityPubObject()
c48e82b5 62 const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
40ff5707 63
50d6de9c 64 activities.push(createActivity)
e71bcc0f
C
65 }
66 }
67
8fffe21a 68 return {
e71bcc0f
C
69 data: activities,
70 total: data.total
71 }
e71bcc0f 72}