From cab9f81e71f0ccf6c112a5267e9e1db908f5db64 Mon Sep 17 00:00:00 2001
From: Benoit Lavenier <benoit.lavenier@e-is.pro>
Date: Mon, 15 May 2023 18:28:19 +0200
Subject: [PATCH] [enh] Optimize BMA `/wot/certifiers-of/:search` and
 `/wot/certified-by/:search` - Close #1440

---
 app/lib/dal/fileDAL.ts                     | 11 ++++++
 app/modules/bma/lib/controllers/wot.ts     | 28 +++++++++++----
 test/integration/identity/identity-test.ts | 42 ++++++++++++++++++++++
 3 files changed, 75 insertions(+), 6 deletions(-)

diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts
index 599d48ed6..9cf47b5a8 100644
--- a/app/lib/dal/fileDAL.ts
+++ b/app/lib/dal/fileDAL.ts
@@ -743,6 +743,17 @@ export class FileDAL implements ServerDAO {
     return await this.iindexDAL.getFromPubkeyOrUid(search);
   }
 
+  async getWrittenIdtyByPubkeyForHashingAndIsMember(
+      pub: string
+  ): Promise<{
+    uid: string;
+    created_on: string;
+    pub: string;
+    member: boolean;
+  } | null> {
+    return await this.iindexDAL.getFromPubkey(pub);
+  }
+
   async getWrittenIdtyByPubkeyForRevocationCheck(
     pubkey: string
   ): Promise<{
diff --git a/app/modules/bma/lib/controllers/wot.ts b/app/modules/bma/lib/controllers/wot.ts
index 61e869b06..9651cb9f2 100644
--- a/app/modules/bma/lib/controllers/wot.ts
+++ b/app/modules/bma/lib/controllers/wot.ts
@@ -132,9 +132,17 @@ export class WOTBinding extends AbstractController {
 
   async certifiersOf(req: any): Promise<HttpCertifications> {
     const search = await ParametersService.getSearchP(req);
-    const idty = (await this.server.dal.getWrittenIdtyByPubkeyOrUIdForHashingAndIsMember(
-      search
-    )) as FullIindexEntry;
+    let idty: FullIindexEntry;
+    if (req.query.pubkey) {
+      idty = (await this.server.dal.getWrittenIdtyByPubkeyForHashingAndIsMember(
+          search
+      )) as FullIindexEntry;
+    }
+    else {
+      idty = (await this.server.dal.getWrittenIdtyByPubkeyOrUIdForHashingAndIsMember(
+          search
+      )) as FullIindexEntry;
+    }
     const certs = await this.server.dal.certsToTarget(
       idty.pub,
       IdentityDTO.getTargetHash(idty)
@@ -229,9 +237,17 @@ export class WOTBinding extends AbstractController {
 
   async certifiedBy(req: any): Promise<HttpCertifications> {
     const search = await ParametersService.getSearchP(req);
-    const idty = (await this.server.dal.getWrittenIdtyByPubkeyOrUIdForHashingAndIsMember(
-      search
-    )) as FullIindexEntry;
+    let idty: FullIindexEntry;
+    if (req.query.pubkey) {
+      idty = (await this.server.dal.getWrittenIdtyByPubkeyForHashingAndIsMember(
+          search
+      )) as FullIindexEntry;
+    }
+    else {
+      idty = (await this.server.dal.getWrittenIdtyByPubkeyOrUIdForHashingAndIsMember(
+        search
+      )) as FullIindexEntry;
+    }
     const certs = await this.server.dal.certsFrom(idty.pub);
     const theCerts: HttpCertification[] = [];
     for (const cert of certs) {
diff --git a/test/integration/identity/identity-test.ts b/test/integration/identity/identity-test.ts
index 6966e9971..847ff19e8 100644
--- a/test/integration/identity/identity-test.ts
+++ b/test/integration/identity/identity-test.ts
@@ -192,6 +192,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certifiers-of/:pubkey of cat giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd');
+      res.should.have.property('uid').equal('cat');
+    });
+  });
+
   it('should have certifiers-of/tic giving results', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/tic', { json: true }), function(res:HttpCertifications) {
       res.should.have.property('pubkey').equal('DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV');
@@ -213,6 +220,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certifiers-of/:pubkey of tic giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV');
+      res.should.have.property('uid').equal('tic');
+    });
+  });
+
   it('should have certifiers-of/toc giving results', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/toc', { json: true }), function(res:HttpCertifications) {
       res.should.have.property('pubkey').equal('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo');
@@ -234,6 +248,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certifiers-of/:pubkey of toc giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo');
+      res.should.have.property('uid').equal('toc');
+    });
+  });
+
   it('requirements of cat', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/requirements/cat', { json: true }), function(res:HttpRequirements) {
       res.should.have.property('identities').be.an.Array;
@@ -298,6 +319,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certified-by/:pubkey of tic giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certified-by/DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV');
+      res.should.have.property('uid').equal('tic');
+    });
+  });
+
   it('should have certified-by/tac giving results', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/certified-by/tac', { json: true }), function(res:HttpCertifications) {
       res.should.have.property('pubkey').equal('2LvDg21dVXvetTD9GdkPLURavLYEqP3whauvPWX4c2qc');
@@ -308,6 +336,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certified-by/:pubkey of tac giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certified-by/2LvDg21dVXvetTD9GdkPLURavLYEqP3whauvPWX4c2qc?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('2LvDg21dVXvetTD9GdkPLURavLYEqP3whauvPWX4c2qc');
+      res.should.have.property('uid').equal('tac');
+    });
+  });
+
   it('should have certified-by/cat giving results', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/certified-by/cat', { json: true }), function(res:HttpCertifications) {
       res.should.have.property('pubkey').equal('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd');
@@ -343,6 +378,13 @@ describe("Identities collision", function() {
     });
   });
 
+  it('should have certified-by/:pubkey of cat giving results', function() {
+    return expectAnswer(rp('http://127.0.0.1:7799/wot/certified-by/HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd?pubkey', { json: true }), function(res:HttpCertifications) {
+      res.should.have.property('pubkey').equal('HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd');
+      res.should.have.property('uid').equal('cat');
+    });
+  });
+
   it('requirements of man2', function() {
     return expectAnswer(rp('http://127.0.0.1:7799/wot/requirements/man2', { json: true }), function(res:HttpRequirements) {
       res.should.have.property('identities').be.an.Array;
-- 
GitLab