From 65e3c95fe9d44b5654b2ac76fc09b9aaac50ca9c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 24 May 2017 19:59:20 +0200 Subject: [PATCH] Replace new page with Kotlin version --- .../net/pterodactylus/sone/web/pages/NewPage.java | 84 ---------------------- .../net/pterodactylus/sone/web/pages/NewPage.kt | 33 +++++++++ .../pterodactylus/sone/web/pages/NewPageTest.kt | 37 +++++++++- 3 files changed, 67 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/NewPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.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 index a00f96c..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/NewPage.java +++ /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 . - */ - -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 David ‘Bombe’ Roden - */ -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 posts = new HashSet(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 sortedPosts = new ArrayList<>(posts); - Collections.sort(sortedPosts, Post.NEWEST_FIRST); - - /* paginate them. */ - Pagination 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 index 0000000..d1841d4 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -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 + } + } + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt index 62b1baa..2420e5b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt @@ -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() - val posts = asList(mock(), mock()) + setMethod(GET) + val extraPost = mock().withTime(2000) + val posts = asList(mock().withTime(1000), mock().withTime(3000)) val postReplies = asList(mock(), mock()) 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>("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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) + whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts) + verifyNoRedirect { + assertThat((templateContext["pagination"] as Pagination).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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) + whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts) + verifyNoRedirect { + assertThat((templateContext["pagination"] as Pagination).items, contains(posts[2])) } } -- 2.7.4