From b50431949441894198b9f310cd07f2bdcf7231de Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 6 May 2017 00:37:58 +0200 Subject: [PATCH] Replace known Sones page with Kotlin version --- .../sone/web/pages/KnownSonesPage.java | 153 --------------------- .../pterodactylus/sone/web/pages/KnownSonesPage.kt | 49 +++++++ .../sone/web/pages/KnownSonesPageTest.kt | 2 +- 3 files changed, 50 insertions(+), 154 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java deleted file mode 100644 index af5a7ec..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Sone - KnownSonesPage.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.pages; - -import static net.pterodactylus.sone.utils.NumberParsers.parseInt; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.utils.Pagination; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; - -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; -import com.google.common.collect.Ordering; - -/** - * This page shows all known Sones. - * - * @author David ‘Bombe’ Roden - */ -public class KnownSonesPage extends SoneTemplatePage { - - private static final String defaultSortField = "activity"; - private static final String defaultSortOrder = "desc"; - - /** - * Creates a “known Sones” page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public KnownSonesPage(Template template, WebInterface webInterface) { - super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false); - } - - // - // TEMPLATEPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - String sortField = request.getHttpRequest().getParam("sort", defaultSortField); - String sortOrder = request.getHttpRequest().getParam("order", defaultSortOrder); - String filter = request.getHttpRequest().getParam("filter"); - templateContext.set("sort", sortField); - templateContext.set("order", sortOrder); - templateContext.set("filter", filter); - final Sone currentSone = getCurrentSone(request.getToadletContext(), false); - Collection knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER); - if ((currentSone != null) && "followed".equals(filter)) { - knownSones = Collections2.filter(knownSones, new Predicate() { - - @Override - public boolean apply(Sone sone) { - return currentSone.hasFriend(sone.getId()); - } - }); - } else if ((currentSone != null) && "not-followed".equals(filter)) { - knownSones = Collections2.filter(knownSones, new Predicate() { - - @Override - public boolean apply(Sone sone) { - return !currentSone.hasFriend(sone.getId()); - } - }); - } else if ("new".equals(filter)) { - knownSones = Collections2.filter(knownSones, new Predicate() { - - /** - * {@inheritDoc} - */ - @Override - public boolean apply(Sone sone) { - return !sone.isKnown(); - } - }); - } else if ("not-new".equals(filter)) { - knownSones = Collections2.filter(knownSones, new Predicate() { - - /** - * {@inheritDoc} - */ - @Override - public boolean apply(Sone sone) { - return sone.isKnown(); - } - }); - } else if ("own".equals(filter)) { - knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER); - } else if ("not-own".equals(filter)) { - knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER)); - } - List sortedSones = new ArrayList(knownSones); - if ("activity".equals(sortField)) { - if ("asc".equals(sortOrder)) { - Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse()); - } else { - Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR); - } - } else if ("posts".equals(sortField)) { - if ("asc".equals(sortOrder)) { - Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse()); - } else { - Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR); - } - } else if ("images".equals(sortField)) { - if ("asc".equals(sortOrder)) { - Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse()); - } else { - Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR); - } - } else { - if ("desc".equals(sortOrder)) { - Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse()); - } else { - Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR); - } - } - Pagination sonePagination = new Pagination(sortedSones, 25); - sonePagination.setPage(parseInt(request.getHttpRequest().getParam("page"), 0)); - templateContext.set("pagination", sonePagination); - templateContext.set("knownSones", sonePagination.getItems()); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt new file mode 100644 index 0000000..53941fc --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt @@ -0,0 +1,49 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.utils.Pagination +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext + +/** + * This page shows all known Sones. + */ +class KnownSonesPage(template: Template, webInterface: WebInterface): + SoneTemplatePage("knownSones.html", template, "Page.KnownSones.Title", webInterface, false) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + getCurrentSone(request.toadletContext).let { currentSone -> + Pagination(webInterface.core.sones + .filterNot { request.parameters["filter"] == "followed" && currentSone != null && !currentSone.hasFriend(it.id) } + .filterNot { request.parameters["filter"] == "not-followed" && currentSone != null && currentSone.hasFriend(it.id) } + .filterNot { request.parameters["filter"] == "new" && it.isKnown } + .filterNot { request.parameters["filter"] == "not-new" && !it.isKnown } + .filterNot { request.parameters["filter"] == "own" && !it.isLocal } + .filterNot { request.parameters["filter"] == "not-own" && it.isLocal } + .sortedWith( + when (request.parameters["sort"]) { + "images" -> Sone.IMAGE_COUNT_COMPARATOR + "name" -> Sone.NICE_NAME_COMPARATOR.reversed() + "posts" -> Sone.POST_COUNT_COMPARATOR + else -> Sone.LAST_ACTIVITY_COMPARATOR + }.let { comparator -> + when (request.parameters["order"]) { + "asc" -> comparator.reversed() + else -> comparator + } + } + ), 25).apply { page = request.parameters["page"]?.toIntOrNull() ?: 0 } + .let { pagination -> + templateContext["pagination"] = pagination + templateContext["knownSones"] = pagination.items + } + templateContext["sort"] = request.parameters["sort"].let { sort -> if (sort in listOf("images", "name", "posts")) sort else "activity" } + templateContext["order"] = request.parameters["order"].let { order -> if (order == "asc") "asc" else "desc" } + templateContext["filter"] = request.parameters["filter"] + } + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt index 599c561..42dd33b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.kt @@ -21,7 +21,7 @@ import org.junit.Test /** * Unit test for [KnownSonesPage]. */ -class KnownSonesPageTest : WebPageTest() { +class KnownSonesPageTest: WebPageTest() { private val page = KnownSonesPage(template, webInterface) -- 2.7.4