From: David ‘Bombe’ Roden Date: Mon, 2 Oct 2017 09:59:22 +0000 (+0200) Subject: Replace mark as known ajax page with Kotlin version X-Git-Tag: 0.9.7^2~47 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2ee758b50d597bfd1ec01db720e5b050027c80ea;p=Sone.git Replace mark as known ajax page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java deleted file mode 100644 index fff1975..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Sone - MarkAsKnownAjaxPage.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.core.Core; -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Reply; -import net.pterodactylus.sone.data.Sone; -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 mark a number of {@link Sone}s, {@link Post}s, - * or {@link Reply}s as known. - * - * @author David ‘Bombe’ Roden - */ -public class MarkAsKnownAjaxPage extends JsonPage { - - /** - * Creates a new “mark as known” AJAX page. - * - * @param webInterface - * The Sone web interface - */ - public MarkAsKnownAjaxPage(WebInterface webInterface) { - super("markAsKnown.ajax", webInterface); - } - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String type = request.getHttpRequest().getParam("type"); - if (!type.equals("sone") && !type.equals("post") && !type.equals("reply")) { - return createErrorJsonObject("invalid-type"); - } - String[] ids = request.getHttpRequest().getParam("id").split(" "); - Core core = webInterface.getCore(); - for (String id : ids) { - if (type.equals("post")) { - Optional post = core.getPost(id); - if (!post.isPresent()) { - continue; - } - core.markPostKnown(post.get()); - } else if (type.equals("reply")) { - Optional reply = core.getPostReply(id); - if (!reply.isPresent()) { - continue; - } - core.markReplyKnown(reply.get()); - } else if (type.equals("sone")) { - Optional sone = core.getSone(id); - if (!sone.isPresent()) { - continue; - } - core.markSoneKnown(sone.get()); - } - } - return createSuccessJsonObject(); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean requiresLogin() { - return false; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt new file mode 100644 index 0000000..beec718 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/MarkAsKnownAjaxPage.kt @@ -0,0 +1,31 @@ +package net.pterodactylus.sone.web.ajax + +import com.google.common.base.Optional +import net.pterodactylus.sone.utils.mapPresent +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 mark a number of [net.pterodactylus.sone.data.Sone]s, + * [net.pterodactylus.sone.data.Post]s, or [net.pterodactylus.sone.data.Reply]s as known. + */ +class MarkAsKnownAjaxPage(webInterface: WebInterface) : JsonPage("markAsKnown.ajax", webInterface) { + + override fun requiresLogin() = false + + override fun createJsonObject(request: FreenetRequest) = when (request.parameters["type"]) { + "sone" -> processIds(request, webInterface.core::getSone, webInterface.core::markSoneKnown) + "post" -> processIds(request, webInterface.core::getPost, webInterface.core::markPostKnown) + "reply" -> processIds(request, webInterface.core::getPostReply, webInterface.core::markReplyKnown) + else -> createErrorJsonObject("invalid-type") + } + + private fun processIds(request: FreenetRequest, getter: (String) -> Optional, marker: (T) -> Unit) = + request.parameters["id"] + ?.split(Regex(" +")) + ?.mapPresent(getter) + ?.onEach(marker) + .let { createSuccessJsonObject() } + +}