From 5f049cf587d824bd836e78bce22795538f462c6d Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Thu, 15 Feb 2024 16:02:56 +0100 Subject: [PATCH] change remote call (init) --- handler.php | 23 +++---- main.js | 155 ++++++++++++++++++++++----------------------- settingssample.php | 2 +- 3 files changed, 86 insertions(+), 94 deletions(-) diff --git a/handler.php b/handler.php index c3d3857..0cc9601 100644 --- a/handler.php +++ b/handler.php @@ -5,44 +5,37 @@ require 'settings.php'; // check authent if ($password && (!isset($_POST['password']) || $_POST['password'] != $password)) { - echo '{"error": "authent"}'; + echo 'error: authent'; } else if (isset($_POST['action'])) { $action = $_POST['action']; + $path = $datadir . $_POST['name']; switch ($action) { case 'fetch': - if (file_exists($datafile)) + if (file_exists($path)) { - echo file_get_contents($datafile); - } - else - { - echo '[]'; + echo file_get_contents($path); } break; case 'push': - $result = file_put_contents($datafile, $_POST['data']); + $result = file_put_contents($path, $_POST['data']); if ($result === false) { - echo '{"error": "could not save ' . $datafile . '"}'; - } - else - { - echo '{"result": "ok"}'; + echo 'error: could not save ' . $_POST['name']; } break; default: - echo '{"error": "unknown action ' . $action . '"}'; + echo 'error: unknown action ' . $action; break; } } else { - echo '{"error": "missing action parameter"}'; + echo 'error: missing action parameter'; } ?> diff --git a/main.js b/main.js index f825f60..d8462d6 100644 --- a/main.js +++ b/main.js @@ -186,10 +186,6 @@ var commands = [ hint: "Download all notes (html files in zip archive)", action: downloadhtmlnotes }, -{ - hint: "Download all notes (json file)", - action: downloadnotesjson -}, { hint: "Insert text in todo", action: promptinserttodo @@ -1063,11 +1059,6 @@ function promptinserttodo() } } -function downloadnotesjson() -{ - download("notes-" + timestamp() + ".json", window.localStorage.getItem("data")); -} - function downloadnotewithsubs() { var note = withsubs(); @@ -1288,6 +1279,67 @@ function migratelegacystorage() } } +function pushall() +{ + var index = JSON.parse(localStorage.getItem("index")); + var list = [queryremote({action: "push", name: "index", data: localStorage.getItem("index")})]; + Object.keys(index).forEach(guid => + { + list.push(queryremote({action: "push", name: guid, data: localStorage.getItem(guid)})); + }); + + return Promise.all(list); +} + +function fetch() +{ + return new Promise(function(resolve) + { + if (settings.sync) + { + var pgpkeys = localStorage.getItem("pgpkeys"); + if (!pgpkeys) + { + loadstorage(); + editpgpkeys(); + showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh."); + } + else + { + queryremote({action: "fetch", name: "index"}) + .then(filecontent => + { + if (!filecontent) + { + return pushall(); + } + else + { + // compare and fetch modified and serialize all that + } + + }).catch(err => + { + if (err == "error: authent") + { + settings.password = prompt("Password: ", settings.password); + savesettings(); + init(); + } + else + { + showtemporaryinfo(err); + } + }); + } + } + else + { + resolve(); + } + }); +} + function init() { migratelegacystorage(); @@ -1298,56 +1350,23 @@ function init() initsnippets(); - if (settings.sync) - { - if (localStorage.getItem("pgpkeys") && localStorage.getItem("pgpkeys").startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----")) - { - queryremote({action: "fetch"}) - .then(data => - { - if (data.length) - { - window.localStorage.setItem("data", JSON.stringify(data)); - } - loadstorage(); - }) - .catch(err => - { - if (err == "Authent failed") - { - settings.password = prompt("Password: ", settings.password); - savesettings(); - init(); - } - else - { - showtemporaryinfo(err); - } - }); - } - else - { - loadstorage(); - editpgpkeys(); - showtemporaryinfo("Pgp key empty or invalid. Enter PGP keys and refresh."); - } - } - else + fetch() + .then( () => { loadstorage(); - } - if (issplit()) - { - if (settings.defaultpreviewinsplit && name == "right") + if (issplit()) { - togglepreview(); + if (settings.defaultpreviewinsplit && name == "right") + { + togglepreview(); + } + else if (name == "left") + { + md.focus(); + } } - else if (name == "left") - { - md.focus(); - } - } + }); } function queryremote(params) @@ -1379,33 +1398,17 @@ function queryremote(params) } else { - var data = {}; - decryptstring(xhr.responseText) .then(decrypted => { - data = JSON.parse(decrypted); - - if (data.error) + if (decrypted.startsWith("error: ")) { - if (data.error == "authent") - { - failed("Authent failed"); - } - else - { - failed("Remote handler returned an error: " + data.error); - } + failed(decrypted); } else { - apply(data); + apply(decrypted); } - }) - .catch( error => - { - failed("Could not decrypt or parse data file."); - console.error(error); }); } } @@ -2441,11 +2444,7 @@ function shortcutmatches(event, shortcut) function executecommand(command) { - if (command.remoteonly && !settings.sync) - { - showtemporaryinfo(command.hint + " is not available in local mode."); - } - else if (command.action) + if (command.action) { command.action(); } diff --git a/settingssample.php b/settingssample.php index 29fe1c6..bb1c1a8 100644 --- a/settingssample.php +++ b/settingssample.php @@ -1,4 +1,4 @@ \ No newline at end of file