From 2cae21104ac3a7a53e2dafc92e0fefddb43fe41f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 2 Oct 2017 14:53:40 +0200 Subject: [PATCH] Replace unbookmark ajax page with Kotlin version --- .../sone/web/ajax/UnbookmarkAjaxPage.java | 71 ---------------------- .../sone/web/ajax/UnbookmarkAjaxPage.kt | 23 +++++++ .../sone/web/ajax/UnbookmarkAjaxPageTest.kt | 4 +- 3 files changed, 25 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java deleted file mode 100644 index 94dd268..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Sone - UnbookmarkAjaxPage.java - Copyright © 2011–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.ajax; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; - -import com.google.common.base.Optional; - -/** - * AJAX page that lets the user unbookmark a post. - * - * @author David ‘Bombe’ Roden - */ -public class UnbookmarkAjaxPage extends JsonPage { - - /** - * Creates a new unbookmark AJAX page. - * - * @param webInterface - * The Sone web interface - */ - public UnbookmarkAjaxPage(WebInterface webInterface) { - super("unbookmark.ajax", webInterface); - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String id = request.getHttpRequest().getParam("post", null); - if ((id == null) || (id.length() == 0)) { - return createErrorJsonObject("invalid-post-id"); - } - Optional post = webInterface.getCore().getPost(id); - if (post.isPresent()) { - webInterface.getCore().unbookmarkPost(post.get()); - } - return createSuccessJsonObject(); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean requiresLogin() { - return false; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.kt new file mode 100644 index 0000000..09a9a88 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.kt @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.utils.also +import net.pterodactylus.sone.utils.let +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest + +/** + * AJAX page that lets the user unbookmark a post. + */ +class UnbookmarkAjaxPage(webInterface: WebInterface) : JsonPage("unbookmark.ajax", webInterface) { + + override fun requiresLogin() = false + + override fun createJsonObject(request: FreenetRequest) = + request.parameters["post"] + ?.let(webInterface.core::getPost) + ?.also(webInterface.core::unbookmarkPost) + ?.let { createSuccessJsonObject() } + ?: createErrorJsonObject("invalid-post-id") + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPageTest.kt index 703b9cd..23ebd9f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPageTest.kt @@ -29,9 +29,9 @@ class UnbookmarkAjaxPageTest : JsonPageTest("unbookmark.ajax", requiresLogin = f } @Test - fun `request with invalid post id does not unbookmark anything but succeeds`() { + fun `request with invalid post id does not unbookmark anything and fails`() { addRequestParameter("post", "invalid") - assertThat(json.isSuccess, equalTo(true)) + assertThat(json.isSuccess, equalTo(false)) verify(core, never()).unbookmarkPost(any()) } -- 2.7.4