]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/inbox.ts
Redirect to uuid video route after upload
[github/Chocobozzz/PeerTube.git] / server / controllers / activitypub / inbox.ts
1 import * as express from 'express'
2 import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared'
3 import { logger } from '../../helpers'
4 import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
5 import { processActivities } from '../../lib/activitypub/process/process'
6 import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares'
7 import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
8 import { ActorModel } from '../../models/activitypub/actor'
9
10 const inboxRouter = express.Router()
11
12 inboxRouter.post('/inbox',
13 signatureValidator,
14 asyncMiddleware(checkSignature),
15 activityPubValidator,
16 asyncMiddleware(inboxController)
17 )
18
19 inboxRouter.post('/account/:name/inbox',
20 signatureValidator,
21 asyncMiddleware(checkSignature),
22 localAccountValidator,
23 activityPubValidator,
24 asyncMiddleware(inboxController)
25 )
26
27 // ---------------------------------------------------------------------------
28
29 export {
30 inboxRouter
31 }
32
33 // ---------------------------------------------------------------------------
34
35 async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
36 const rootActivity: RootActivity = req.body
37 let activities: Activity[] = []
38
39 if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
40 activities = (rootActivity as ActivityPubCollection).items
41 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
42 activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
43 } else {
44 activities = [ rootActivity as Activity ]
45 }
46
47 // Only keep activities we are able to process
48 logger.debug('Filtering %d activities...', activities.length)
49 activities = activities.filter(a => isActivityValid(a))
50 logger.debug('We keep %d activities.', activities.length, { activities })
51
52 let specificActor: ActorModel = undefined
53 if (res.locals.account) {
54 specificActor = res.locals.account
55 } else if (res.locals.videoChannel) {
56 specificActor = res.locals.videoChannel
57 }
58
59 await processActivities(activities, res.locals.signature.actor, specificActor)
60
61 res.status(204).end()
62 }