2 * Sone - ListNotificationFilters.java - Copyright © 2010 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.notify;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
28 import net.pterodactylus.sone.freenet.wot.Trust;
29 import net.pterodactylus.util.notify.Notification;
30 import net.pterodactylus.util.validation.Validation;
33 * Filter for {@link ListNotification}s.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class ListNotificationFilters {
40 * Filters new-post and new-reply notifications in the given list of
41 * notifications. If {@code currentSone} is <code>null</code>, new-post and
42 * new-reply notifications are removed completely. If {@code currentSone} is
43 * not {@code null}, only posts that are posted by a friend Sone or the Sone
44 * itself, and replies that are replies to posts of friend Sones or the Sone
45 * itself will be retained in the notifications.
47 * @param notifications
48 * The notifications to filter
50 * The current Sone, or {@code null} if not logged in
51 * @return The filtered notifications
53 @SuppressWarnings("unchecked")
54 public static List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
55 List<Notification> filteredNotifications = new ArrayList<Notification>();
56 for (Notification notification : notifications) {
57 if (notification.getId().equals("new-post-notification")) {
58 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone);
59 if (filteredNotification != null) {
60 filteredNotifications.add(filteredNotification);
62 } else if (notification.getId().equals("new-reply-notification")) {
63 ListNotification<Reply> filteredNotification = filterNewReplyNotification((ListNotification<Reply>) notification, currentSone);
64 if (filteredNotification != null) {
65 filteredNotifications.add(filteredNotification);
68 filteredNotifications.add(notification);
71 return filteredNotifications;
75 * Filters the new posts of the given notification. If {@code currentSone}
76 * is {@code null}, {@code null} is returned and the notification is
77 * subsequently removed. Otherwise only posts that are posted by friend
78 * Sones of the given Sone are retained; all other posts are removed.
80 * @param newPostNotification
81 * The new-post notification
83 * The current Sone, or {@code null} if not logged in
84 * @return The filtered new-post notification, or {@code null} if the
85 * notification should be removed
87 public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
88 if (currentSone == null) {
91 List<Post> newPosts = new ArrayList<Post>();
92 for (Post post : newPostNotification.getElements()) {
93 if (isPostVisible(currentSone, post)) {
97 if (newPosts.isEmpty()) {
100 if (newPosts.size() == newPostNotification.getElements().size()) {
101 return newPostNotification;
103 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
104 filteredNotification.setElements(newPosts);
105 return filteredNotification;
109 * Filters the new replies of the given notification. If {@code currentSone}
110 * is {@code null}, {@code null} is returned and the notification is
111 * subsequently removed. Otherwise only replies that are replies to posts
112 * that are posted by friend Sones of the given Sone are retained; all other
113 * replies are removed.
115 * @param newReplyNotification
116 * The new-reply notification
118 * The current Sone, or {@code null} if not logged in
119 * @return The filtered new-reply notification, or {@code null} if the
120 * notification should be removed
122 public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
123 if (currentSone == null) {
126 List<Reply> newReplies = new ArrayList<Reply>();
127 for (Reply reply : newReplyNotification.getElements()) {
128 if (isReplyVisible(currentSone, reply)) {
129 newReplies.add(reply);
132 if (newReplies.isEmpty()) {
135 if (newReplies.size() == newReplyNotification.getElements().size()) {
136 return newReplyNotification;
138 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
139 filteredNotification.setElements(newReplies);
140 return filteredNotification;
144 * Checks whether a post is visible to the given Sone. A post is not
145 * considered visible if one of the following statements is true:
147 * <li>The post does not have a Sone.</li>
148 * <li>The Sone of the post is not the given Sone, the given Sone does not
149 * follow the post’s Sone, and the given Sone is not the recipient of the
151 * <li>The trust relationship between the two Sones can not be retrieved.</li>
152 * <li>The given Sone has explicitely assigned negative trust to the post’s
154 * <li>The given Sone has not explicitely assigned negative trust to the
155 * post’s Sone but the implicit trust is negative.</li>
156 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
158 * If none of these statements is true the post is considered visible.
161 * The Sone that checks for a post’s visibility
163 * The post to check for visibility
164 * @return {@code true} if the post is considered visible, {@code false}
167 public static boolean isPostVisible(Sone sone, Post post) {
168 Validation.begin().isNotNull("Sone", sone).isNotNull("Post", post).check().isNotNull("Sone’s Identity", sone.getIdentity()).check().isInstanceOf("Sone’s Identity", sone.getIdentity(), OwnIdentity.class).check();
169 Sone postSone = post.getSone();
170 if (postSone == null) {
173 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
175 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
178 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
184 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
187 if (post.getTime() > System.currentTimeMillis()) {
194 * Checks whether a reply is visible to the given Sone. A reply is not
195 * considered visible if one of the following statements is true:
197 * <li>The reply does not have a post.</li>
198 * <li>The reply’s post does not have a Sone.</li>
199 * <li>The Sone of the reply’s post is not the given Sone, the given Sone
200 * does not follow the reply’s post’s Sone, and the given Sone is not the
201 * recipient of the reply’s post.</li>
202 * <li>The trust relationship between the two Sones can not be retrieved.</li>
203 * <li>The given Sone has explicitely assigned negative trust to the post’s
205 * <li>The given Sone has not explicitely assigned negative trust to the
206 * reply’s post’s Sone but the implicit trust is negative.</li>
207 * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
208 * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
210 * If none of these statements is true the reply is considered visible.
213 * The Sone that checks for a post’s visibility
215 * The reply to check for visibility
216 * @return {@code true} if the reply is considered visible, {@code false}
219 public static boolean isReplyVisible(Sone sone, Reply reply) {
220 Validation.begin().isNotNull("Sone", sone).isNotNull("Reply", reply).check().isNotNull("Sone’s Identity", sone.getIdentity()).check().isInstanceOf("Sone’s Identity", sone.getIdentity(), OwnIdentity.class).check();
221 Post post = reply.getPost();
225 if (!isPostVisible(sone, post)) {
228 if (reply.getTime() > System.currentTimeMillis()) {