From 6d52e869d165a4c3b64cdf6f6d44c08f4d075309 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste Pasquier <contact@jbpasquier.eu>
Date: Mon, 5 Jul 2021 19:15:43 +0200
Subject: [PATCH] feature: don't create federation when there is only one
 target

---
 src/orbit-envoy.pug | 46 +++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)

diff --git a/src/orbit-envoy.pug b/src/orbit-envoy.pug
index a647728c..3c405c5e 100644
--- a/src/orbit-envoy.pug
+++ b/src/orbit-envoy.pug
@@ -113,15 +113,20 @@ for component of components
       for(const [attribute, path] of Object.entries(component.parameters)) {
         if(typeof path === 'string') {
           if(path.startsWith('federation://')) {
-            federations[`store://local.${component.uniq}/${attribute}/`] = {
-              "@cache": "false",
-              "@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
-              "@type": "ldp:Container",
-              "@id": `store://local.${component.uniq}/${attribute}/`,
-              "ldp:contains": generateUrl(federation, path),
-              "permissions": [{"mode": {"@type": "view"}}]
-            };
-            component.parameters[attribute] = `store://local.${component.uniq}/${attribute}/`;
+            let contains = generateUrl(federation, path);
+            if(contains.length > 1) {
+              federations[`store://local.${component.uniq}/${attribute}/`] = {
+                "@cache": "false",
+                "@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
+                "@type": "ldp:Container",
+                "@id": `store://local.${component.uniq}/${attribute}/`,
+                "ldp:contains": contains,
+                "permissions": [{"mode": {"@type": "view"}}]
+              };
+              component.parameters[attribute] = `store://local.${component.uniq}/${attribute}/`;
+            } else {
+              component.parameters[attribute] = federation.values().next().value + path.replace(/federation:\//, '');
+            }
           }
           if(path.startsWith('server://')) {
             component.parameters[attribute] = client.server + path.replace(/server:\//, '');
@@ -163,15 +168,20 @@ for component of components
           for(const [attribute, path] of Object.entries(extension.parameters)) {
             if(typeof path === 'string') {
               if(path.startsWith('federation://')) {
-                federations[`store://local.${extension.uniq}/${attribute}/`] = {
-                  "@cache": "false",
-                  "@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
-                  "@type": "ldp:Container",
-                  "@id": `store://local.${extension.uniq}/${attribute}/`,
-                  "ldp:contains": generateUrl(federation, path),
-                  "permissions": [{"mode": {"@type": "view"}}]
-                };
-                extension.parameters[attribute] = `store://local.${extension.uniq}/${attribute}/`;
+                let contains = generateUrl(federation, path);
+                if(contains.length > 1) {
+                  federations[`store://local.${extension.uniq}/${attribute}/`] = {
+                    "@cache": "false",
+                    "@context": "https://cdn.happy-dev.fr/owl/hdcontext.jsonld",
+                    "@type": "ldp:Container",
+                    "@id": `store://local.${extension.uniq}/${attribute}/`,
+                    "ldp:contains": contains,
+                    "permissions": [{"mode": {"@type": "view"}}]
+                  };
+                  extension.parameters[attribute] = `store://local.${extension.uniq}/${attribute}/`;
+                } else {
+                  component.parameters[attribute] = federation.values().next().value + path.replace(/federation:\//, '');
+                }
               }
               if(path.startsWith('server://')) {
                 extension.parameters[attribute] = client.server + path.replace(/server:\//, '');
-- 
GitLab