From 718383205bb3b06f55eac2cec04c6da368fd60ba Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 20 Jun 2022 18:13:19 +0000 Subject: [PATCH] Add a thing to remove apps from your app list. Should I have called in 'uninstall'? git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3916 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 16 ++++++++++++++++ core/core.js | 30 +++++++++++++++++++++++++++++- core/index.html | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/client.js b/core/client.js index b911aa0d..ac8f544a 100644 --- a/core/client.js +++ b/core/client.js @@ -366,6 +366,22 @@ function save(save_to) { }); } +function deleteApp() { + let name = document.getElementById("name"); + let path = name && name.value ? name.value : url(); + + if (confirm(`Are you sure you want to delete the app '${path}'?`)) { + fetch(path + 'delete').then(function(response) { + if (!response.ok) { + throw new Error(response.status + ' ' + response.statusText); + } + alert('Deleted.'); + }).catch(function(error) { + alert(error); + }); + } +} + function pullFromParent() { load(gParentApp ? gParentApp.path : null).then(x => save()).catch(function(error) { alert(error) diff --git a/core/core.js b/core/core.js index f04d3c0c..2062e0cf 100644 --- a/core/core.js +++ b/core/core.js @@ -459,6 +459,34 @@ async function blobHandler(request, response, blobId, uri) { response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"}); response.end("/" + newBlobId); + } else if (uri == "/delete") { + let match; + if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) { + var user = match[1]; + var appName = match[2]; + var credentials = auth.query(request.headers); + if (credentials && credentials.session && + (credentials.session.name == user || + (credentials.permissions.administration && user == 'core'))) { + var database = new Database(user); + var apps = new Set(); + try { + apps = new Set(JSON.parse(database.get('apps'))); + } catch { + } + if (apps.delete(appName)) { + database.set('apps', JSON.stringify([...apps])); + } + database.remove('path:' + appName); + } else { + response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"}); + response.end("401 Unauthorized"); + return; + } + } + + response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"}); + response.end('OK'); } else { var data; var type; @@ -581,7 +609,7 @@ loadSettings().then(function() { return staticDirectoryHandler(request, response, 'deps/split/', match[1]); } else if (match = /^\/smoothie\/([\.\w-/]*)$/.exec(request.uri)) { return staticDirectoryHandler(request, response, 'deps/smoothie/', match[1]); - } else if (match = /^(.*)(\/save?)$/.exec(request.uri)) { + } else if (match = /^(.*)(\/(?:save|delete)?)$/.exec(request.uri)) { return blobHandler(request, response, match[1], match[2]); } else if (match = /^\/trace$/.exec(request.uri)) { var data = trace(); diff --git a/core/index.html b/core/index.html index f338d4de..2514c3ad 100644 --- a/core/index.html +++ b/core/index.html @@ -33,6 +33,7 @@ +