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 public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
54 ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
55 if (newPostNotification != null) {
56 ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
57 int notificationIndex = notifications.indexOf(newPostNotification);
58 if (filteredNotification == null) {
59 notifications.remove(notificationIndex);
61 notifications.set(notificationIndex, filteredNotification);
64 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
65 if (newReplyNotification != null) {
66 ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
67 int notificationIndex = notifications.indexOf(newReplyNotification);
68 if (filteredNotification == null) {
69 notifications.remove(notificationIndex);
71 notifications.set(notificationIndex, filteredNotification);
78 * Filters the new posts of the given notification. If {@code currentSone}
79 * is {@code null}, {@code null} is returned and the notification is
80 * subsequently removed. Otherwise only posts that are posted by friend
81 * Sones of the given Sone are retained; all other posts are removed.
83 * @param newPostNotification
84 * The new-post notification
86 * The current Sone, or {@code null} if not logged in
87 * @return The filtered new-post notification, or {@code null} if the
88 * notification should be removed
90 public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
91 if (currentSone == null) {
94 List<Post> newPosts = new ArrayList<Post>();
95 for (Post post : newPostNotification.getElements()) {
96 if (isPostVisible(currentSone, post)) {
100 if (newPosts.isEmpty()) {
103 if (newPosts.size() == newPostNotification.getElements().size()) {
104 return newPostNotification;
106 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
107 filteredNotification.setElements(newPosts);
108 return filteredNotification;
112 * Filters the new replies of the given notification. If {@code currentSone}
113 * is {@code null}, {@code null} is returned and the notification is
114 * subsequently removed. Otherwise only replies that are replies to posts
115 * that are posted by friend Sones of the given Sone are retained; all other
116 * replies are removed.
118 * @param newReplyNotification
119 * The new-reply notification
121 * The current Sone, or {@code null} if not logged in
122 * @return The filtered new-reply notification, or {@code null} if the
123 * notification should be removed
125 public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
126 if (currentSone == null) {
129 List<Reply> newReplies = new ArrayList<Reply>();
130 for (Reply reply : newReplyNotification.getElements()) {
131 if (isPostVisible(currentSone, reply.getPost())) {
132 newReplies.add(reply);
135 if (newReplies.isEmpty()) {
138 if (newReplies.size() == newReplyNotification.getElements().size()) {
139 return newReplyNotification;
141 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
142 filteredNotification.setElements(newReplies);
143 return filteredNotification;
147 * Finds the notification with the given ID in the list of notifications and
151 * The type of the item in the notification
152 * @param notifications
153 * The notification to search
154 * @param notificationId
155 * The ID of the requested notification
156 * @param notificationElementClass
157 * The class of the notification item
158 * @return The requested notification, or {@code null} if no notification
159 * with the given ID could be found
161 @SuppressWarnings("unchecked")
162 private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
163 for (Notification notification : notifications) {
164 if (!notificationId.equals(notification.getId())) {
167 return (ListNotification<T>) notification;
173 * Checks whether a post is visible to the given Sone. A post is not
174 * considered visible if one of the following statements is true:
176 * <li>The post does not have a Sone.</li>
177 * <li>The Sone of the post is not the given Sone, the given Sone does not
178 * follow the post’s Sone, and the given Sone is not the recipient of the
180 * <li>The trust relationship between the two Sones can not be retrieved.</li>
181 * <li>The given Sone has explicitely assigned negative trust to the post’s
183 * <li>The given Sone has not explicitely assigned negative trust to the
184 * post’s Sone but the implicit trust is negative.</li>
185 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
187 * If none of these statements is true the post is considered visible.
190 * The Sone that checks for a post’s visibility
192 * The post to check for visibility
193 * @return {@code true} if the post is considered visible, {@code false}
196 public static boolean isPostVisible(Sone sone, Post post) {
197 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();
198 Sone postSone = post.getSone();
199 if (postSone == null) {
202 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
204 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
207 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
213 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
216 if (post.getTime() > System.currentTimeMillis()) {
223 * Checks whether a reply is visible to the given Sone. A reply is not
224 * considered visible if one of the following statements is true:
226 * <li>The reply does not have a post.</li>
227 * <li>The reply’s post does not have a Sone.</li>
228 * <li>The Sone of the reply’s post is not the given Sone, the given Sone
229 * does not follow the reply’s post’s Sone, and the given Sone is not the
230 * recipient of the reply’s post.</li>
231 * <li>The trust relationship between the two Sones can not be retrieved.</li>
232 * <li>The given Sone has explicitely assigned negative trust to the post’s
234 * <li>The given Sone has not explicitely assigned negative trust to the
235 * reply’s post’s Sone but the implicit trust is negative.</li>
236 * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
237 * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
239 * If none of these statements is true the reply is considered visible.
242 * The Sone that checks for a post’s visibility
244 * The reply to check for visibility
245 * @return {@code true} if the reply is considered visible, {@code false}
248 public static boolean isReplyVisible(Sone sone, Reply reply) {
249 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();
250 Post post = reply.getPost();
254 if (!isPostVisible(sone, post)) {
257 if (reply.getTime() > System.currentTimeMillis()) {