Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ViewSonePage.java
1 /*
2  * Sone - ViewSonePage.java - Copyright © 2010–2015 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
21
22 import java.net.URI;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.template.SoneAccessor;
35 import net.pterodactylus.sone.web.page.FreenetRequest;
36 import net.pterodactylus.util.collection.Pagination;
37 import net.pterodactylus.util.template.Template;
38 import net.pterodactylus.util.template.TemplateContext;
39
40 import com.google.common.base.Optional;
41
42 /**
43  * Lets the user browser another Sone.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class ViewSonePage extends SoneTemplatePage {
48
49         /**
50          * Creates a new “view Sone” page.
51          *
52          * @param template
53          *            The template to render
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public ViewSonePage(Template template, WebInterface webInterface) {
58                 super("viewSone.html", template, webInterface, false);
59         }
60
61         //
62         // TEMPLATEPAGE METHODS
63         //
64
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         protected String getPageTitle(FreenetRequest request) {
70                 String soneId = request.getHttpRequest().getParam("sone");
71                 Optional<Sone> sone = webInterface.getCore().getSone(soneId);
72                 if (sone.isPresent()) {
73                         String soneName = SoneAccessor.getNiceName(sone.get());
74                         return soneName + " - " + webInterface.getL10n().getString("Page.ViewSone.Title");
75                 }
76                 return webInterface.getL10n().getString("Page.ViewSone.Page.TitleWithoutSone");
77         }
78
79         /**
80          * {@inheritDoc}
81          */
82         @Override
83         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
84                 super.processTemplate(request, templateContext);
85                 String soneId = request.getHttpRequest().getParam("sone");
86                 Optional<Sone> sone = webInterface.getCore().getSone(soneId);
87                 templateContext.set("sone", sone.orNull());
88                 templateContext.set("soneId", soneId);
89                 if (!sone.isPresent()) {
90                         return;
91                 }
92                 List<Post> sonePosts = sone.get().getPosts();
93                 sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.get().getId()));
94                 Collections.sort(sonePosts, Post.TIME_COMPARATOR);
95                 Pagination<Post> postPagination = new Pagination<Post>(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("postPage"), 0));
96                 templateContext.set("postPagination", postPagination);
97                 templateContext.set("posts", postPagination.getItems());
98                 Set<PostReply> replies = sone.get().getReplies();
99                 final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
100                 for (PostReply reply : replies) {
101                         Optional<Post> post = reply.getPost();
102                         if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.get().equals(post.get().getSone()) || (sone.get().getId().equals(post.get().getRecipientId().orNull()))) {
103                                 continue;
104                         }
105                         repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get().getId()));
106                 }
107                 List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
108                 Collections.sort(posts, new Comparator<Post>() {
109
110                         @Override
111                         public int compare(Post leftPost, Post rightPost) {
112                                 return (int) Math.min(Integer.MAX_VALUE, Math.max(Integer.MIN_VALUE, repliedPosts.get(rightPost).get(0).getTime() - repliedPosts.get(leftPost).get(0).getTime()));
113                         }
114
115                 });
116
117                 Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(parseInt(request.getHttpRequest().getParam("repliedPostPage"), 0));
118                 templateContext.set("repliedPostPagination", repliedPostPagination);
119                 templateContext.set("repliedPosts", repliedPostPagination.getItems());
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public boolean isLinkExcepted(URI link) {
127                 return true;
128         }
129
130 }