From 321c7d5512c2f7fc5648d03ebc08887856756b95 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 10 Jul 2018 18:23:16 +0200 Subject: [PATCH] Move pin/unpin outside of userObject --- www/common/mergeDrive.js | 137 ++++++-------------------------- www/common/outer/async-store.js | 22 +++-- www/common/outer/userObject.js | 47 ++++------- www/common/proxy-manager.js | 46 ++++++++--- www/common/userObject.js | 10 +-- 5 files changed, 93 insertions(+), 169 deletions(-) diff --git a/www/common/mergeDrive.js b/www/common/mergeDrive.js index 0ddf72bb8..59f4363b3 100644 --- a/www/common/mergeDrive.js +++ b/www/common/mergeDrive.js @@ -6,84 +6,6 @@ define([ ], function (Crypt, FO, Hash, Realtime) { var exp = {}; - var getType = function (el) { - if (el === null) { return "null"; } - return Array.isArray(el) ? "array" : typeof(el); - }; - - var findAvailableKey = function (obj, key) { - if (typeof (obj[key]) === "undefined") { return key; } - var i = 1; - var nkey = key; - while (typeof (obj[nkey]) !== "undefined") { - nkey = key + '_' + i; - i++; - } - return nkey; - }; - - var createFromPath = function (proxy, oldFo, path, id) { - var root = proxy.drive; - - if (!oldFo.isFile(id)) { return; } - - var error = function (msg) { - console.error(msg || "Unable to find that path", path); - }; - - if (oldFo.isInTrashRoot(path)) { - id = oldFo.find(path.slice(0,3)); - path.pop(); - } - - var next, nextRoot; - path.forEach(function (p, i) { - if (!root) { return; } - if (typeof(p) === "string") { - if (getType(root) !== "object") { root = undefined; error(); return; } - if (i === path.length - 1) { - root[Hash.createChannelId()] = id; - return; - } - next = getType(path[i+1]); - nextRoot = getType(root[p]); - if (nextRoot !== "undefined") { - if (next === "string" && nextRoot === "object" || next === "number" && nextRoot === "array") { - root = root[p]; - return; - } - p = findAvailableKey(root, p); - } - if (next === "number") { - root[p] = []; - root = root[p]; - return; - } - root[p] = {}; - root = root[p]; - return; - } - // Path contains a non-string element: it's an array index - if (typeof(p) !== "number") { root = undefined; error(); return; } - if (getType(root) !== "array") { root = undefined; error(); return; } - if (i === path.length - 1) { - if (root.indexOf(id) === -1) { root.push(id); } - return; - } - next = getType(path[i+1]); - if (next === "number") { - error('2 consecutives arrays in the user object'); - root = undefined; - //root.push([]); - //root = root[root.length - 1]; - return; - } - root.push({}); - root = root[root.length - 1]; - return; - }); - }; - exp.anonDriveIntoUser = function (proxyData, fsHash, cb) { // Make sure we have an FS_hash and we don't use it, otherwise just stop the migration and cb if (!fsHash || !proxyData.loggedIn) { @@ -105,46 +27,39 @@ define([ if (parsed) { var proxy = proxyData.proxy; var oldFo = FO.init(parsed.drive, { - loggedIn: proxyData.loggedIn, + loggedIn: true, outer: true }); var onMigrated = function () { oldFo.fixFiles(true); - var newFo = proxyData.userObject; - var oldRecentPads = parsed.drive[newFo.FILES_DATA]; - var newRecentPads = proxy.drive[newFo.FILES_DATA]; - var oldFiles = oldFo.getFiles([newFo.FILES_DATA]); - var newHrefs = Object.keys(newRecentPads).map(function (id) { - return newRecentPads[id].href || newRecentPads[id].roHref; - }); + var manager = proxyData.manager; + var oldRecentPads = parsed.drive[oldFo.FILES_DATA]; + var oldFiles = oldFo.getFiles([oldFo.FILES_DATA]); oldFiles.forEach(function (id) { - var href = oldRecentPads[id].href || oldRecentPads[id].roHref; - var isRo = href === oldRecentPads[id].roHref; - // Do not migrate a pad if we already have it, it would create a duplicate in the drive - if (newHrefs.indexOf(href) !== -1) { return; } - // If we have a stronger version, do not add the current href - // If the current href is read-only, don't check, we won't have a stronger - if (isRo && Hash.findStronger(href, oldRecentPads[id].channel, newRecentPads)) { return; } - // If we have a weaker version, replace the href by the new one - // If the current href is an edit link, don't check, we won't have a weaker - if (!isRo) { - var weaker = Hash.findWeaker(href, oldRecentPads[id].channel, newRecentPads); - if (weaker) { - // Update RECENTPADS - weaker.href = href; - return; - } - } - // Here it means we have a new href, so we should add it to the drive at its old location - var paths = oldFo.findFile(id); - if (paths.length === 0) { return; } - // Add the file data in our array and use the id to add the file var data = oldFo.getFileData(id); + var channel = data.channel; + + var datas = manager.findChannel(channel, true); + // Do not migrate a pad if we already have it, it would create a duplicate + // in the drive + if (datas.length !== 0) { + // We want to merge a read-only pad: it can't be stronger than what + // we already have so abort + if (!data.href) { return; } + + // We want to merge an edit pad: check if we have the same channel + // but read-only and upgrade it in that case + datas.forEach(function (pad) { + if (!pad.href) { data.href = pad.href; } + }); + return; + } + // Here it means we have a new href, so we should add it to the drive if (data) { - // XXX - newFo.pushData(data, function (err, id) { - if (err) { return void console.error("Cannot import file:", data, err); } - createFromPath(proxy, oldFo, paths[0], id); + manager.addPad(null, data, function (err) { + if (err) { + return void console.error("Cannot import file:", data, err); + } }); } }); diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 9730f6337..accc5c380 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -447,7 +447,7 @@ define([ if (data.password) { pad.password = data.password; } if (data.channel) { pad.channel = data.channel; } store.manager.addPad(data.path, pad, function (e) { - if (e) { return void cb({error: "Error while adding a template:"+ e}); } + if (e) { return void cb({error: "Error while adding the pad:"+ e}); } sendDriveEvent('DRIVE_CHANGE', { path: ['drive', UserObject.FILES_DATA] }, clientId); @@ -1170,12 +1170,11 @@ define([ }; Store.addSharedFolder = function (clientId, data, cb) { var path = data.path; + var folderData = data.folderData || {}; var id; nThen(function (waitFor) { - // TODO XXX get the folder data (href, title, ...) - // XXX href should be stored in your drive's .sharedFolders - // and title should be stored in the sharef folder's metadata - var folderData = data.folderData || {}; + Store.pinPads(clientId, [folderData.channel], waitFor()); + }).nThen(function (waitFor) { // 1. add the shared folder to our list of shared folders store.userObject.pushSharedFolder(folderData, waitFor(function (err, folderId) { if (err) { @@ -1370,9 +1369,16 @@ define([ var onReady = function (clientId, returned, cb) { var proxy = store.proxy; - var manager = store.manager = ProxyManager.create(proxy.drive, proxy.edPublic, { - pinPads: function (data, cb) { Store.pinPads(null, data, cb); }, - unpinPads: function (data, cb) { Store.unpinPads(null, data, cb); }, + var unpin = function (data, cb) { + if (!store.loggedIn) { return void cb(); } + Store.unpinPads(null, data, cb); + }; + var pin = function (data, cb) { + if (!store.loggedIn) { return void cb(); } + Store.pinPads(null, data, cb); + }; + var manager = store.manager = ProxyManager.create(proxy.drive, proxy.edPublic, pin, unpin, { + outer: true, removeOwnedChannel: function (data, cb) { Store.removeOwnedChannel(null, data, cb); }, edPublic: store.proxy.edPublic, loggedIn: store.loggedIn, diff --git a/www/common/outer/userObject.js b/www/common/outer/userObject.js index d3854f652..454ba3fc6 100644 --- a/www/common/outer/userObject.js +++ b/www/common/outer/userObject.js @@ -14,10 +14,6 @@ define([ }; module.init = function (config, exp, files) { - var unpinPads = config.unpinPads || function () { - console.error("unpinPads was not provided"); - }; - var pinPads = config.pinPads; var removeOwnedChannel = config.removeOwnedChannel || function () { console.error("removeOwnedChannel was not provided"); }; @@ -54,24 +50,15 @@ define([ exp.pushData = function (data, cb) { if (typeof cb !== "function") { cb = function () {}; } - var todo = function () { - var id = Util.createRandomInteger(); - files[FILES_DATA][id] = data; - cb(null, id); - }; - if (!loggedIn || !AppConfig.enablePinning || config.testMode) { - return void todo(); - } - if (!pinPads) { return; } - pinPads([data.channel], function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - todo(); - }); + var id = Util.createRandomInteger(); + files[FILES_DATA][id] = data; + cb(null, id); }; exp.pushSharedFolder = function (data, cb) { if (typeof cb !== "function") { cb = function () {}; } - // Check we already have this shared folder in our drive + + // Check if we already have this shared folder in our drive if (Object.keys(files[SHARED_FOLDERS]).some(function (k) { return files[SHARED_FOLDERS][k].channel === data.channel; })) { @@ -79,19 +66,12 @@ define([ } // Add the folder - var todo = function () { - var id = Util.createRandomInteger(); - files[SHARED_FOLDERS][id] = data; - cb(null, id); - }; if (!loggedIn || !AppConfig.enablePinning || config.testMode) { return void cb("EAUTH"); } - if (!pinPads) { return void cb('EAUTH'); } - pinPads([data.channel], function (obj) { - if (obj && obj.error) { return void cb(obj.error); } - todo(); - }); + var id = Util.createRandomInteger(); + files[SHARED_FOLDERS][id] = data; + cb(null, id); }; // FILES DATA @@ -102,7 +82,7 @@ define([ // Find files in FILES_DATA that are not anymore in the drive, and remove them from // FILES_DATA. If there are owned pads, remove them from server too, unless the flag tells // us they're already removed - exp.checkDeletedFiles = function (isOwnPadRemoved, noUnpin) { + exp.checkDeletedFiles = function (isOwnPadRemoved, cb) { if (!loggedIn && !config.testMode) { return; } var filesList = exp.getFiles([ROOT, 'hrefArray', TRASH]); @@ -132,7 +112,7 @@ define([ } }); if (!toClean.length) { return; } - if (noUnpin) { return; } + cb(null, toClean); unpinPads(toClean, function (response) { if (response && response.error) { return console.error(response.error); } // console.error(response); @@ -150,7 +130,7 @@ define([ files[TRASH][obj.name].splice(idx, 1); }); }; - exp.deleteMultiplePermanently = function (paths, nocheck, isOwnPadRemoved, noUnpin) { + exp.deleteMultiplePermanently = function (paths, nocheck, isOwnPadRemoved, cb) { var hrefPaths = paths.filter(function(x) { return exp.isPathIn(x, ['hrefArray']); }); var rootPaths = paths.filter(function(x) { return exp.isPathIn(x, [ROOT]); }); var trashPaths = paths.filter(function(x) { return exp.isPathIn(x, [TRASH]); }); @@ -162,7 +142,7 @@ define([ if (!id) { return; } spliceFileData(id); }); - return; + return void cb(); } var ids = []; @@ -203,7 +183,8 @@ define([ // In some cases, we want to remove pads from a location without removing them from // FILES_DATA (replaceHref) - if (!nocheck) { exp.checkDeletedFiles(isOwnPadRemoved, noUnpin); } + if (!nocheck) { exp.checkDeletedFiles(isOwnPadRemoved, cb); } + else { cb(); } }; // Move diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index ba4a17503..bf43bb94e 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -216,7 +216,7 @@ define([ }); // Remove the elements from the old location (without unpinning) - Env.user.userObject.delete(resolved.main, waitFor(), false, false, true); + Env.user.userObject.delete(resolved.main, waitFor()); } } var folderIds = Object.keys(resolved.folders); @@ -240,7 +240,7 @@ define([ }); // Remove the elements from the old location (without unpinning) - uoFrom.delete(paths, waitFor(), false, false, true); + uoFrom.delete(paths, waitFor()); } }); } @@ -280,12 +280,20 @@ define([ } nThen(function (waitFor)  { if (resolved.main.length) { - Env.user.userObject.delete(resolved.main, waitFor(), data.nocheck, - data.isOwnPadRemoved); + Env.user.userObject.delete(resolved.main, waitFor(function (err, toUnpin) { + if (!Env.unpinPads) { return; } + Env.unpinPads(toUnpin, waitFor(function (response) { + if (response && response.error) { return console.error(response.error); } + })); + }), data.nocheck, data.isOwnPadRemoved); } Object.keys(resolved.folders).forEach(function (id) { - Env.folders[id].userObject.delete(resolved.folders[id], waitFor(), data.nocheck, - data.isOwnPadRemoved); + Env.folders[id].userObject.delete(resolved.folders[id], waitFor(function (err, toUnpin) { + if (!Env.unpinPads) { return; } + Env.unpinPads(toUnpin, waitFor(function (response) { + if (response && response.error) { return console.error(response.error); } + })); + }), data.nocheck, data.isOwnPadRemoved); }); }).nThen(function () { cb(); @@ -480,15 +488,26 @@ define([ uo = resolved.userObject; p = resolved.path; } - uo.pushData(pad, function (e, id) { - if (e) { return void cb(e); } - uo.add(id, p); - cb(); + var todo = function () { + console.log('here'); + uo.pushData(pad, function (e, id) { + if (e) { return void cb(e); } + console.log(id, p); + uo.add(id, p); + cb(); + }); + }; + if (!Env.pinPads) { return void todo(); } + Env.pinPads([pad.channel], function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + todo(); }); }; - var create = function (proxy, edPublic, uoConfig) { + var create = function (proxy, edPublic, pinPads, unpinPads, uoConfig) { var Env = { + pinPads: pinPads, + unpinPads: unpinPads, cfg: uoConfig, edPublic: edPublic, user: { @@ -675,7 +694,10 @@ define([ return ret; }; - var findChannels = function (Env, channels) { + var findChannels = function (Env, channels, onlyMain) { + if (onlyMain) { + return Env.user.userObject.findChannels(channels); + } var ret = []; var userObjects = _getUserObjects(Env); userObjects.forEach(function (uo) { diff --git a/www/common/userObject.js b/www/common/userObject.js index ddc9e303c..2449a2dc5 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -46,7 +46,7 @@ define([ console.error.apply(console, arguments); }; - if (pinPads) { + if (config.outer) { // Extend "exp" with methods used only outside of the iframe (requires access to store) OuterFO.init(config, exp, files); } @@ -561,20 +561,20 @@ define([ // DELETE // Permanently delete multiple files at once using a list of paths // NOTE: We have to be careful when removing elements from arrays (trash root, unsorted or template) - exp.delete = function (paths, cb, nocheck, isOwnPadRemoved, noUnpin) { + exp.delete = function (paths, cb, nocheck, isOwnPadRemoved) { if (sframeChan) { return void sframeChan.query("Q_DRIVE_USEROBJECT", { cmd: "delete", data: { paths: paths, nocheck: nocheck, - noUnpin: noUnpin, isOwnPadRemoved: isOwnPadRemoved } }, cb); } - exp.deleteMultiplePermanently(paths, nocheck, isOwnPadRemoved, noUnpin); - if (typeof cb === "function") { cb(); } + cb = cb || function () {}; + exp.deleteMultiplePermanently(paths, nocheck, isOwnPadRemoved, cb); + //if (typeof cb === "function") { cb(); } }; exp.emptyTrash = function (cb) { if (sframeChan) {