Replace known Sones page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 5 May 2017 22:37:58 +0000 (00:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 5 May 2017 22:37:58 +0000 (00:37 +0200)
src/main/java/net/pterodactylus/sone/web/pages/KnownSonesPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPageTest.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 (file)
index af5a7ec..0000000
+++ /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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
-               if ((currentSone != null) && "followed".equals(filter)) {
-                       knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
-
-                               @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<Sone>() {
-
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return !currentSone.hasFriend(sone.getId());
-                               }
-                       });
-               } else if ("new".equals(filter)) {
-                       knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
-
-                               /**
-                                * {@inheritDoc}
-                                */
-                               @Override
-                               public boolean apply(Sone sone) {
-                                       return !sone.isKnown();
-                               }
-                       });
-               } else if ("not-new".equals(filter)) {
-                       knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
-
-                               /**
-                                * {@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<Sone> sortedSones = new ArrayList<Sone>(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<Sone> sonePagination = new Pagination<Sone>(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 (file)
index 0000000..53941fc
--- /dev/null
@@ -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"]
+               }
+       }
+
+}
index 599c561..42dd33b 100644 (file)
@@ -21,7 +21,7 @@ import org.junit.Test
 /**
  * Unit test for [KnownSonesPage].
  */
-class KnownSonesPageTest : WebPageTest() {
+class KnownSonesPageTest: WebPageTest() {
 
        private val page = KnownSonesPage(template, webInterface)