Allow to skip Sone-specific checks in notification filters.
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilters.java
1 /*
2  * Sone - ListNotificationFilters.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.notify;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
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;
31
32 /**
33  * Filter for {@link ListNotification}s.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class ListNotificationFilters {
38
39         /**
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.
46          *
47          * @param notifications
48          *            The notifications to filter
49          * @param currentSone
50          *            The current Sone, or {@code null} if not logged in
51          * @return The filtered notifications
52          */
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);
61                                 }
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);
66                                 }
67                         } else {
68                                 filteredNotifications.add(notification);
69                         }
70                 }
71                 return filteredNotifications;
72         }
73
74         /**
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.
79          *
80          * @param newPostNotification
81          *            The new-post notification
82          * @param currentSone
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
86          */
87         public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
88                 if (currentSone == null) {
89                         return null;
90                 }
91                 List<Post> newPosts = new ArrayList<Post>();
92                 for (Post post : newPostNotification.getElements()) {
93                         if (isPostVisible(currentSone, post)) {
94                                 newPosts.add(post);
95                         }
96                 }
97                 if (newPosts.isEmpty()) {
98                         return null;
99                 }
100                 if (newPosts.size() == newPostNotification.getElements().size()) {
101                         return newPostNotification;
102                 }
103                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
104                 filteredNotification.setElements(newPosts);
105                 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
106                 return filteredNotification;
107         }
108
109         /**
110          * Filters the new replies of the given notification. If {@code currentSone}
111          * is {@code null}, {@code null} is returned and the notification is
112          * subsequently removed. Otherwise only replies that are replies to posts
113          * that are posted by friend Sones of the given Sone are retained; all other
114          * replies are removed.
115          *
116          * @param newReplyNotification
117          *            The new-reply notification
118          * @param currentSone
119          *            The current Sone, or {@code null} if not logged in
120          * @return The filtered new-reply notification, or {@code null} if the
121          *         notification should be removed
122          */
123         public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
124                 if (currentSone == null) {
125                         return null;
126                 }
127                 List<Reply> newReplies = new ArrayList<Reply>();
128                 for (Reply reply : newReplyNotification.getElements()) {
129                         if (isReplyVisible(currentSone, reply)) {
130                                 newReplies.add(reply);
131                         }
132                 }
133                 if (newReplies.isEmpty()) {
134                         return null;
135                 }
136                 if (newReplies.size() == newReplyNotification.getElements().size()) {
137                         return newReplyNotification;
138                 }
139                 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
140                 filteredNotification.setElements(newReplies);
141                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
142                 return filteredNotification;
143         }
144
145         /**
146          * Checks whether a post is visible to the given Sone. A post is not
147          * considered visible if one of the following statements is true:
148          * <ul>
149          * <li>The post does not have a Sone.</li>
150          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
151          * </ul>
152          * <p>
153          * If {@code post} is not {@code null} more checks are performed, and the
154          * post will be invisible if:
155          * </p>
156          * <ul>
157          * <li>The Sone of the post is not the given Sone, the given Sone does not
158          * follow the post’s Sone, and the given Sone is not the recipient of the
159          * post.</li>
160          * <li>The trust relationship between the two Sones can not be retrieved.</li>
161          * <li>The given Sone has explicitely assigned negative trust to the post’s
162          * Sone.</li>
163          * <li>The given Sone has not explicitely assigned negative trust to the
164          * post’s Sone but the implicit trust is negative.</li>
165          * </ul>
166          * If none of these statements is true the post is considered visible.
167          *
168          * @param sone
169          *            The Sone that checks for a post’s visibility (may be
170          *            {@code null} to skip Sone-specific checks, such as trust)
171          * @param post
172          *            The post to check for visibility
173          * @return {@code true} if the post is considered visible, {@code false}
174          *         otherwise
175          */
176         public static boolean isPostVisible(Sone sone, Post post) {
177                 Validation.begin().isNotNull("Post", post).check();
178                 Sone postSone = post.getSone();
179                 if (postSone == null) {
180                         return false;
181                 }
182                 if (sone != null) {
183                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
184                         if (trust != null) {
185                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
186                                         return false;
187                                 }
188                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
189                                         return false;
190                                 }
191                         } else {
192                                 return false;
193                         }
194                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
195                                 return false;
196                         }
197                 }
198                 if (post.getTime() > System.currentTimeMillis()) {
199                         return false;
200                 }
201                 return true;
202         }
203
204         /**
205          * Checks whether a reply is visible to the given Sone. A reply is not
206          * considered visible if one of the following statements is true:
207          * <ul>
208          * <li>The reply does not have a post.</li>
209          * <li>The reply’s post does not have a Sone.</li>
210          * <li>The Sone of the reply’s post is not the given Sone, the given Sone
211          * does not follow the reply’s post’s Sone, and the given Sone is not the
212          * recipient of the reply’s post.</li>
213          * <li>The trust relationship between the two Sones can not be retrieved.</li>
214          * <li>The given Sone has explicitely assigned negative trust to the post’s
215          * Sone.</li>
216          * <li>The given Sone has not explicitely assigned negative trust to the
217          * reply’s post’s Sone but the implicit trust is negative.</li>
218          * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
219          * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
220          * </ul>
221          * If none of these statements is true the reply is considered visible.
222          *
223          * @param sone
224          *            The Sone that checks for a post’s visibility (may be
225          *            {@code null} to skip Sone-specific checks, such as trust)
226          * @param reply
227          *            The reply to check for visibility
228          * @return {@code true} if the reply is considered visible, {@code false}
229          *         otherwise
230          */
231         public static boolean isReplyVisible(Sone sone, Reply reply) {
232                 Validation.begin().isNotNull("Reply", reply).check();
233                 Post post = reply.getPost();
234                 if (post == null) {
235                         return false;
236                 }
237                 if (!isPostVisible(sone, post)) {
238                         return false;
239                 }
240                 if (reply.getTime() > System.currentTimeMillis()) {
241                         return false;
242                 }
243                 return true;
244         }
245
246 }