2 * Sone - KnownSonesPage.java - Copyright © 2010–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.pages;
20 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.web.WebInterface;
29 import net.pterodactylus.sone.web.page.FreenetRequest;
30 import net.pterodactylus.util.collection.Pagination;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
34 import com.google.common.base.Predicate;
35 import com.google.common.base.Predicates;
36 import com.google.common.collect.Collections2;
37 import com.google.common.collect.Ordering;
40 * This page shows all known Sones.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class KnownSonesPage extends SoneTemplatePage {
46 private static final String defaultSortField = "activity";
47 private static final String defaultSortOrder = "desc";
50 * Creates a “known Sones” page.
53 * The template to render
55 * The Sone web interface
57 public KnownSonesPage(Template template, WebInterface webInterface) {
58 super("knownSones.html", template, "Page.KnownSones.Title", webInterface, false);
62 // TEMPLATEPAGE METHODS
69 protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
70 String sortField = request.getHttpRequest().getParam("sort", defaultSortField);
71 String sortOrder = request.getHttpRequest().getParam("order", defaultSortOrder);
72 String filter = request.getHttpRequest().getParam("filter");
73 templateContext.set("sort", sortField);
74 templateContext.set("order", sortOrder);
75 templateContext.set("filter", filter);
76 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
77 Collection<Sone> knownSones = Collections2.filter(webInterface.getCore().getSones(), Sone.EMPTY_SONE_FILTER);
78 if ((currentSone != null) && "followed".equals(filter)) {
79 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
82 public boolean apply(Sone sone) {
83 return currentSone.hasFriend(sone.getId());
86 } else if ((currentSone != null) && "not-followed".equals(filter)) {
87 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
90 public boolean apply(Sone sone) {
91 return !currentSone.hasFriend(sone.getId());
94 } else if ("new".equals(filter)) {
95 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
101 public boolean apply(Sone sone) {
102 return !sone.isKnown();
105 } else if ("not-new".equals(filter)) {
106 knownSones = Collections2.filter(knownSones, new Predicate<Sone>() {
112 public boolean apply(Sone sone) {
113 return sone.isKnown();
116 } else if ("own".equals(filter)) {
117 knownSones = Collections2.filter(knownSones, Sone.LOCAL_SONE_FILTER);
118 } else if ("not-own".equals(filter)) {
119 knownSones = Collections2.filter(knownSones, Predicates.not(Sone.LOCAL_SONE_FILTER));
121 List<Sone> sortedSones = new ArrayList<Sone>(knownSones);
122 if ("activity".equals(sortField)) {
123 if ("asc".equals(sortOrder)) {
124 Collections.sort(sortedSones, Ordering.from(Sone.LAST_ACTIVITY_COMPARATOR).reverse());
126 Collections.sort(sortedSones, Sone.LAST_ACTIVITY_COMPARATOR);
128 } else if ("posts".equals(sortField)) {
129 if ("asc".equals(sortOrder)) {
130 Collections.sort(sortedSones, Ordering.from(Sone.POST_COUNT_COMPARATOR).reverse());
132 Collections.sort(sortedSones, Sone.POST_COUNT_COMPARATOR);
134 } else if ("images".equals(sortField)) {
135 if ("asc".equals(sortOrder)) {
136 Collections.sort(sortedSones, Ordering.from(Sone.IMAGE_COUNT_COMPARATOR).reverse());
138 Collections.sort(sortedSones, Sone.IMAGE_COUNT_COMPARATOR);
141 if ("desc".equals(sortOrder)) {
142 Collections.sort(sortedSones, Ordering.from(Sone.NICE_NAME_COMPARATOR).reverse());
144 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
147 Pagination<Sone> sonePagination = new Pagination<Sone>(sortedSones, 25).setPage(parseInt(request.getHttpRequest().getParam("page"), 0));
148 templateContext.set("pagination", sonePagination);
149 templateContext.set("knownSones", sonePagination.getItems());