From 0cbe816860e3e333b56fe08933b85482a08e30f0 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Thu, 18 Aug 2022 12:32:19 +0200
Subject: [PATCH] feature: enhance `import-lookup` command to forward
 memberships

---
 app/modules/bma/lib/dtos.ts  |  5 ++++
 app/modules/crawler/index.ts | 58 ++++++++++++++++++++++++++++++------
 2 files changed, 54 insertions(+), 9 deletions(-)

diff --git a/app/modules/bma/lib/dtos.ts b/app/modules/bma/lib/dtos.ts
index 1ba54fe4a..dcf1ebe25 100644
--- a/app/modules/bma/lib/dtos.ts
+++ b/app/modules/bma/lib/dtos.ts
@@ -629,6 +629,11 @@ export const RequirementsPendingMembership = {
   type: String,
   blockstamp: String,
   sig: String,
+  signature: String,
+  issuer: String,
+  membership: String,
+  userid: String,
+  certts: String,
 };
 
 export interface HttpRequirementsPendingMembership {
diff --git a/app/modules/crawler/index.ts b/app/modules/crawler/index.ts
index 77a3f2d7b..bb3d86990 100644
--- a/app/modules/crawler/index.ts
+++ b/app/modules/crawler/index.ts
@@ -560,7 +560,8 @@ export const CrawlerDependency = {
         },
       },
       {
-        name: "import-lookup [search] [fromhost] [fromport] [tohost] [toport]",
+        name:
+          "import-lookup [search] [fromhost] [fromport] [frompath] [tohost] [toport] [topath]",
         desc: "Exchange peerings with another node",
         onDatabaseExecute: async (
           server: Server,
@@ -571,18 +572,31 @@ export const CrawlerDependency = {
           const search = params[0];
           const fromhost = params[1];
           const fromport = params[2];
-          const tohost = params[3];
-          const toport = params[4];
+          const frompath = params[3];
+          const tohost = params[4];
+          const toport = params[5];
+          const topath = params[6];
           const logger = server.logger;
           try {
             logger.info(
-              'Looking for "%s" at %s:%s...',
+              'Looking for "%s" at %s:%s%s...',
               search,
               fromhost,
-              fromport
+              fromport,
+              frompath
+            );
+            const sourcePeer = Contacter.fromHostPortPath(
+              fromhost,
+              fromport,
+              frompath,
+              { timeout: 60 * 1000 }
+            );
+            const targetPeer = Contacter.fromHostPortPath(
+              tohost,
+              toport,
+              topath,
+              { timeout: 60 * 1000 }
             );
-            const sourcePeer = new Contacter(fromhost, fromport);
-            const targetPeer = new Contacter(tohost, toport);
             const lookup = await sourcePeer.getLookup(search);
             for (const res of lookup.results) {
               for (const uid of res.uids) {
@@ -626,7 +640,12 @@ export const CrawlerDependency = {
                 }
               }
             }
-            const certBy = await sourcePeer.getCertifiedBy(search);
+            let certBy: any = { certifications: [] };
+            try {
+              certBy = await sourcePeer.getCertifiedBy(search);
+            } catch (e) {
+              logger.error("No certified-by on remote");
+            }
             const mapBlocks: any = {};
             for (const signed of certBy.certifications) {
               if (signed.written) {
@@ -666,7 +685,7 @@ export const CrawlerDependency = {
                 });
                 try {
                   logger.info(
-                    "Success cert %s -> %s",
+                    "Posting cert %s -> %s",
                     certBy.pubkey.slice(0, 8),
                     signed.uid
                   );
@@ -676,6 +695,27 @@ export const CrawlerDependency = {
                 }
               }
             }
+            // Memberships
+            const requirements = await sourcePeer.getRequirements(search);
+            for (let idty of requirements.identities) {
+              for (let pendingMs of idty.pendingMemberships) {
+                const rawMs = rawer.getMembership({
+                  currency: "g1",
+                  issuer: pendingMs.issuer,
+                  type: pendingMs.membership,
+                  blockstamp: pendingMs.blockstamp,
+                  userid: pendingMs.userid,
+                  certts: pendingMs.certts,
+                  signature: pendingMs.signature,
+                });
+                try {
+                  logger.info("Posting membership");
+                  await targetPeer.postRenew(rawMs);
+                } catch (e) {
+                  logger.error(e);
+                }
+              }
+            }
             logger.info("Sent.");
             await server.disconnect();
           } catch (e) {
-- 
GitLab