Move pad attributes in pad data

This commit is contained in:
yflory
2017-07-05 12:21:01 +02:00
parent 3e802c2443
commit 43ac71603c
6 changed files with 81 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
define(function () {
define(['jquery'], function ($) {
var module = {};
module.create = function (cfg, onLocal, Cryptpad) {
@@ -45,7 +45,8 @@ define(function () {
};
// update title: href is optional; if not specified, we use window.location.href
exp.updateTitle = function (newTitle, href) {
exp.updateTitle = function (newTitle, href, cb) {
cb = cb || $.noop;
if (newTitle === exp.title) { return; }
// Change the title now, and set it back to the old value if there is an error
var oldTitle = exp.title;
@@ -54,9 +55,10 @@ define(function () {
console.log("Couldn't set pad title");
console.error(err);
updateLocalTitle(oldTitle);
return;
return void cb(err);
}
updateLocalTitle(data);
cb(null, data);
if (!$title) { return; }
$title.find('span.title').text(data);
$title.find('input').val(data);

View File

@@ -425,9 +425,8 @@ define([
// STORAGE
common.setPadAttribute = function (attr, value, cb) {
getStore().setDrive([getHash(), attr].join('.'), value, function (err, data) {
cb(err, data);
});
var href = getRelativeHref(window.location.href);
getStore().setPadAttribute(href, attr, value, cb);
};
common.setAttribute = function (attr, value, cb) {
getStore().set(["cryptpad", attr].join('.'), value, function (err, data) {
@@ -440,9 +439,8 @@ define([
// STORAGE
common.getPadAttribute = function (attr, cb) {
getStore().getDrive([getHash(), attr].join('.'), function (err, data) {
cb(err, data);
});
var href = getRelativeHref(window.location.href);
getStore().getPadAttribute(href, attr, cb);
};
common.getAttribute = function (attr, cb) {
getStore().get(["cryptpad", attr].join('.'), function (err, data) {

View File

@@ -60,6 +60,9 @@ define([
cb(void 0, res);
};
ret.setPadAttribute = filesOp.setAttribute;
ret.getPadAttribute = filesOp.getAttribute;
ret.getDrive = function (key, cb) {
cb(void 0, storeObj.drive[key]);
};

View File

@@ -132,6 +132,21 @@ define([
if (type === 'name') { return data.filename; }
return data.filename || data.title || NEW_FILE_NAME;
};
exp.getAttribute = function (href, attr, cb) {
cb = cb || $.noop;
var id = exp.getIdFromHref(href);
if (!id) { return void cb(null, undefined); }
var data = getFileData(id);
cb(null, data[attr]);
};
exp.setAttribute = function (href, attr, value, cb) {
cb = cb || $.noop;
var id = exp.getIdFromHref(href);
if (!id) { return void cb("E_INVAL_HREF"); }
if (!attr || !attr.trim()) { return void cb("E_INVAL_ATTR"); }
var data = getFileData(id);
data[attr] = value;
};
// PATHS
@@ -970,6 +985,20 @@ define([
us.splice(idx, 1);
});
};
var migrateAttributes = function (el, id, parsed) {
// Migrate old pad attributes
['userid', 'previewMode'].forEach(function (attr) {
var key = parsed.hash + '.' + attr;
var key2 = parsed.hash.slice(0,-1) + '.' + attr;// old pads not ending with /
if (files[key] || files[key2]) {
debug("Migrating pad attribute", attr, "for pad", id);
el[attr] = files[key] || files[key2];
}
delete files[key];
delete files[key2];
});
// Migration done
};
var fixFilesData = function () {
if (typeof files[FILES_DATA] !== "object") { debug("OLD_FILES_DATA was not an object"); files[FILES_DATA] = {}; }
var fd = files[FILES_DATA];
@@ -989,6 +1018,15 @@ define([
toClean.push(id);
continue;
}
var parsed = Cryptpad.parsePadUrl(el.href);
if (!parsed.hash) {
debug("Removing an element in filesData with a invalid href.", el);
toClean.push(id);
continue;
}
migrateAttributes(el, id, parsed);
if ((Cryptpad.isLoggedIn() || config.testMode) && rootFiles.indexOf(id) === -1) {
debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", id, el);
var newName = Cryptpad.createChannelId();
@@ -1001,12 +1039,19 @@ define([
});
};
var fixDrive = function () {
Object.keys(files).forEach(function (key) {
if (key.slice(0,1) === '/') { delete files[key]; }
});
};
fixRoot();
fixTrashRoot();
if (!workgroup) {
fixTemplate();
fixFilesData();
}
fixDrive();
if (JSON.stringify(files) !== before) {
debug("Your file system was corrupted. It has been cleaned so that the pads you visit can be stored safely");