Split types and typings
[oweals/peertube.git] / server / controllers / activitypub / inbox.ts
index f0e65015bb9796fa3cc6995460db5e1c6cc79bb4..8b42478ee254a159982fb51640458a18881587b3 100644 (file)
@@ -5,10 +5,8 @@ import { logger } from '../../helpers/logger'
 import { processActivities } from '../../lib/activitypub/process/process'
 import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChannelValidator, signatureValidator } from '../../middlewares'
 import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
-import { VideoChannelModel } from '../../models/video/video-channel'
-import { AccountModel } from '../../models/account/account'
 import { queue } from 'async'
-import { ActorModel } from '../../models/activitypub/actor'
+import { MActorDefault, MActorSignature } from '../../types/models'
 
 const inboxRouter = express.Router()
 
@@ -42,20 +40,25 @@ export {
 
 // ---------------------------------------------------------------------------
 
-const inboxQueue = queue<{ activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel }, Error>((task, cb) => {
+type QueueParam = { activities: Activity[], signatureActor?: MActorSignature, inboxActor?: MActorDefault }
+const inboxQueue = queue<QueueParam, Error>((task, cb) => {
   const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
 
   processActivities(task.activities, options)
     .then(() => cb())
+    .catch(err => {
+      logger.error('Error in process activities.', { err })
+      cb()
+    })
 })
 
 function inboxController (req: express.Request, res: express.Response) {
   const rootActivity: RootActivity = req.body
-  let activities: Activity[] = []
+  let activities: Activity[]
 
-  if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
+  if ([ 'Collection', 'CollectionPage' ].includes(rootActivity.type)) {
     activities = (rootActivity as ActivityPubCollection).items
-  } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
+  } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].includes(rootActivity.type)) {
     activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
   } else {
     activities = [ rootActivity as Activity ]
@@ -66,12 +69,7 @@ function inboxController (req: express.Request, res: express.Response) {
   activities = activities.filter(a => isActivityValid(a))
   logger.debug('We keep %d activities.', activities.length, { activities })
 
-  let accountOrChannel: VideoChannelModel | AccountModel
-  if (res.locals.account) {
-    accountOrChannel = res.locals.account
-  } else if (res.locals.videoChannel) {
-    accountOrChannel = res.locals.videoChannel
-  }
+  const accountOrChannel = res.locals.account || res.locals.videoChannel
 
   logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)