From 5096eaa5c9259972a07ba5fec125dc9103d727bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 5 Jun 2017 01:11:58 +0200 Subject: [PATCH] Replace view Sone page with Kotlin version --- .../pterodactylus/sone/web/pages/ViewSonePage.java | 132 --------------------- .../pterodactylus/sone/web/pages/ViewSonePage.kt | 58 +++++++++ 2 files changed, 58 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/pages/ViewSonePage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/pages/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/pages/ViewSonePage.java deleted file mode 100644 index f540ba2..0000000 --- a/src/main/java/net/pterodactylus/sone/web/pages/ViewSonePage.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Sone - ViewSonePage.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.net.URI; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -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.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.Optional; - -/** - * Lets the user browser another Sone. - * - * @author David ‘Bombe’ Roden - */ -public class ViewSonePage extends SoneTemplatePage { - - /** - * Creates a new “view Sone” page. - * - * @param template - * The template to render - * @param webInterface - * The Sone web interface - */ - public ViewSonePage(Template template, WebInterface webInterface) { - super("viewSone.html", template, webInterface, false); - } - - // - // TEMPLATEPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected String getPageTitle(FreenetRequest request) { - String soneId = request.getHttpRequest().getParam("sone"); - Optional sone = webInterface.getCore().getSone(soneId); - if (sone.isPresent()) { - String soneName = SoneAccessor.getNiceName(sone.get()); - return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title"); - } - return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone"); - } - - /** - * {@inheritDoc} - */ - @Override - protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { - String soneId = request.getHttpRequest().getParam("sone"); - Optional sone = webInterface.getCore().getSone(soneId); - templateContext.set("sone", sone.orNull()); - templateContext.set("soneId", soneId); - if (!sone.isPresent()) { - return; - } - List sonePosts = sone.get().getPosts(); - sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.get().getId())); - Collections.sort(sonePosts, Post.NEWEST_FIRST); - Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()); - postPagination.setPage(parseInt(request.getHttpRequest().getParam("postPage"), 0)); - templateContext.set("postPagination", postPagination); - templateContext.set("posts", postPagination.getItems()); - Set replies = sone.get().getReplies(); - final Map> repliedPosts = new HashMap>(); - for (PostReply reply : replies) { - Optional post = reply.getPost(); - if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.get().equals(post.get().getSone()) || (sone.get().getId().equals(post.get().getRecipientId().orNull()))) { - continue; - } - repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get().getId())); - } - List posts = new ArrayList(repliedPosts.keySet()); - Collections.sort(posts, new Comparator() { - - @Override - public int compare(Post leftPost, Post rightPost) { - return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime())); - } - - }); - - Pagination repliedPostPagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()); - repliedPostPagination.setPage(parseInt(request.getHttpRequest().getParam("repliedPostPage"), 0)); - templateContext.set("repliedPostPagination", repliedPostPagination); - templateContext.set("repliedPosts", repliedPostPagination.getItems()); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isLinkExcepted(URI link) { - return true; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt new file mode 100644 index 0000000..6903920 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ViewSonePage.kt @@ -0,0 +1,58 @@ +package net.pterodactylus.sone.web.pages + +import net.pterodactylus.sone.data.Post +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.template.SoneAccessor +import net.pterodactylus.sone.utils.let +import net.pterodactylus.sone.utils.mapPresent +import net.pterodactylus.sone.utils.paginate +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 +import java.net.URI + +/** + * Lets the user browser another Sone. + */ +class ViewSonePage(template: Template, webInterface: WebInterface): + SoneTemplatePage("viewSone.html", template, webInterface, false) { + + override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + templateContext["soneId"] = request.parameters["sone"] + request.parameters["sone"].let(webInterface.core::getSone).let { sone -> + templateContext["sone"] = sone + val sonePosts = sone.posts + val directedPosts = webInterface.core.getDirectedPosts(sone.id) + (sonePosts + directedPosts) + .sortedByDescending(Post::getTime) + .paginate(webInterface.core.preferences.postsPerPage) + .apply { page = request.parameters["postPage"]?.toIntOrNull() ?: 0 } + .also { + templateContext["postPagination"] = it + templateContext["posts"] = it.items + } + sone.replies + .mapPresent(PostReply::getPost) + .distinct() + .minus(sonePosts) + .minus(directedPosts) + .sortedByDescending { webInterface.core.getReplies(it.id).first().time } + .paginate(webInterface.core.preferences.postsPerPage) + .apply { page = request.parameters["repliedPostPage"]?.toIntOrNull() ?: 0 } + .also { + templateContext["repliedPostPagination"] = it + templateContext["repliedPosts"] = it.items + } + } + } + + override fun isLinkExcepted(link: URI?) = true + + public override fun getPageTitle(request: FreenetRequest): String = + request.parameters["sone"].let(webInterface.core::getSone).let { sone -> + "${SoneAccessor.getNiceName(sone)} - ${webInterface.l10n.getString("Page.ViewSone.Title")}" + } ?: webInterface.l10n.getString("Page.ViewSone.Page.TitleWithoutSone") + +} -- 2.7.4