Merge branch 'defaultTitle'

pull/1/head
ansuz 8 years ago
commit c9ff6a519b

@ -73,5 +73,11 @@ define(function () {
out.loginText = '<p>Your username and password are used to generate a unique key which is never known by our server.</p>\n' + out.loginText = '<p>Your username and password are used to generate a unique key which is never known by our server.</p>\n' +
'<p>Be careful not to forget your credentials, as they are impossible to recover</p>'; '<p>Be careful not to forget your credentials, as they are impossible to recover</p>';
out.type = {};
out.type.pad = 'Pad';
out.type.code = 'Code';
out.type.poll = 'Poll';
out.type.slide = 'Presentation';
return out; return out;
}); });

@ -25,6 +25,8 @@ define([
spinner: Cryptpad.spinner(document.body), spinner: Cryptpad.spinner(document.body),
}; };
Cryptpad.styleAlerts();
module.spinner.show(); module.spinner.show();
var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow; var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow;
@ -240,12 +242,13 @@ define([
}; };
var suggestName = function () { var suggestName = function () {
var hash = window.location.hash.slice(1, 9); var parsed = Cryptpad.parsePadUrl(window.location.href);
var name = Cryptpad.getDefaultName(parsed, []);
if (document.title === hash) { if (document.title.slice(0, name.length) === name) {
return getHeadingText() || hash; return getHeadingText() || document.title;
} else { } else {
return document.title || getHeadingText() || hash; return document.title || getHeadingText() || name;
} }
}; };
@ -375,7 +378,8 @@ define([
console.error(err); console.error(err);
return; return;
} }
document.title = window.location.hash.slice(1,9); var parsed = Cryptpad.parsePadUrl(href);
document.title = Cryptpad.getDefaultName(parsed, []);
}); });
}); });
}); });
@ -439,14 +443,14 @@ define([
configureTheme(); configureTheme();
}); });
window.location.hash = info.channel + secret.key; window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key);
Cryptpad.getPadTitle(function (err, title) { Cryptpad.getPadTitle(function (err, title) {
if (err) { if (err) {
console.log("Unable to get pad title"); console.log("Unable to get pad title");
console.error(err); console.error(err);
return; return;
} }
document.title = title || window.location.hash.slice(1,9); document.title = title || info.channel.slice(0, 8);
Cryptpad.rememberPad(title, function (err, data) { Cryptpad.rememberPad(title, function (err, data) {
if (err) { if (err) {
console.log("Unable to set pad title"); console.log("Unable to set pad title");
@ -622,7 +626,6 @@ define([
Cryptpad.alert(Messages.disconnectAlert); Cryptpad.alert(Messages.disconnectAlert);
}; };
Cryptpad.styleAlerts();
var realtime = module.realtime = Realtime.start(config); var realtime = module.realtime = Realtime.start(config);
editor.on('change', onLocal); editor.on('change', onLocal);

@ -100,7 +100,7 @@ define([
// don't do anything funny unless you're on a cryptpad subdomain // don't do anything funny unless you're on a cryptpad subdomain
if (!/cryptpad.fr$/i.test(hostname)) { return; } if (!/cryptpad.fr$/i.test(hostname)) { return; }
if (hash.length >= 56) { if (hash.length >= 25) {
// you're on the right domain // you're on the right domain
return; return;
} }
@ -116,16 +116,68 @@ define([
window.location.hostname = 'old.cryptpad.fr'; window.location.hostname = 'old.cryptpad.fr';
}; };
var hexToBase64 = common.hexToBase64 = function (hex) {
var hexArray = hex
.replace(/\r|\n/g, "")
.replace(/([\da-fA-F]{2}) ?/g, "0x$1 ")
.replace(/ +$/, "")
.split(" ");
var byteString = String.fromCharCode.apply(null, hexArray);
return window.btoa(byteString).replace(/\//g, '-').slice(0,-2);
};
var base64ToHex = common.base64ToHex = function (b64String) {
var hexArray = [];
atob(b64String.replace(/-/g, '/') + "==").split("").forEach(function(e){
var h = e.charCodeAt(0).toString(16);
if (h.length === 1) { h = "0"+h; }
hexArray.push(h);
});
return hexArray.join("");
};
var getHashFromKeys = common.getHashFromKeys = function (chanKey, cryptKey) {
return '/1/' + hexToBase64(chanKey) + '/' + cryptKey.replace(/\//g, '-');
};
var getSecrets = common.getSecrets = function () { var getSecrets = common.getSecrets = function () {
var secret = {}; var secret = {};
if (!/#/.test(window.location.href)) { if (!/#/.test(window.location.href)) {
secret.key = Crypto.genKey(); secret.key = Crypto.genKey();
} else { } else {
var hash = window.location.hash.slice(1); var hash = window.location.hash.slice(1);
if (hash.length === 0) {
secret.key = Crypto.genKey();
return secret;
}
common.redirect(hash); common.redirect(hash);
// old hash system : #{hexChanKey}{cryptKey}
// new hash system : #/{hashVersion}/{b64ChanKey}/{cryptKey}
if (hash.slice(0,1) !== '/' && hash.length >= 56) {
// Old hash
secret.channel = hash.slice(0, 32); secret.channel = hash.slice(0, 32);
secret.key = hash.slice(32); secret.key = hash.slice(32);
} }
else {
// New hash
var hashArray = hash.split('/');
if (hashArray.length < 4) {
common.alert("Unable to parse the key");
throw new Error("Unable to parse the key");
}
var version = hashArray[1];
if (version === "1") {
secret.channel = base64ToHex(hashArray[2]);
secret.key = hashArray[3].replace(/-/g, '/'); //TODO replace / by -
if (secret.channel.length !== 32 || secret.key.length !== 24) {
common.alert("The channel key and/or the encryption key is invalid");
console.log("Channel key length : " + secret.channel.length + " != 32");
console.log("Encryption key length : " + secret.key.length + " != 24");
throw new Error("The channel key and/or the encryption key is invalid");
}
}
}
}
return secret; return secret;
}; };
@ -193,6 +245,25 @@ define([
return ret; return ret;
}; };
var isNameAvailable = function (title, parsed, pads) {
return !pads.some(function (pad) {
// another pad is already using that title
if (pad.title === title) {
return true;
}
});
};
// Create untitled documents when no name is given
var getDefaultName = common.getDefaultName = function (parsed, recentPads) {
var type = parsed.type;
var untitledIndex = 1;
var name = (Messages.type)[type] + ' - ' + new Date().toString().split(' ').slice(0,4).join(' ');
if (isNameAvailable(name, parsed, recentPads)) { return name; }
while (!isNameAvailable(name + ' - ' + untitledIndex, parsed, recentPads)) { untitledIndex++; }
return name + ' - ' + untitledIndex;
};
var makePad = function (href, title) { var makePad = function (href, title) {
var now = ''+new Date(); var now = ''+new Date();
return { return {
@ -379,6 +450,8 @@ define([
} }
}); });
if (title === '') { title = getDefaultName(parsed, pads); }
cb(void 0, title); cb(void 0, title);
}); });
}; };
@ -396,7 +469,7 @@ define([
var conflicts = pads.some(function (pad) { var conflicts = pads.some(function (pad) {
// another pad is already using that title // another pad is already using that title
if (pad.title === title) { if (pad.title === title) {
var p = parsePadUrl(href); var p = parsePadUrl(pad.href);
if (p.type === parsed.type && p.hash === parsed.hash) { if (p.type === parsed.type && p.hash === parsed.hash) {
// the duplicate pad has the same type and hash // the duplicate pad has the same type and hash

@ -21,13 +21,14 @@ define([
], function (Config, Messages, Crypto, realtimeInput, Hyperjson, ], function (Config, Messages, Crypto, realtimeInput, Hyperjson,
Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad, Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad,
Visible, Notify) { Visible, Notify) {
var $ = window.jQuery; var $ = window.jQuery;
var saveAs = window.saveAs; var saveAs = window.saveAs;
var ifrw = $('#pad-iframe')[0].contentWindow; var ifrw = $('#pad-iframe')[0].contentWindow;
var Ckeditor; // to be initialized later... var Ckeditor; // to be initialized later...
var DiffDom = window.diffDOM; var DiffDom = window.diffDOM;
Cryptpad.styleAlerts();
var stringify = function (obj) { var stringify = function (obj) {
return JSONSortify(obj); return JSONSortify(obj);
}; };
@ -439,12 +440,13 @@ define([
}; };
var suggestName = module.suggestName = function () { var suggestName = module.suggestName = function () {
var hash = window.location.hash.slice(1, 9); var parsed = Cryptpad.parsePadUrl(window.location.href);
var name = Cryptpad.getDefaultName(parsed, []);
if (document.title === hash) { if (document.title.slice(0, name.length) === name) {
return getHeadingText() || hash; return getHeadingText() || document.title;
} else { } else {
return document.title || getHeadingText() || hash; return document.title || getHeadingText() || name;
} }
}; };
@ -535,14 +537,15 @@ define([
Cryptpad.confirm(Messages.forgetPrompt, function (yes) { Cryptpad.confirm(Messages.forgetPrompt, function (yes) {
if (!yes) { return; } if (!yes) { return; }
Cryptpad.forgetPad(href, function (err, data) { Cryptpad.forgetPad(href, function (err, data) {
document.title = window.location.hash.slice(1,9); var parsed = Cryptpad.parsePadUrl(href);
document.title = Cryptpad.getDefaultName(parsed, []);
}); });
}); });
}); });
$rightside.append($forgetPad); $rightside.append($forgetPad);
// set the hash // set the hash
window.location.hash = info.channel + secret.key; window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key);
Cryptpad.getPadTitle(function (err, title) { Cryptpad.getPadTitle(function (err, title) {
if (err) { if (err) {
@ -550,7 +553,7 @@ define([
console.log("Couldn't get pad title"); console.log("Couldn't get pad title");
return; return;
} }
document.title = title || window.location.hash.slice(1, 9); document.title = title || info.channel.slice(0, 8);
Cryptpad.rememberPad(title, function (err, data) { Cryptpad.rememberPad(title, function (err, data) {
if (err) { if (err) {
console.log("Couldn't remember pad"); console.log("Couldn't remember pad");
@ -558,8 +561,6 @@ define([
} }
}); });
}); });
Cryptpad.styleAlerts();
}; };
// this should only ever get called once, when the chain syncs // this should only ever get called once, when the chain syncs

@ -649,17 +649,6 @@ define([
setEditable(false); setEditable(false);
}); });
Cryptpad.getPadTitle(function (err, title) {
title = document.title = title || window.location.hash.slice(1, 9);
Cryptpad.rememberPad(title, function (err, data) {
if (err) {
console.log("unable to remember pad");
console.log(err);
return;
}
});
});
var $toolbar = $('#toolbar'); var $toolbar = $('#toolbar');
@ -669,12 +658,15 @@ define([
return $('<button>', opt); return $('<button>', opt);
}; };
var suggestName = function () { var suggestName = module.suggestName = function () {
var hash = window.location.hash.slice(1, 9); var parsed = Cryptpad.parsePadUrl(window.location.href);
if (document.title === hash) { var name = Cryptpad.getDefaultName(parsed, []);
return $title.val() || hash;
if (document.title.slice(0, name.length) === name) {
return $title.val() || document.title;
} else {
return document.title || $title.val() || name;
} }
return document.title || $title.val() || hash;
}; };
$toolbar.append(Button({ $toolbar.append(Button({
@ -691,7 +683,8 @@ define([
console.error(err); console.error(err);
return; return;
} }
document.title = window.location.hash.slice(1, 9); var parsed = Cryptpad.parsePadUrl(href);
document.title = Cryptpad.getDefaultName(parsed, []);
}); });
}); });
})); }));
@ -848,11 +841,22 @@ define([
var rt = module.rt = Listmap.create(config); var rt = module.rt = Listmap.create(config);
rt.proxy.on('create', function (info) { rt.proxy.on('create', function (info) {
var realtime = module.realtime = info.realtime; var realtime = module.realtime = info.realtime;
window.location.hash = info.channel + secret.key; window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key);
module.patchText = TextPatcher.create({ module.patchText = TextPatcher.create({
realtime: realtime, realtime: realtime,
logging: true, logging: true,
}); });
Cryptpad.getPadTitle(function (err, title) {
title = document.title = title || info.channel.slice(0, 8);
Cryptpad.rememberPad(title, function (err, data) {
if (err) {
console.log("unable to remember pad");
console.log(err);
return;
}
});
});
}).on('ready', ready) }).on('ready', ready)
.on('disconnect', function () { .on('disconnect', function () {
setEditable(false); setEditable(false);

@ -92,7 +92,7 @@ define([
}; };
var onInit = config.onInit = function (info) { var onInit = config.onInit = function (info) {
window.location.hash = info.channel + secret.key; window.location.hash = Cryptpad.getHashFromKeys(info.channel, secret.key);
$(window).on('hashchange', function() { $(window).on('hashchange', function() {
window.location.reload(); window.location.reload();
}); });
@ -102,7 +102,7 @@ define([
console.log("Couldn't get pad title"); console.log("Couldn't get pad title");
return; return;
} }
document.title = APP.title = title || window.location.hash.slice(1, 9); document.title = APP.title = title || info.channel.slice(0, 8);
Cryptpad.rememberPad(title, function (err, data) { Cryptpad.rememberPad(title, function (err, data) {
if (err) { if (err) {
console.log("Couldn't remember pad"); console.log("Couldn't remember pad");
@ -140,7 +140,8 @@ define([
console.log(err); console.log(err);
return; return;
} }
document.title = APP.title = window.location.hash.slice(1,9); var parsed = Cryptpad.parsePadUrl(href);
document.title = APP.title = Cryptpad.getDefaultName(parsed, []);
}); });
}); });
}); });

Loading…
Cancel
Save