Move pin/unpin outside of userObject

pull/1/head
yflory 6 years ago
parent 66a494cbd2
commit 321c7d5512

@ -6,84 +6,6 @@ define([
], function (Crypt, FO, Hash, Realtime) { ], function (Crypt, FO, Hash, Realtime) {
var exp = {}; 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) { 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 // Make sure we have an FS_hash and we don't use it, otherwise just stop the migration and cb
if (!fsHash || !proxyData.loggedIn) { if (!fsHash || !proxyData.loggedIn) {
@ -105,46 +27,39 @@ define([
if (parsed) { if (parsed) {
var proxy = proxyData.proxy; var proxy = proxyData.proxy;
var oldFo = FO.init(parsed.drive, { var oldFo = FO.init(parsed.drive, {
loggedIn: proxyData.loggedIn, loggedIn: true,
outer: true outer: true
}); });
var onMigrated = function () { var onMigrated = function () {
oldFo.fixFiles(true); oldFo.fixFiles(true);
var newFo = proxyData.userObject; var manager = proxyData.manager;
var oldRecentPads = parsed.drive[newFo.FILES_DATA]; var oldRecentPads = parsed.drive[oldFo.FILES_DATA];
var newRecentPads = proxy.drive[newFo.FILES_DATA]; var oldFiles = oldFo.getFiles([oldFo.FILES_DATA]);
var oldFiles = oldFo.getFiles([newFo.FILES_DATA]);
var newHrefs = Object.keys(newRecentPads).map(function (id) {
return newRecentPads[id].href || newRecentPads[id].roHref;
});
oldFiles.forEach(function (id) { oldFiles.forEach(function (id) {
var href = oldRecentPads[id].href || oldRecentPads[id].roHref; var data = oldFo.getFileData(id);
var isRo = href === oldRecentPads[id].roHref; var channel = data.channel;
// Do not migrate a pad if we already have it, it would create a duplicate in the drive
if (newHrefs.indexOf(href) !== -1) { return; } var datas = manager.findChannel(channel, true);
// If we have a stronger version, do not add the current href // Do not migrate a pad if we already have it, it would create a duplicate
// If the current href is read-only, don't check, we won't have a stronger // in the drive
if (isRo && Hash.findStronger(href, oldRecentPads[id].channel, newRecentPads)) { return; } if (datas.length !== 0) {
// If we have a weaker version, replace the href by the new one // We want to merge a read-only pad: it can't be stronger than what
// If the current href is an edit link, don't check, we won't have a weaker // we already have so abort
if (!isRo) { if (!data.href) { return; }
var weaker = Hash.findWeaker(href, oldRecentPads[id].channel, newRecentPads);
if (weaker) { // We want to merge an edit pad: check if we have the same channel
// Update RECENTPADS // but read-only and upgrade it in that case
weaker.href = href; datas.forEach(function (pad) {
if (!pad.href) { data.href = pad.href; }
});
return; return;
} }
} // Here it means we have a new href, so we should add it to the drive
// 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);
if (data) { if (data) {
// XXX manager.addPad(null, data, function (err) {
newFo.pushData(data, function (err, id) { if (err) {
if (err) { return void console.error("Cannot import file:", data, err); } return void console.error("Cannot import file:", data, err);
createFromPath(proxy, oldFo, paths[0], id); }
}); });
} }
}); });

@ -447,7 +447,7 @@ define([
if (data.password) { pad.password = data.password; } if (data.password) { pad.password = data.password; }
if (data.channel) { pad.channel = data.channel; } if (data.channel) { pad.channel = data.channel; }
store.manager.addPad(data.path, pad, function (e) { 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', { sendDriveEvent('DRIVE_CHANGE', {
path: ['drive', UserObject.FILES_DATA] path: ['drive', UserObject.FILES_DATA]
}, clientId); }, clientId);
@ -1170,12 +1170,11 @@ define([
}; };
Store.addSharedFolder = function (clientId, data, cb) { Store.addSharedFolder = function (clientId, data, cb) {
var path = data.path; var path = data.path;
var folderData = data.folderData || {};
var id; var id;
nThen(function (waitFor) { nThen(function (waitFor) {
// TODO XXX get the folder data (href, title, ...) Store.pinPads(clientId, [folderData.channel], waitFor());
// XXX href should be stored in your drive's .sharedFolders }).nThen(function (waitFor) {
// and title should be stored in the sharef folder's metadata
var folderData = data.folderData || {};
// 1. add the shared folder to our list of shared folders // 1. add the shared folder to our list of shared folders
store.userObject.pushSharedFolder(folderData, waitFor(function (err, folderId) { store.userObject.pushSharedFolder(folderData, waitFor(function (err, folderId) {
if (err) { if (err) {
@ -1370,9 +1369,16 @@ define([
var onReady = function (clientId, returned, cb) { var onReady = function (clientId, returned, cb) {
var proxy = store.proxy; var proxy = store.proxy;
var manager = store.manager = ProxyManager.create(proxy.drive, proxy.edPublic, { var unpin = function (data, cb) {
pinPads: function (data, cb) { Store.pinPads(null, data, cb); }, if (!store.loggedIn) { return void cb(); }
unpinPads: function (data, cb) { Store.unpinPads(null, data, 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); }, removeOwnedChannel: function (data, cb) { Store.removeOwnedChannel(null, data, cb); },
edPublic: store.proxy.edPublic, edPublic: store.proxy.edPublic,
loggedIn: store.loggedIn, loggedIn: store.loggedIn,

@ -14,10 +14,6 @@ define([
}; };
module.init = function (config, exp, files) { 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 () { var removeOwnedChannel = config.removeOwnedChannel || function () {
console.error("removeOwnedChannel was not provided"); console.error("removeOwnedChannel was not provided");
}; };
@ -54,24 +50,15 @@ define([
exp.pushData = function (data, cb) { exp.pushData = function (data, cb) {
if (typeof cb !== "function") { cb = function () {}; } if (typeof cb !== "function") { cb = function () {}; }
var todo = function () {
var id = Util.createRandomInteger(); var id = Util.createRandomInteger();
files[FILES_DATA][id] = data; files[FILES_DATA][id] = data;
cb(null, id); 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();
});
};
exp.pushSharedFolder = function (data, cb) { exp.pushSharedFolder = function (data, cb) {
if (typeof cb !== "function") { cb = function () {}; } 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) { if (Object.keys(files[SHARED_FOLDERS]).some(function (k) {
return files[SHARED_FOLDERS][k].channel === data.channel; return files[SHARED_FOLDERS][k].channel === data.channel;
})) { })) {
@ -79,19 +66,12 @@ define([
} }
// Add the folder // Add the folder
var todo = function () {
var id = Util.createRandomInteger();
files[SHARED_FOLDERS][id] = data;
cb(null, id);
};
if (!loggedIn || !AppConfig.enablePinning || config.testMode) { if (!loggedIn || !AppConfig.enablePinning || config.testMode) {
return void cb("EAUTH"); return void cb("EAUTH");
} }
if (!pinPads) { return void cb('EAUTH'); } var id = Util.createRandomInteger();
pinPads([data.channel], function (obj) { files[SHARED_FOLDERS][id] = data;
if (obj && obj.error) { return void cb(obj.error); } cb(null, id);
todo();
});
}; };
// FILES DATA // FILES DATA
@ -102,7 +82,7 @@ define([
// Find files in FILES_DATA that are not anymore in the drive, and remove them from // 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 // FILES_DATA. If there are owned pads, remove them from server too, unless the flag tells
// us they're already removed // us they're already removed
exp.checkDeletedFiles = function (isOwnPadRemoved, noUnpin) { exp.checkDeletedFiles = function (isOwnPadRemoved, cb) {
if (!loggedIn && !config.testMode) { return; } if (!loggedIn && !config.testMode) { return; }
var filesList = exp.getFiles([ROOT, 'hrefArray', TRASH]); var filesList = exp.getFiles([ROOT, 'hrefArray', TRASH]);
@ -132,7 +112,7 @@ define([
} }
}); });
if (!toClean.length) { return; } if (!toClean.length) { return; }
if (noUnpin) { return; } cb(null, toClean);
unpinPads(toClean, function (response) { unpinPads(toClean, function (response) {
if (response && response.error) { return console.error(response.error); } if (response && response.error) { return console.error(response.error); }
// console.error(response); // console.error(response);
@ -150,7 +130,7 @@ define([
files[TRASH][obj.name].splice(idx, 1); 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 hrefPaths = paths.filter(function(x) { return exp.isPathIn(x, ['hrefArray']); });
var rootPaths = paths.filter(function(x) { return exp.isPathIn(x, [ROOT]); }); var rootPaths = paths.filter(function(x) { return exp.isPathIn(x, [ROOT]); });
var trashPaths = paths.filter(function(x) { return exp.isPathIn(x, [TRASH]); }); var trashPaths = paths.filter(function(x) { return exp.isPathIn(x, [TRASH]); });
@ -162,7 +142,7 @@ define([
if (!id) { return; } if (!id) { return; }
spliceFileData(id); spliceFileData(id);
}); });
return; return void cb();
} }
var ids = []; var ids = [];
@ -203,7 +183,8 @@ define([
// In some cases, we want to remove pads from a location without removing them from // In some cases, we want to remove pads from a location without removing them from
// FILES_DATA (replaceHref) // FILES_DATA (replaceHref)
if (!nocheck) { exp.checkDeletedFiles(isOwnPadRemoved, noUnpin); } if (!nocheck) { exp.checkDeletedFiles(isOwnPadRemoved, cb); }
else { cb(); }
}; };
// Move // Move

@ -216,7 +216,7 @@ define([
}); });
// Remove the elements from the old location (without unpinning) // 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); var folderIds = Object.keys(resolved.folders);
@ -240,7 +240,7 @@ define([
}); });
// Remove the elements from the old location (without unpinning) // 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)  { nThen(function (waitFor)  {
if (resolved.main.length) { if (resolved.main.length) {
Env.user.userObject.delete(resolved.main, waitFor(), data.nocheck, Env.user.userObject.delete(resolved.main, waitFor(function (err, toUnpin) {
data.isOwnPadRemoved); 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) { Object.keys(resolved.folders).forEach(function (id) {
Env.folders[id].userObject.delete(resolved.folders[id], waitFor(), data.nocheck, Env.folders[id].userObject.delete(resolved.folders[id], waitFor(function (err, toUnpin) {
data.isOwnPadRemoved); 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 () { }).nThen(function () {
cb(); cb();
@ -480,15 +488,26 @@ define([
uo = resolved.userObject; uo = resolved.userObject;
p = resolved.path; p = resolved.path;
} }
var todo = function () {
console.log('here');
uo.pushData(pad, function (e, id) { uo.pushData(pad, function (e, id) {
if (e) { return void cb(e); } if (e) { return void cb(e); }
console.log(id, p);
uo.add(id, p); uo.add(id, p);
cb(); 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 = { var Env = {
pinPads: pinPads,
unpinPads: unpinPads,
cfg: uoConfig, cfg: uoConfig,
edPublic: edPublic, edPublic: edPublic,
user: { user: {
@ -675,7 +694,10 @@ define([
return ret; return ret;
}; };
var findChannels = function (Env, channels) { var findChannels = function (Env, channels, onlyMain) {
if (onlyMain) {
return Env.user.userObject.findChannels(channels);
}
var ret = []; var ret = [];
var userObjects = _getUserObjects(Env); var userObjects = _getUserObjects(Env);
userObjects.forEach(function (uo) { userObjects.forEach(function (uo) {

@ -46,7 +46,7 @@ define([
console.error.apply(console, arguments); console.error.apply(console, arguments);
}; };
if (pinPads) { if (config.outer) {
// Extend "exp" with methods used only outside of the iframe (requires access to store) // Extend "exp" with methods used only outside of the iframe (requires access to store)
OuterFO.init(config, exp, files); OuterFO.init(config, exp, files);
} }
@ -561,20 +561,20 @@ define([
// DELETE // DELETE
// Permanently delete multiple files at once using a list of paths // 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) // 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) { if (sframeChan) {
return void sframeChan.query("Q_DRIVE_USEROBJECT", { return void sframeChan.query("Q_DRIVE_USEROBJECT", {
cmd: "delete", cmd: "delete",
data: { data: {
paths: paths, paths: paths,
nocheck: nocheck, nocheck: nocheck,
noUnpin: noUnpin,
isOwnPadRemoved: isOwnPadRemoved isOwnPadRemoved: isOwnPadRemoved
} }
}, cb); }, cb);
} }
exp.deleteMultiplePermanently(paths, nocheck, isOwnPadRemoved, noUnpin); cb = cb || function () {};
if (typeof cb === "function") { cb(); } exp.deleteMultiplePermanently(paths, nocheck, isOwnPadRemoved, cb);
//if (typeof cb === "function") { cb(); }
}; };
exp.emptyTrash = function (cb) { exp.emptyTrash = function (cb) {
if (sframeChan) { if (sframeChan) {

Loading…
Cancel
Save