]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/activitypub.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / middlewares / activitypub.ts
index bed2bfeabe444e659ecbe514d8c8b3c8056d4133..061b2dddc432d4ec156f47b04be688832e29a3a8 100644 (file)
@@ -1,9 +1,10 @@
-import { NextFunction, Request, Response, RequestHandler } from 'express'
+import { eachSeries } from 'async'
+import { NextFunction, Request, RequestHandler, Response } from 'express'
 import { ActivityPubSignature } from '../../shared'
 import { isSignatureVerified, logger } from '../helpers'
-import { fetchRemoteAccountAndCreatePod } from '../helpers/activitypub'
-import { database as db, ACTIVITY_PUB_ACCEPT_HEADER } from '../initializers'
-import { each, eachSeries, waterfall } from 'async'
+import { database as db } from '../initializers'
+import { ACTIVITY_PUB } from '../initializers/constants'
+import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub/account'
 
 async function checkSignature (req: Request, res: Response, next: NextFunction) {
   const signatureObject: ActivityPubSignature = req.body.signature
@@ -14,15 +15,14 @@ async function checkSignature (req: Request, res: Response, next: NextFunction)
 
   // We don't have this account in our database, fetch it on remote
   if (!account) {
-    const accountResult = await fetchRemoteAccountAndCreatePod(signatureObject.creator)
+    account = await fetchRemoteAccount(signatureObject.creator)
 
-    if (!accountResult) {
+    if (!account) {
       return res.sendStatus(403)
     }
 
-    // Save our new account in database
-    account = accountResult.account
-    await account.save()
+    // Save our new account and its server in database
+    await saveAccountAndServerIfNotExist(account)
   }
 
   const verified = await isSignatureVerified(account, req.body)
@@ -37,7 +37,7 @@ async function checkSignature (req: Request, res: Response, next: NextFunction)
 
 function executeIfActivityPub (fun: RequestHandler | RequestHandler[]) {
   return (req: Request, res: Response, next: NextFunction) => {
-    if (req.header('Accept') !== ACTIVITY_PUB_ACCEPT_HEADER) {
+    if (req.header('Accept') !== ACTIVITY_PUB.ACCEPT_HEADER) {
       return next()
     }