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);
67 } else if (notification.getId().equals("mention-notification")) {
68 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null);
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}, {@code null} is returned and the notification is
82 * subsequently removed. Otherwise only posts that are posted by friend
83 * Sones of the given Sone are retained; all other posts are removed.
85 * @param newPostNotification
86 * The new-post notification
88 * The current Sone, or {@code null} if not logged in
89 * @return The filtered new-post notification, or {@code null} if the
90 * notification should be removed
92 public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
93 if (currentSone == null) {
96 List<Post> newPosts = new ArrayList<Post>();
97 for (Post post : newPostNotification.getElements()) {
98 if (isPostVisible(currentSone, post)) {
102 if (newPosts.isEmpty()) {
105 if (newPosts.size() == newPostNotification.getElements().size()) {
106 return newPostNotification;
108 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
109 filteredNotification.setElements(newPosts);
110 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
111 return filteredNotification;
115 * Filters the new replies of the given notification. If {@code currentSone}
116 * is {@code null}, {@code null} is returned and the notification is
117 * subsequently removed. Otherwise only replies that are replies to posts
118 * that are posted by friend Sones of the given Sone are retained; all other
119 * replies are removed.
121 * @param newReplyNotification
122 * The new-reply notification
124 * The current Sone, or {@code null} if not logged in
125 * @return The filtered new-reply notification, or {@code null} if the
126 * notification should be removed
128 public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
129 if (currentSone == null) {
132 List<Reply> newReplies = new ArrayList<Reply>();
133 for (Reply reply : newReplyNotification.getElements()) {
134 if (isReplyVisible(currentSone, reply)) {
135 newReplies.add(reply);
138 if (newReplies.isEmpty()) {
141 if (newReplies.size() == newReplyNotification.getElements().size()) {
142 return newReplyNotification;
144 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
145 filteredNotification.setElements(newReplies);
146 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
147 return filteredNotification;
151 * Checks whether a post is visible to the given Sone. A post is not
152 * considered visible if one of the following statements is true:
154 * <li>The post does not have a Sone.</li>
155 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
158 * If {@code post} is not {@code null} more checks are performed, and the
159 * post will be invisible if:
162 * <li>The Sone of the post is not the given Sone, the given Sone does not
163 * follow the post’s Sone, and the given Sone is not the recipient of the
165 * <li>The trust relationship between the two Sones can not be retrieved.</li>
166 * <li>The given Sone has explicitely assigned negative trust to the post’s
168 * <li>The given Sone has not explicitely assigned negative trust to the
169 * post’s Sone but the implicit trust is negative.</li>
171 * If none of these statements is true the post is considered visible.
174 * The Sone that checks for a post’s visibility (may be
175 * {@code null} to skip Sone-specific checks, such as trust)
177 * The post to check for visibility
178 * @return {@code true} if the post is considered visible, {@code false}
181 public static boolean isPostVisible(Sone sone, Post post) {
182 Validation.begin().isNotNull("Post", post).check();
183 Sone postSone = post.getSone();
184 if (postSone == null) {
188 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
190 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
193 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
199 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
203 if (post.getTime() > System.currentTimeMillis()) {
210 * Checks whether a reply is visible to the given Sone. A reply is not
211 * considered visible if one of the following statements is true:
213 * <li>The reply does not have a post.</li>
214 * <li>The reply’s post does not have a Sone.</li>
215 * <li>The Sone of the reply’s post is not the given Sone, the given Sone
216 * does not follow the reply’s post’s Sone, and the given Sone is not the
217 * recipient of the reply’s post.</li>
218 * <li>The trust relationship between the two Sones can not be retrieved.</li>
219 * <li>The given Sone has explicitely assigned negative trust to the post’s
221 * <li>The given Sone has not explicitely assigned negative trust to the
222 * reply’s post’s Sone but the implicit trust is negative.</li>
223 * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
224 * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
226 * If none of these statements is true the reply is considered visible.
229 * The Sone that checks for a post’s visibility (may be
230 * {@code null} to skip Sone-specific checks, such as trust)
232 * The reply to check for visibility
233 * @return {@code true} if the reply is considered visible, {@code false}
236 public static boolean isReplyVisible(Sone sone, Reply reply) {
237 Validation.begin().isNotNull("Reply", reply).check();
238 Post post = reply.getPost();
242 if (!isPostVisible(sone, post)) {
245 if (reply.getTime() > System.currentTimeMillis()) {