From 1d53e7d8a1fe6ab899b6dfb78ede3f1f49a208b7 Mon Sep 17 00:00:00 2001
From: yflory <yann.flory@xwiki.com>
Date: Thu, 5 Sep 2019 19:03:42 +0200
Subject: [PATCH] Teams: listAllTags and getTemplates

---
 www/common/outer/async-store.js | 29 ++++++++++++++++++++++++-----
 www/common/outer/team.js        |  4 +++-
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js
index a9947f3b2..7d04d23f4 100644
--- a/www/common/outer/async-store.js
+++ b/www/common/outer/async-store.js
@@ -407,6 +407,9 @@ define([
             if (!store.loggedIn) { return cb(); }
             if (store.rpc) { return void cb(account); }
             require(['/common/pinpad.js'], function (Pinpad) {
+                // XXX Teams: we wont' pass the team's proxy directly here because all the users
+                // may not have access to the edPrivate key
+                // Users without edPrivate should not be able to create a pinpad object
                 Pinpad.create(store.network, store.proxy, function (e, call) {
                     if (e) { return void cb({error: e}); }
 
@@ -846,17 +849,33 @@ define([
 
         // Tags
         Store.listAllTags = function (clientId, data, cb) {
-            cb(store.manager.getTagsList());
+            var tags = {};
+            getAllStores.forEach(function (s) {
+                var l = s.manager.getTagsList();
+                Object.keys(l).forEach(function (tag) {
+                    tags[tag] = (tags[tag] || 0) + l[tag];
+                });
+            });
+            cb(tags);
         };
 
         // Templates
+        // Note: maybe we should get templates "per team" to avoid creating a document with a template
+        // from a different team
         Store.getTemplates = function (clientId, data, cb) {
             // No templates in shared folders: we don't need the manager here
-            var templateFiles = store.userObject.getFiles(['template']);
             var res = [];
-            templateFiles.forEach(function (f) {
-                var data = store.userObject.getFileData(f);
-                res.push(JSON.parse(JSON.stringify(data)));
+            var channels = [];
+            getAllStores.forEach(function (s) {
+                var templateFiles = s.userObject.getFiles(['template']);
+                templateFiles.forEach(function (f) {
+                    var data = s.userObject.getFileData(f);
+                    // Don't push duplicates
+                    if (channels.indexOf(data.channel) !== -1) { return }
+                    channels.push(data.channel);
+                    // Puhs a copy of the data
+                    res.push(JSON.parse(JSON.stringify(data)));
+                });
             });
             cb(res);
         };
diff --git a/www/common/outer/team.js b/www/common/outer/team.js
index 9adcf99f9..9ce0e570f 100644
--- a/www/common/outer/team.js
+++ b/www/common/outer/team.js
@@ -33,7 +33,8 @@ define([
         // sanity check: do we have all the required keys?
         // initialize team rpc with pin, unpin, ...
         // team.rpc = rpc
-        // load manager
+        // load manager with userObject
+        //   team.manager =... team.userObject = ....
         // load shared folders
         // ~resetPins for the team?
         // getPinLimit
@@ -73,6 +74,7 @@ define([
                 listmap: lm,
                 clients: [],
                 manager: undefined, // XXX
+                userObject: undefined, // XXX
                 realtime: lm.realtime,
                 handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); },
                 sharedFolders: {}, // equivalent of store.sharedFolders in async-store