inbox.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 { ActorModel } from '../../models/activitypub/actor'
  10. const inboxRouter = express.Router()
  11. inboxRouter.post('/inbox',
  12. signatureValidator,
  13. asyncMiddleware(checkSignature),
  14. asyncMiddleware(activityPubValidator),
  15. inboxController
  16. )
  17. inboxRouter.post('/accounts/:name/inbox',
  18. signatureValidator,
  19. asyncMiddleware(checkSignature),
  20. asyncMiddleware(localAccountValidator),
  21. asyncMiddleware(activityPubValidator),
  22. inboxController
  23. )
  24. inboxRouter.post('/video-channels/:name/inbox',
  25. signatureValidator,
  26. asyncMiddleware(checkSignature),
  27. asyncMiddleware(localVideoChannelValidator),
  28. asyncMiddleware(activityPubValidator),
  29. inboxController
  30. )
  31. // ---------------------------------------------------------------------------
  32. export {
  33. inboxRouter
  34. }
  35. // ---------------------------------------------------------------------------
  36. const inboxQueue = queue<{ activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel }, Error>((task, cb) => {
  37. const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
  38. processActivities(task.activities, options)
  39. .then(() => cb())
  40. })
  41. function inboxController (req: express.Request, res: express.Response) {
  42. const rootActivity: RootActivity = req.body
  43. let activities: Activity[] = []
  44. if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
  45. activities = (rootActivity as ActivityPubCollection).items
  46. } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
  47. activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
  48. } else {
  49. activities = [ rootActivity as Activity ]
  50. }
  51. // Only keep activities we are able to process
  52. logger.debug('Filtering %d activities...', activities.length)
  53. activities = activities.filter(a => isActivityValid(a))
  54. logger.debug('We keep %d activities.', activities.length, { activities })
  55. const accountOrChannel = res.locals.account || res.locals.videoChannel
  56. logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
  57. inboxQueue.push({
  58. activities,
  59. signatureActor: res.locals.signature.actor,
  60. inboxActor: accountOrChannel ? accountOrChannel.Actor : undefined
  61. })
  62. return res.status(204).end()
  63. }