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, true);
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);
67 } else if (notification.getId().equals("mention-notification")) {
68 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null, false);
69 if (filteredNotification != null) {
70 filteredNotifications.add(filteredNotification);
73 filteredNotifications.add(notification);
76 return filteredNotifications;
80 * Filters the new posts of the given notification. If {@code currentSone}
81 * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
82 * returned and the notification is subsequently removed. Otherwise only
83 * posts that are posted by friend Sones of the given Sone are retained; all
84 * other posts are removed.
86 * @param newPostNotification
87 * The new-post notification
89 * The current Sone, or {@code null} if not logged in
91 * Whether a non-{@code null} Sone in {@code currentSone} is
93 * @return The filtered new-post notification, or {@code null} if the
94 * notification should be removed
96 public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone, boolean soneRequired) {
97 if (soneRequired && (currentSone == null)) {
100 List<Post> newPosts = new ArrayList<Post>();
101 for (Post post : newPostNotification.getElements()) {
102 if (isPostVisible(currentSone, post)) {
106 if (newPosts.isEmpty()) {
109 if (newPosts.size() == newPostNotification.getElements().size()) {
110 return newPostNotification;
112 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
113 filteredNotification.setElements(newPosts);
114 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
115 return filteredNotification;
119 * Filters the new replies of the given notification. If {@code currentSone}
120 * is {@code null}, {@code null} is returned and the notification is
121 * subsequently removed. Otherwise only replies that are replies to posts
122 * that are posted by friend Sones of the given Sone are retained; all other
123 * replies are removed.
125 * @param newReplyNotification
126 * The new-reply notification
128 * The current Sone, or {@code null} if not logged in
129 * @return The filtered new-reply notification, or {@code null} if the
130 * notification should be removed
132 public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
133 if (currentSone == null) {
136 List<Reply> newReplies = new ArrayList<Reply>();
137 for (Reply reply : newReplyNotification.getElements()) {
138 if (isReplyVisible(currentSone, reply)) {
139 newReplies.add(reply);
142 if (newReplies.isEmpty()) {
145 if (newReplies.size() == newReplyNotification.getElements().size()) {
146 return newReplyNotification;
148 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
149 filteredNotification.setElements(newReplies);
150 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
151 return filteredNotification;
155 * Checks whether a post is visible to the given Sone. A post is not
156 * considered visible if one of the following statements is true:
158 * <li>The post does not have a Sone.</li>
159 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
162 * If {@code post} is not {@code null} more checks are performed, and the
163 * post will be invisible if:
166 * <li>The Sone of the post is not the given Sone, the given Sone does not
167 * follow the post’s Sone, and the given Sone is not the recipient of the
169 * <li>The trust relationship between the two Sones can not be retrieved.</li>
170 * <li>The given Sone has explicitely assigned negative trust to the post’s
172 * <li>The given Sone has not explicitely assigned negative trust to the
173 * post’s Sone but the implicit trust is negative.</li>
175 * If none of these statements is true the post is considered visible.
178 * The Sone that checks for a post’s visibility (may be
179 * {@code null} to skip Sone-specific checks, such as trust)
181 * The post to check for visibility
182 * @return {@code true} if the post is considered visible, {@code false}
185 public static boolean isPostVisible(Sone sone, Post post) {
186 Validation.begin().isNotNull("Post", post).check();
187 Sone postSone = post.getSone();
188 if (postSone == null) {
192 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
194 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
197 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
203 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
207 if (post.getTime() > System.currentTimeMillis()) {
214 * Checks whether a reply is visible to the given Sone. A reply is not
215 * considered visible if one of the following statements is true:
217 * <li>The reply does not have a post.</li>
218 * <li>The reply’s post does not have a Sone.</li>
219 * <li>The Sone of the reply’s post is not the given Sone, the given Sone
220 * does not follow the reply’s post’s Sone, and the given Sone is not the
221 * recipient of the reply’s post.</li>
222 * <li>The trust relationship between the two Sones can not be retrieved.</li>
223 * <li>The given Sone has explicitely assigned negative trust to the post’s
225 * <li>The given Sone has not explicitely assigned negative trust to the
226 * reply’s post’s Sone but the implicit trust is negative.</li>
227 * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
228 * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
230 * If none of these statements is true the reply is considered visible.
233 * The Sone that checks for a post’s visibility (may be
234 * {@code null} to skip Sone-specific checks, such as trust)
236 * The reply to check for visibility
237 * @return {@code true} if the reply is considered visible, {@code false}
240 public static boolean isReplyVisible(Sone sone, Reply reply) {
241 Validation.begin().isNotNull("Reply", reply).check();
242 Post post = reply.getPost();
246 if (!isPostVisible(sone, post)) {
249 if (reply.getTime() > System.currentTimeMillis()) {