Add action hooks to user routes
[oweals/peertube.git] / server / controllers / activitypub / outbox.ts
1 import * as express from 'express'
2 import { Activity } from '../../../shared/models/activitypub/activity'
3 import { VideoPrivacy } from '../../../shared/models/videos'
4 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
5 import { logger } from '../../helpers/logger'
6 import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send'
7 import { buildAudience } from '../../lib/activitypub/audience'
8 import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares'
9 import { VideoModel } from '../../models/video/video'
10 import { activityPubResponse } from './utils'
11 import { MActorLight } from '@server/typings/models'
12
13 const outboxRouter = express.Router()
14
15 outboxRouter.get('/accounts/:name/outbox',
16   localAccountValidator,
17   asyncMiddleware(outboxController)
18 )
19
20 outboxRouter.get('/video-channels/:name/outbox',
21   localVideoChannelValidator,
22   asyncMiddleware(outboxController)
23 )
24
25 // ---------------------------------------------------------------------------
26
27 export {
28   outboxRouter
29 }
30
31 // ---------------------------------------------------------------------------
32
33 async function outboxController (req: express.Request, res: express.Response) {
34   const accountOrVideoChannel = res.locals.account || res.locals.videoChannel
35   const actor = accountOrVideoChannel.Actor
36   const actorOutboxUrl = actor.url + '/outbox'
37
38   logger.info('Receiving outbox request for %s.', actorOutboxUrl)
39
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 }
45
46 async function buildActivities (actor: MActorLight, start: number, count: number) {
47   const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count)
48   const activities: Activity[] = []
49
50   for (const video of data.data) {
51     const byActor = video.VideoChannel.Account.Actor
52     const createActivityAudience = buildAudience([ byActor.followersUrl ], video.privacy === VideoPrivacy.PUBLIC)
53
54     // This is a shared video
55     if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
56       const videoShare = video.VideoShares[0]
57       const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience)
58
59       activities.push(announceActivity)
60     } else {
61       const videoObject = video.toActivityPubObject()
62       const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience)
63
64       activities.push(createActivity)
65     }
66   }
67
68   return {
69     data: activities,
70     total: data.total
71   }
72 }