X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FViewSonePage.java;h=2df911cac77abb73166171f6d00cb3ed041c2341;hp=4a12ed9b34c6cfc02d688566ee3984eea5db5b7d;hb=a47643aed43d118ca68044f95451bb5374cdb332;hpb=d94f4ab1052fc15248396a062c56ffd45bfefb85 diff --git a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index 4a12ed9..2df911c 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -1,5 +1,5 @@ /* - * Sone - ViewSonePage.java - Copyright © 2010 David Roden + * Sone - ViewSonePage.java - Copyright © 2010–2012 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 @@ -17,8 +17,24 @@ package net.pterodactylus.sone.web; +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.web.page.FreenetRequest; +import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; /** * Lets the user browser another Sone. @@ -36,7 +52,7 @@ public class ViewSonePage extends SoneTemplatePage { * The Sone web interface */ public ViewSonePage(Template template, WebInterface webInterface) { - super("viewSone.html", template, "Page.ViewSone.Title", webInterface); + super("viewSone.html", template, webInterface, false); } // @@ -47,22 +63,64 @@ public class ViewSonePage extends SoneTemplatePage { * {@inheritDoc} */ @Override - protected void processTemplate(Request request, Template template) throws RedirectException { - super.processTemplate(request, template); + protected String getPageTitle(FreenetRequest request) { String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.core().getSone(soneId); - template.set("sone", sone); + Sone sone = webInterface.getCore().getSone(soneId, false); + if ((sone != null) && (sone.getTime() > 0)) { + String soneName = SoneAccessor.getNiceName(sone); + return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title"); + } + return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone"); } - // - // SONETEMPLATEPAGE METHODS - // + /** + * {@inheritDoc} + */ + @Override + protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { + super.processTemplate(request, templateContext); + String soneId = request.getHttpRequest().getParam("sone"); + Sone sone = webInterface.getCore().getSone(soneId, false); + templateContext.set("sone", sone); + templateContext.set("soneId", soneId); + if (sone == null) { + return; + } + List sonePosts = sone.getPosts(); + sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone)); + Collections.sort(sonePosts, Post.TIME_COMPARATOR); + Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); + templateContext.set("postPagination", postPagination); + templateContext.set("posts", postPagination.getItems()); + Set replies = sone.getReplies(); + final Map> repliedPosts = new HashMap>(); + for (PostReply reply : replies) { + Post post = reply.getPost(); + if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) { + continue; + } + repliedPosts.put(post, webInterface.getCore().getReplies(post)); + } + 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()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0)); + templateContext.set("repliedPostPagination", repliedPostPagination); + templateContext.set("repliedPosts", repliedPostPagination.getItems()); + } /** * {@inheritDoc} */ @Override - protected boolean requiresLogin() { + public boolean isLinkExcepted(URI link) { return true; }