]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/activitypub/inbox.ts
Translated using Weblate (Gaelic)
[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 { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
4 import { logger } from '../../helpers/logger'
5 import { processActivities } from '../../lib/activitypub/process/process'
6 import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChannelValidator, signatureValidator } from '../../middlewares'
7 import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
8 import { queue } from 'async'
9 import { MActorDefault, MActorSignature } from '../../typings/models'
10
11 const inboxRouter = express.Router()
12
13 inboxRouter.post('/inbox',
14 signatureValidator,
15 asyncMiddleware(checkSignature),
16 asyncMiddleware(activityPubValidator),
17 inboxController
18 )
19
20 inboxRouter.post('/accounts/:name/inbox',
21 signatureValidator,
22 asyncMiddleware(checkSignature),
23 asyncMiddleware(localAccountValidator),
24 asyncMiddleware(activityPubValidator),
25 inboxController
26 )
27 inboxRouter.post('/video-channels/:name/inbox',
28 signatureValidator,
29 asyncMiddleware(checkSignature),
30 asyncMiddleware(localVideoChannelValidator),
31 asyncMiddleware(activityPubValidator),
32 inboxController
33 )
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 inboxRouter
39 }
40
41 // ---------------------------------------------------------------------------
42
43 type QueueParam = { activities: Activity[], signatureActor?: MActorSignature, inboxActor?: MActorDefault }
44 const inboxQueue = queue<QueueParam, Error>((task, cb) => {
45 const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
46
47 processActivities(task.activities, options)
48 .then(() => cb())
49 })
50
51 function inboxController (req: express.Request, res: express.Response) {
52 const rootActivity: RootActivity = req.body
53 let activities: Activity[] = []
54
55 if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
56 activities = (rootActivity as ActivityPubCollection).items
57 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
58 activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
59 } else {
60 activities = [ rootActivity as Activity ]
61 }
62
63 // Only keep activities we are able to process
64 logger.debug('Filtering %d activities...', activities.length)
65 activities = activities.filter(a => isActivityValid(a))
66 logger.debug('We keep %d activities.', activities.length, { activities })
67
68 const accountOrChannel = res.locals.account || res.locals.videoChannel
69
70 logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
71
72 inboxQueue.push({
73 activities,
74 signatureActor: res.locals.signature.actor,
75 inboxActor: accountOrChannel ? accountOrChannel.Actor : undefined
76 })
77
78 return res.status(204).end()
79 }