From 2250327211a1f5c1adf2eeef68091a58dd0bf46d Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 29 May 2017 17:20:19 +0200 Subject: [PATCH] Add "delete identity" button in settings --- init.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++---- settings.js | 25 +++++++++++++++++++++++ settings.phtml | 8 ++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 settings.js diff --git a/init.php b/init.php index 2fbabab..4c0f598 100644 --- a/init.php +++ b/init.php @@ -110,11 +110,23 @@ class Micropub extends Plugin implements IHandler } $accounts = PluginHost::getInstance()->get($this, 'accounts', []); + if (isset($_REQUEST['accordion']) + && $_REQUEST['accordion'] == 'micropub' + ) { + $accordionActive = 'selected="true"'; + } else { + $accordionActive = ''; + } //FIXME: default identity include __DIR__ . '/settings.phtml'; } + public function get_prefs_js() + { + return file_get_contents(__DIR__ . '/settings.js'); + } + /** * CLI command */ @@ -148,6 +160,8 @@ class Micropub extends Plugin implements IHandler return $this->authreturnAction(); } else if ($mode == 'post') { return $this->postAction(); + } else if ($mode == 'deleteIdentity') { + return $this->deleteIdentityAction(); } else { return $this->errorOut('Unsupported mode'); } @@ -158,17 +172,17 @@ class Micropub extends Plugin implements IHandler if (!isset($_POST['me'])) { return $this->errorOut('"me" parameter missing'); } - $me = $_POST['me']; + $me = trim($_POST['me']); if (!isset($_POST['replyTo'])) { return $this->errorOut('"replyTo" parameter missing'); } - $replyTo = $_POST['replyTo']; + $replyTo = trim($_POST['replyTo']); if (!isset($_POST['content'])) { return $this->errorOut('"content" parameter missing'); } - $content = $_POST['content']; + $content = trim($_POST['content']); $accounts = PluginHost::getInstance()->get($this, 'accounts', []); if (!isset($accounts[$me])) { @@ -346,7 +360,39 @@ class Micropub extends Plugin implements IHandler $host->set($this, 'accounts', $accounts); //all fine now. - header('Location: prefs.php'); + //the accordion parameter will never work + // because fox has serious mental problems + // https://discourse.tt-rss.org/t/open-a-certain-accordion-in-preferences-by-url-parameter/234 + header('Location: prefs.php?accordion=micropub'); + } + + /** + * Backend preferences action: Remove a given account + */ + protected function deleteIdentityAction() + { + if (!isset($_POST['me'])) { + return $this->errorOut('"me" parameter missing'); + } + $me = trim($_POST['me']); + + $host = PluginHost::getInstance(); + $accounts = $host->get($this, 'accounts', []); + if (!isset($accounts[$me])) { + return $this->errorOut('Unknown identity'); + } + + unset($accounts[$me]); + $host->set($this, 'accounts', $accounts); + header('Content-type: application/json'); + + echo json_encode( + [ + 'code' => '200', + 'message' => 'Identity removed', + ] + ); + exit(); } /** diff --git a/settings.js b/settings.js new file mode 100644 index 0000000..40657a2 --- /dev/null +++ b/settings.js @@ -0,0 +1,25 @@ +/** + * Delete a given account + */ +function micropubDeleteIdentity(accountUrl) +{ + new Ajax.Request('backend.php', { + parameters: { + 'op': 'pluginhandler', + 'plugin': 'micropub', + 'method': 'action', + 'mode': 'deleteIdentity', + 'me': accountUrl, + }, + onSuccess: function(transport) { + notify_info('Account removed'); + var elems = dojo.query('tr[data-url="' + accountUrl + '"]'); + if (elems.length > 0) { + elems.first().remove(); + } + }, + onFailure: function(transport) { + notify_error(transport.responseText); + } + }); +} diff --git a/settings.phtml b/settings.phtml index a06f2ea..a22a00b 100644 --- a/settings.phtml +++ b/settings.phtml @@ -1,4 +1,4 @@ -
+
>

Registered identities

@@ -6,17 +6,21 @@ + $account) { ?> - + + -- 2.30.2
Default Identity URL
+ Remove +