From 70328c1ee52a67654eaa899ab951d4c1dbe3a697 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 29 Jun 2017 22:30:16 +0200 Subject: [PATCH] Replace bookmark ajax page with Kotlin version --- .../sone/web/ajax/BookmarkAjaxPage.java | 71 ---------------------- .../net/pterodactylus/sone/utils/Optionals.kt | 2 +- .../sone/web/ajax/BookmarkAjaxPage.kt | 23 +++++++ .../pterodactylus/sone/web/ajax/JsonPageTest.kt | 3 + 4 files changed, 27 insertions(+), 72 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java deleted file mode 100644 index ce5276b..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Sone - BookmarkAjaxPage.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 bookmark a post. - * - * @author David ‘Bombe’ Roden - */ -public class BookmarkAjaxPage extends JsonPage { - - /** - * Creates a new bookmark AJAX page. - * - * @param webInterface - * The Sone web interface - */ - public BookmarkAjaxPage(WebInterface webInterface) { - super("bookmark.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().bookmarkPost(post.get()); - } - return createSuccessJsonObject(); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean requiresLogin() { - return false; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt index c8167b9..326db9d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt @@ -3,7 +3,7 @@ package net.pterodactylus.sone.utils import com.google.common.base.Optional fun Optional.let(block: (T) -> R): R? = if (isPresent) block(get()) else null -fun Optional.also(block: (T) -> Unit) = if (isPresent) block(get()) else Unit +fun Optional.also(block: (T) -> Unit): Optional { if (isPresent) block(get()); return this } fun T?.asOptional(): Optional = this?.let { Optional.of(it) } ?: Optional.absent() diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt new file mode 100644 index 0000000..fd22f06 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.utils.also +import net.pterodactylus.sone.utils.emptyToNull +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 bookmark a post. + */ +class BookmarkAjaxPage(webInterface: WebInterface) : JsonPage("bookmark.ajax", webInterface) { + + override fun requiresLogin() = false + + override fun createJsonObject(request: FreenetRequest) = + request.parameters["post"].emptyToNull + ?.let(webInterface.core::getPost) + ?.also(webInterface.core::bookmarkPost) + ?.let { createSuccessJsonObject() } + ?: createErrorJsonObject("invalid-post-id") + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index b5f17cf..b4d897c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -19,6 +19,7 @@ import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.notify.Notification +import net.pterodactylus.util.web.Method.GET import org.junit.Before import org.mockito.ArgumentMatchers.anyInt import org.mockito.ArgumentMatchers.anyString @@ -82,11 +83,13 @@ open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock