From: David ‘Bombe’ Roden Date: Sat, 9 Sep 2017 17:31:49 +0000 (+0200) Subject: Replace get likes ajax page with Kotlin version X-Git-Tag: 0.9.7^2~73 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=889b7b47200adb464d44604a812e7bbb1ba89e1f;p=Sone.git Replace get likes ajax page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java deleted file mode 100644 index 7bb75e6..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Sone - GetLikesAjaxPage.java - Copyright © 2010–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 static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; -import static net.pterodactylus.sone.data.Sone.NICE_NAME_COMPARATOR; - -import java.util.Set; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.PostReply; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.template.SoneAccessor; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.base.Optional; -import com.google.common.collect.FluentIterable; - -/** - * AJAX page that retrieves the number of “likes” a {@link Post} has. - * - * @author David ‘Bombe’ Roden - */ -public class GetLikesAjaxPage extends JsonPage { - - /** - * Creates a new “get post likes” AJAX page. - * - * @param webInterface - * The Sone web interface - */ - public GetLikesAjaxPage(WebInterface webInterface) { - super("getLikes.ajax", webInterface); - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String type = request.getHttpRequest().getParam("type", null); - String id = request.getHttpRequest().getParam(type, null); - if ((id == null) || (id.length() == 0)) { - return createErrorJsonObject("invalid-" + type + "-id"); - } - if ("post".equals(type)) { - Optional post = webInterface.getCore().getPost(id); - if (!post.isPresent()) { - return createErrorJsonObject("invalid-post-id"); - } - Set sones = webInterface.getCore().getLikes(post.get()); - return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); - } else if ("reply".equals(type)) { - Optional reply = webInterface.getCore().getPostReply(id); - if (!reply.isPresent()) { - return createErrorJsonObject("invalid-reply-id"); - } - Set sones = webInterface.getCore().getLikes(reply.get()); - return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); - } - return createErrorJsonObject("invalid-type"); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean needsFormPassword() { - return false; - } - - // - // PRIVATE METHODS - // - - /** - * Creates a JSON array (containing the IDs and the nice names) from the - * given Sones, after sorting them by name. - * - * @param sones - * The Sones to convert to an array - * @return The Sones, sorted by name - */ - private static JsonNode getSones(Set sones) { - ArrayNode soneArray = new ArrayNode(instance); - for (Sone sone : FluentIterable.from(sones).toSortedList(NICE_NAME_COMPARATOR)) { - soneArray.add(new ObjectNode(instance).put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone))); - } - return soneArray; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt index 008c141..2ee54ee 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt @@ -6,6 +6,10 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory.instance import com.fasterxml.jackson.databind.node.ObjectNode fun jsonObject(block: ObjectNode.() -> Unit): ObjectNode = ObjectNode(instance).apply(block) +fun jsonObject(vararg properties: Pair) = jsonObject { + properties.forEach { put(it.first, it.second) } +} + fun jsonArray(vararg objects: String?): ArrayNode = objects.fold(ArrayNode(instance), ArrayNode::add) fun jsonArray(vararg objects: JsonNode?): ArrayNode = objects.fold(ArrayNode(instance), ArrayNode::add) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.kt new file mode 100644 index 0000000..fc2142d --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.kt @@ -0,0 +1,45 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.template.SoneAccessor +import net.pterodactylus.sone.utils.jsonArray +import net.pterodactylus.sone.utils.jsonObject +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 retrieves the number of “likes” a [net.pterodactylus.sone.data.Post] + * or [net.pterodactylus.sone.data.PostReply] has. + */ +class GetLikesAjaxPage(webInterface: WebInterface) : JsonPage("getLikes.ajax", webInterface) { + + override fun needsFormPassword() = false + + override fun createJsonObject(request: FreenetRequest) = + when (request.parameters["type"]) { + "post" -> request.parameters["post"] + .let(webInterface.core::getPost) + ?.let(webInterface.core::getLikes) + ?.toReply() + ?: createErrorJsonObject("invalid-post-id") + "reply" -> request.parameters["reply"] + .let(webInterface.core::getPostReply) + ?.let(webInterface.core::getLikes) + ?.toReply() + ?: createErrorJsonObject("invalid-reply-id") + else -> createErrorJsonObject("invalid-type") + } + + private fun Set.toReply() = createSuccessJsonObject().apply { + put("likes", size) + put("sones", sortedBy { SoneAccessor.getNiceName(it) } + .map { + jsonObject("id" to it.id, "name" to SoneAccessor.getNiceName(it)) + } + .let { jsonArray(*it.toTypedArray()) } + ) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/JsonTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/JsonTest.kt index abf391d..b2b0272 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/JsonTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/JsonTest.kt @@ -22,6 +22,12 @@ class JsonTest { } @Test + fun `object node is created with properties`() { + val objectNode = jsonObject("foo" to "bar", "baz" to "quo") + assertThat(objectNode.toString(), equalTo("{\"foo\":\"bar\",\"baz\":\"quo\"}")) + } + + @Test fun `array node is created correctly`() { val arrayNode = listOf( jsonObject { put("foo", "bar") },