Pin the pads

pull/1/head
yflory 8 years ago
parent b3867429ee
commit cde724399c

@ -389,6 +389,19 @@ load pinpad dynamically only after you know that it will be needed */
* title * title
* ??? // what else can we put in here? * ??? // 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) { var migrateRecentPads = common.migrateRecentPads = function (pads) {
return pads.map(function (pad) { return pads.map(function (pad) {
var hash; var hash;
@ -405,16 +418,7 @@ load pinpad dynamically only after you know that it will be needed */
ctime: pad[1], ctime: pad[1],
}; };
} else if (pad && typeof(pad) === 'object') { } else if (pad && typeof(pad) === 'object') {
if (!pad.ctime) { pad.ctime = pad.atime; } hash = checkObjectData(pad);
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);
if (!hash || !common.parseHash(hash)) { return; } if (!hash || !common.parseHash(hash)) { return; }
return pad; return pad;
} else { } else {
@ -424,6 +428,18 @@ load pinpad dynamically only after you know that it will be needed */
} }
}).filter(function (x) { return x; }); }).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 // Get the pads from localStorage to migrate them to the object store
var getLegacyPads = common.getLegacyPads = function (cb) { 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 // 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 type = parsed.type;
var untitledIndex = 1; var untitledIndex = 1;
var name = (Messages.type)[type] + ' - ' + new Date().toString().split(' ').slice(0,4).join(' '); var name = (Messages.type)[type] + ' - ' + new Date().toString().split(' ').slice(0,4).join(' ');
return name; return name;
}; };
var isDefaultName = common.isDefaultName = function (parsed, title) { var isDefaultName = common.isDefaultName = function (parsed, title) {
var name = getDefaultName(parsed, []); var name = getDefaultName(parsed);
return title === name; return title === name;
}; };
@ -594,29 +610,18 @@ load pinpad dynamically only after you know that it will be needed */
}; };
// STORAGE // STORAGE
/* fetch and migrate your pad history from localStorage */ /* fetch and migrate your pad history from the store */
var getRecentPads = common.getRecentPads = function (cb) { var getRecentPads = common.getRecentPads = function (cb) {
getStore().getDrive(storageKey, function (err, recentPads) { getStore().getDrive(storageKey, function (err, recentPads) {
if (isArray(recentPads)) { if (isArray(recentPads)) {
cb(void 0, migrateRecentPads(recentPads)); checkRecentPads(recentPads);
cb(void 0, recentPads);
return; return;
} }
cb(void 0, []); 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 // STORAGE: Display Name
var getLastName = common.getLastName = function (cb) { var getLastName = common.getLastName = function (cb) {
common.getAttribute('username', function (err, userName) { common.getAttribute('username', function (err, userName) {
@ -636,7 +641,6 @@ load pinpad dynamically only after you know that it will be needed */
}; };
// STORAGE // STORAGE
// TODO integrate pinning if enabled
var forgetPad = common.forgetPad = function (href, cb) { var forgetPad = common.forgetPad = function (href, cb) {
var parsed = parsePadUrl(href); 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 href = window.location.href;
var parsed = parsePadUrl(href); var parsed = parsePadUrl(href);
href = getRelativeHref(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) { getRecentPads(function (err, recent) {
if (err) { if (err) {
cb(err); cb(err);
@ -779,20 +785,15 @@ load pinpad dynamically only after you know that it will be needed */
if (!contains) { if (!contains) {
var data = makePad(href, name); var data = makePad(href, name);
renamed.push(data); getStore().pushData(data);
if (typeof(getStore().addPad) === "function") { getStore().addPad(href, common.initialPath, common.initialName || name);
getStore().addPad(href, common.initialPath, common.initialName || name);
}
} }
if (updateWeaker.length > 0) {
setRecentPads(renamed, function (err, data) { updateWeaker.forEach(function (obj) {
if (updateWeaker.length > 0) { getStore().replaceHref(obj.o, obj.n);
updateWeaker.forEach(function (obj) { });
getStore().replaceHref(obj.o, obj.n); }
}); cb(err, recent);
}
cb(err, data);
});
}); });
}; };
@ -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 // TODO check if pin list is up to date
// if not, reset // if not, reset
common.arePinsSynced(function (err, yes) {
if (!yes) {
common.resetPins(function (err, hash) {
console.log('RESET DONE');
});
}
});
cb(); cb();
}); });
} else if (PINNING_ENABLED) { } 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 () { var pinsReady = common.pinsReady = function () {
if (!isLoggedIn()) {
return false;
}
if (!PINNING_ENABLED) { if (!PINNING_ENABLED) {
console.error('[PINNING_DISABLED]'); console.error('[PINNING_DISABLED]');
return false; return false;
@ -1121,8 +1132,7 @@ load pinpad dynamically only after you know that it will be needed */
}; };
var resetPins = common.resetPins = function (cb) { var resetPins = common.resetPins = function (cb) {
if (!PINNING_ENABLED) { return void console.error('[PINNING_DISABLED]'); } if (!pinsReady()) { return void cb ('[RPC_NOT_READY]'); }
if (!rpc) { return void console.error('[RPC_NOT_READY]'); }
var list = getCanonicalChannelList(); var list = getCanonicalChannelList();
rpc.reset(list, function (e, hash) { 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 createButton = common.createButton = function (type, rightside, data, callback) {
var button; var button;
var size = "17px"; var size = "17px";

@ -45,6 +45,23 @@ define([
return a; 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) { var comparePath = exp.comparePath = function (a, b) {
if (!a || !b || !$.isArray(a) || !$.isArray(b)) { return false; } if (!a || !b || !$.isArray(a) || !$.isArray(b)) { return false; }
if (a.length !== b.length) { return false; } if (a.length !== b.length) { return false; }
@ -459,7 +476,7 @@ define([
var idx = files[FILES_DATA].indexOf(f); var idx = files[FILES_DATA].indexOf(f);
if (idx !== -1) { if (idx !== -1) {
debug("Removing", f, "from filesData"); debug("Removing", f, "from filesData");
files[FILES_DATA].splice(idx, 1); spliceFileData(idx);
removePadAttribute(f.href); removePadAttribute(f.href);
} }
}); });
@ -745,7 +762,7 @@ define([
}; };
var pushNewFileData = function (href, title) { var pushNewFileData = function (href, title) {
files[FILES_DATA].push({ pushFileData({
href: href, href: href,
title: title, title: title,
atime: +new Date(), atime: +new Date(),
@ -865,7 +882,7 @@ define([
var idx = files[FILES_DATA].indexOf(f); var idx = files[FILES_DATA].indexOf(f);
if (idx !== -1) { if (idx !== -1) {
debug("Removing", f, "from filesData"); debug("Removing", f, "from filesData");
files[FILES_DATA].splice(idx, 1); spliceFileData(idx);
// Remove the "padAttributes" stored in the realtime object for that pad // Remove the "padAttributes" stored in the realtime object for that pad
removePadAttribute(f.href); removePadAttribute(f.href);
} }
@ -1009,7 +1026,7 @@ define([
return o.href === href; return o.href === href;
}); });
if (!test) { if (!test) {
files[FILES_DATA].push(fileData); pushFileData(fileData);
} }
if (files[TEMPLATE].indexOf(href) === -1) { if (files[TEMPLATE].indexOf(href) === -1) {
files[TEMPLATE].push(href); files[TEMPLATE].push(href);
@ -1141,7 +1158,7 @@ define([
toClean.forEach(function (el) { toClean.forEach(function (el) {
var idx = fd.indexOf(el); var idx = fd.indexOf(el);
if (idx !== -1) { if (idx !== -1) {
fd.splice(idx, 1); spliceFileData(idx);
} }
}); });
}; };

@ -86,6 +86,9 @@ define([
cb(void 0, Object.keys(storeObj)); cb(void 0, Object.keys(storeObj));
}; };
ret.removeData = filesOp.removeData;
ret.pushData = filesOp.pushData;
ret.addPad = function (href, path, name) { ret.addPad = function (href, path, name) {
filesOp.addPad(href, path, name); filesOp.addPad(href, path, name);
}; };

Loading…
Cancel
Save