Replace new page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 24 May 2017 17:59:20 +0000 (19:59 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 24 May 2017 17:59:20 +0000 (19:59 +0200)
src/main/java/net/pterodactylus/sone/web/pages/NewPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt

diff --git a/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java b/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java
deleted file mode 100644 (file)
index a00f96c..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Sone - NewPage.java - Copyright © 2013–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.Collections;
-import java.util.HashSet;
-import java.util.List;
-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.notify.PostVisibilityFilter;
-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;
-
-/**
- * Page that displays all new posts and replies. The posts are filtered using
- * {@link PostVisibilityFilter#isPostVisible(Sone, Post)} and sorted by time.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class NewPage extends SoneTemplatePage {
-
-       /**
-        * Creates a new “new posts and replies” page.
-        *
-        * @param template
-        *            The template to render
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public NewPage(Template template, WebInterface webInterface) {
-               super("new.html", template, "Page.New.Title", webInterface);
-       }
-
-       //
-       // SONETEMPLATEPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
-               /* collect new elements from notifications. */
-               Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts(getCurrentSone(request.getToadletContext(), false)));
-               for (PostReply reply : webInterface.getNewReplies(getCurrentSone(request.getToadletContext(), false))) {
-                       posts.add(reply.getPost().get());
-               }
-
-               /* filter and sort them. */
-               List<Post> sortedPosts = new ArrayList<>(posts);
-               Collections.sort(sortedPosts, Post.NEWEST_FIRST);
-
-               /* paginate them. */
-               Pagination<Post> pagination = new Pagination<>(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage());
-               pagination.setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
-               templateContext.set("pagination", pagination);
-               templateContext.set("posts", pagination.getItems());
-       }
-
-}
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt
new file mode 100644 (file)
index 0000000..d1841d4
--- /dev/null
@@ -0,0 +1,33 @@
+package net.pterodactylus.sone.web.pages
+
+import net.pterodactylus.sone.utils.Pagination
+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
+import net.pterodactylus.util.template.Template
+import net.pterodactylus.util.template.TemplateContext
+
+/**
+ * Page that displays all new posts and replies. The posts are filtered using
+ * [PostVisibilityFilter.isPostVisible(Sone, Post)] and sorted by time.
+ */
+class NewPage(template: Template, webInterface: WebInterface):
+               SoneTemplatePage("new.html", template, "Page.New.Title", webInterface, false) {
+
+       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) =
+                       getCurrentSone(request.toadletContext).let { currentSone ->
+                               (webInterface.getNewPosts(currentSone) + webInterface.getNewReplies(currentSone).mapPresent { it.post })
+                                               .distinct()
+                                               .sortedByDescending { it.time }
+                                               .let { posts ->
+                                                       Pagination(posts, webInterface.core.preferences.postsPerPage).apply {
+                                                               page = request.parameters["page"]?.toIntOrNull() ?: 0
+                                                       }.let { pagination ->
+                                                               templateContext["pagination"] = pagination
+                                                               templateContext["posts"] = pagination.items
+                                                       }
+                                               }
+                       }
+
+}
index 62b1baa..2420e5b 100644 (file)
@@ -5,7 +5,10 @@ import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.test.asOptional
 import net.pterodactylus.sone.test.mock
 import net.pterodactylus.sone.test.whenever
+import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.util.web.Method.GET
 import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
 import org.hamcrest.Matchers.containsInAnyOrder
 import org.hamcrest.Matchers.equalTo
 import org.junit.Before
@@ -44,8 +47,9 @@ class NewPageTest: WebPageTest() {
 
        @Test
        fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
-               val extraPost = mock<Post>()
-               val posts = asList(mock<Post>(), mock<Post>())
+               setMethod(GET)
+               val extraPost = mock<Post>().withTime(2000)
+               val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
                val postReplies = asList(mock<PostReply>(), mock<PostReply>())
                whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
                whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
@@ -54,7 +58,34 @@ class NewPageTest: WebPageTest() {
 
                verifyNoRedirect {
                        val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
-                       assertThat(renderedPosts, containsInAnyOrder(posts[0], posts[1], extraPost))
+                       assertThat(renderedPosts, containsInAnyOrder(posts[1], extraPost, posts[0]))
+               }
+       }
+
+       private fun Post.withTime(time: Long) = apply { whenever(this.time).thenReturn(time) }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `posts are paginated properly`() {
+               setMethod(GET)
+               webInterface.core.preferences.postsPerPage = 2
+               val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
+               whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+               verifyNoRedirect {
+                       assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
+               }
+       }
+
+       @Test
+       @Suppress("UNCHECKED_CAST")
+       fun `posts are paginated properly on second page`() {
+               setMethod(GET)
+               webInterface.core.preferences.postsPerPage = 2
+               addHttpRequestParameter("page", "1")
+               val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
+               whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+               verifyNoRedirect {
+                       assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
                }
        }