diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 48231245d..b787978ca 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -389,6 +389,19 @@ load pinpad dynamically only after you know that it will be needed */ * title * ??? // what else can we put in here? */ + var checkObjectData = function (pad) { + if (!pad.ctime) { pad.ctime = pad.atime; } + if (/^https*:\/\//.test(pad.href)) { + pad.href = common.getRelativeHref(pad.href); + } + var parsed = common.parsePadUrl(pad.href); + if (!parsed || !parsed.hash) { return; } + if (!pad.title) { + pad.title = common.getDefaultname(parsed); + } + return parsed.hash; + }; + // Migrate from legacy store (localStorage) var migrateRecentPads = common.migrateRecentPads = function (pads) { return pads.map(function (pad) { var hash; @@ -405,16 +418,7 @@ load pinpad dynamically only after you know that it will be needed */ ctime: pad[1], }; } else if (pad && typeof(pad) === 'object') { - if (!pad.ctime) { pad.ctime = pad.atime; } - if (!pad.title) { - pad.href.replace(/#(.*)$/, function (x, hash) { - pad.title = hash.slice(0,8); - }); - } - if (/^https*:\/\//.test(pad.href)) { - pad.href = common.getRelativeHref(pad.href); - } - hash = pad.href.slice(pad.href.indexOf('#')+1); + hash = checkObjectData(pad); if (!hash || !common.parseHash(hash)) { return; } return pad; } else { @@ -424,6 +428,18 @@ load pinpad dynamically only after you know that it will be needed */ } }).filter(function (x) { return x; }); }; + // Remove everything from RecentPads that is not an object and check the objects + var checkRecentPads = common.checkRecentPads = function (pads) { + pads.forEach(function (pad, i) { + if (pad && typeof(pad) === 'object') { + var hash = checkObjectData(pad); + if (!hash || !common.parseHash(hash)) { return; } + return pad; + } + console.error("[Cryptpad.migrateRecentPads] pad had unexpected value"); + getStore().removeData(i); + }); + }; // Get the pads from localStorage to migrate them to the object store var getLegacyPads = common.getLegacyPads = function (cb) { @@ -486,14 +502,14 @@ load pinpad dynamically only after you know that it will be needed */ }; // Create untitled documents when no name is given - var getDefaultName = common.getDefaultName = function (parsed, recentPads) { + var getDefaultName = common.getDefaultName = function (parsed) { var type = parsed.type; var untitledIndex = 1; var name = (Messages.type)[type] + ' - ' + new Date().toString().split(' ').slice(0,4).join(' '); return name; }; var isDefaultName = common.isDefaultName = function (parsed, title) { - var name = getDefaultName(parsed, []); + var name = getDefaultName(parsed); return title === name; }; @@ -594,29 +610,18 @@ load pinpad dynamically only after you know that it will be needed */ }; // STORAGE - /* fetch and migrate your pad history from localStorage */ + /* fetch and migrate your pad history from the store */ var getRecentPads = common.getRecentPads = function (cb) { getStore().getDrive(storageKey, function (err, recentPads) { if (isArray(recentPads)) { - cb(void 0, migrateRecentPads(recentPads)); + checkRecentPads(recentPads); + cb(void 0, recentPads); return; } cb(void 0, []); }); }; - // STORAGE - /* commit a list of pads to localStorage */ - // TODO integrate pinning if enabled - var setRecentPads = common.setRecentPads = function (pads, cb) { - getStore().setDrive(storageKey, pads, function (err, data) { - if (PINNING_ENABLED && isLoggedIn()) { - console.log("TODO check pin hash"); - } - cb(err, data); - }); - }; - // STORAGE: Display Name var getLastName = common.getLastName = function (cb) { common.getAttribute('username', function (err, userName) { @@ -636,7 +641,6 @@ load pinpad dynamically only after you know that it will be needed */ }; // STORAGE - // TODO integrate pinning if enabled var forgetPad = common.forgetPad = function (href, cb) { var parsed = parsePadUrl(href); @@ -724,6 +728,8 @@ load pinpad dynamically only after you know that it will be needed */ var href = window.location.href; var parsed = parsePadUrl(href); href = getRelativeHref(href); + // getRecentPads return the array from the drive, not a copy + // We don't have to call "set..." at the end, everything is stored with listmap getRecentPads(function (err, recent) { if (err) { cb(err); @@ -779,20 +785,15 @@ load pinpad dynamically only after you know that it will be needed */ if (!contains) { var data = makePad(href, name); - renamed.push(data); - if (typeof(getStore().addPad) === "function") { - getStore().addPad(href, common.initialPath, common.initialName || name); - } + getStore().pushData(data); + getStore().addPad(href, common.initialPath, common.initialName || name); } - - setRecentPads(renamed, function (err, data) { - if (updateWeaker.length > 0) { - updateWeaker.forEach(function (obj) { - getStore().replaceHref(obj.o, obj.n); - }); - } - cb(err, data); - }); + if (updateWeaker.length > 0) { + updateWeaker.forEach(function (obj) { + getStore().replaceHref(obj.o, obj.n); + }); + } + cb(err, recent); }); }; @@ -920,6 +921,13 @@ load pinpad dynamically only after you know that it will be needed */ // TODO check if pin list is up to date // if not, reset + common.arePinsSynced(function (err, yes) { + if (!yes) { + common.resetPins(function (err, hash) { + console.log('RESET DONE'); + }); + } + }); cb(); }); } else if (PINNING_ENABLED) { @@ -1098,6 +1106,9 @@ load pinpad dynamically only after you know that it will be needed */ }; var pinsReady = common.pinsReady = function () { + if (!isLoggedIn()) { + return false; + } if (!PINNING_ENABLED) { console.error('[PINNING_DISABLED]'); return false; @@ -1121,8 +1132,7 @@ load pinpad dynamically only after you know that it will be needed */ }; var resetPins = common.resetPins = function (cb) { - if (!PINNING_ENABLED) { return void console.error('[PINNING_DISABLED]'); } - if (!rpc) { return void console.error('[RPC_NOT_READY]'); } + if (!pinsReady()) { return void cb ('[RPC_NOT_READY]'); } var list = getCanonicalChannelList(); rpc.reset(list, function (e, hash) { @@ -1131,6 +1141,24 @@ load pinpad dynamically only after you know that it will be needed */ }); }; + var pinPads = common.pinPads = function (pads, cb) { + if (!pinsReady()) { return void cb ('[RPC_NOT_READY]'); } + + rpc.pin(pads, function (e, hash) { + if (e) { return void cb(e); } + cb(void 0, hash); + }); + }; + + var unpinPads = common.unpinPads = function (pads, cb) { + if (!pinsReady()) { return void cb ('[RPC_NOT_READY]'); } + + rpc.unpin(pads, function (e, hash) { + if (e) { return void cb(e); } + cb(void 0, hash); + }); + }; + var createButton = common.createButton = function (type, rightside, data, callback) { var button; var size = "17px"; diff --git a/www/common/fileObject.js b/www/common/fileObject.js index 537ada142..202a67f4c 100644 --- a/www/common/fileObject.js +++ b/www/common/fileObject.js @@ -45,6 +45,23 @@ define([ return a; }; + var pushFileData = exp.pushData = function (data) { + Cryptpad.pinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e, hash) { + console.log(hash); + }); + files[FILES_DATA].push(data); + }; + var spliceFileData = exp.removeData = function (idx) { + var data = files[FILES_DATA][idx]; + if (typeof data === "object") { + Cryptpad.unpinPads([Cryptpad.hrefToHexChannelId(data.href)], function (e, hash) { + console.log(hash); + }); + } + files[FILES_DATA].splice(idx, 1); + }; + + var comparePath = exp.comparePath = function (a, b) { if (!a || !b || !$.isArray(a) || !$.isArray(b)) { return false; } if (a.length !== b.length) { return false; } @@ -72,12 +89,15 @@ define([ var isPathInTrash = exp.isPathInTrash = function (path) { return path[0] && path[0] === TRASH; }; + var isInTrashRoot = exp.isInTrashRoot = function (path) { + return path[0] === TRASH && path.length === 4; + }; var isPathInFilesData = exp.isPathInFilesData = function (path) { return path[0] && path[0] === FILES_DATA; }; - var isFile = exp.isFile = function (element) { + var isFile = exp.isFile = function (element) { return typeof(element) === "string"; }; @@ -422,10 +442,6 @@ define([ return paths; }; - var isInTrashRoot = exp.isInTrashRoot = function (path) { - return path[0] === TRASH && path.length === 4; - }; - var removePadAttribute = function (f) { Object.keys(files).forEach(function (key) { var hash = f.indexOf('#') !== -1 ? f.slice(f.indexOf('#') + 1) : null; @@ -459,7 +475,7 @@ define([ var idx = files[FILES_DATA].indexOf(f); if (idx !== -1) { debug("Removing", f, "from filesData"); - files[FILES_DATA].splice(idx, 1); + spliceFileData(idx); removePadAttribute(f.href); } }); @@ -469,9 +485,9 @@ define([ var parentPath = path.slice(); var key = parentPath.pop(); var parentEl = exp.findElement(files, parentPath); - if (path.length === 4 && path[0] === TRASH) { + if (isInTrashRoot(path)) { files[TRASH][path[1]].splice(path[2], 1); - } else if (path[0] === UNSORTED || path[0] === TEMPLATE) { + } else if (isPathInHrefArray(path)) { parentEl.splice(key, 1); } else { parentEl[key] = undefined; @@ -745,7 +761,7 @@ define([ }; var pushNewFileData = function (href, title) { - files[FILES_DATA].push({ + pushFileData({ href: href, title: title, atime: +new Date(), @@ -865,7 +881,7 @@ define([ var idx = files[FILES_DATA].indexOf(f); if (idx !== -1) { debug("Removing", f, "from filesData"); - files[FILES_DATA].splice(idx, 1); + spliceFileData(idx); // Remove the "padAttributes" stored in the realtime object for that pad removePadAttribute(f.href); } @@ -1009,7 +1025,7 @@ define([ return o.href === href; }); if (!test) { - files[FILES_DATA].push(fileData); + pushFileData(fileData); } if (files[TEMPLATE].indexOf(href) === -1) { files[TEMPLATE].push(href); @@ -1141,7 +1157,7 @@ define([ toClean.forEach(function (el) { var idx = fd.indexOf(el); if (idx !== -1) { - fd.splice(idx, 1); + spliceFileData(idx); } }); }; diff --git a/www/common/fsStore.js b/www/common/fsStore.js index 794a39ecd..1974aa27d 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -86,6 +86,9 @@ define([ cb(void 0, Object.keys(storeObj)); }; + ret.removeData = filesOp.removeData; + ret.pushData = filesOp.pushData; + ret.addPad = function (href, path, name) { filesOp.addPad(href, path, name); };