Assume a post is visible if the trust updater has not received the trust value yet.
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / ListNotificationFilters.java
1 /*
2  * Sone - ListNotificationFilters.java - Copyright © 2010–2012 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.PostReply;
26 import net.pterodactylus.sone.data.Reply;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
29 import net.pterodactylus.sone.freenet.wot.Trust;
30 import net.pterodactylus.util.notify.Notification;
31 import net.pterodactylus.util.validation.Validation;
32
33 /**
34  * Filter for {@link ListNotification}s.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class ListNotificationFilters {
39
40         /**
41          * Filters new-post and new-reply notifications in the given list of
42          * notifications. If {@code currentSone} is <code>null</code>, new-post and
43          * new-reply notifications are removed completely. If {@code currentSone} is
44          * not {@code null}, only posts that are posted by a friend Sone or the Sone
45          * itself, and replies that are replies to posts of friend Sones or the Sone
46          * itself will be retained in the notifications.
47          *
48          * @param notifications
49          *            The notifications to filter
50          * @param currentSone
51          *            The current Sone, or {@code null} if not logged in
52          * @return The filtered notifications
53          */
54         @SuppressWarnings("unchecked")
55         public static List<Notification> filterNotifications(Collection<? extends Notification> notifications, Sone currentSone) {
56                 List<Notification> filteredNotifications = new ArrayList<Notification>();
57                 for (Notification notification : notifications) {
58                         if (notification.getId().equals("new-sone-notification")) {
59                                 if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get())) {
60                                         continue;
61                                 }
62                                 filteredNotifications.add(notification);
63                         } else if (notification.getId().equals("new-post-notification")) {
64                                 if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get())) {
65                                         continue;
66                                 }
67                                 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone, true);
68                                 if (filteredNotification != null) {
69                                         filteredNotifications.add(filteredNotification);
70                                 }
71                         } else if (notification.getId().equals("new-reply-notification")) {
72                                 if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get())) {
73                                         continue;
74                                 }
75                                 ListNotification<PostReply> filteredNotification = filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
76                                 if (filteredNotification != null) {
77                                         filteredNotifications.add(filteredNotification);
78                                 }
79                         } else if (notification.getId().equals("mention-notification")) {
80                                 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null, false);
81                                 if (filteredNotification != null) {
82                                         filteredNotifications.add(filteredNotification);
83                                 }
84                         } else {
85                                 filteredNotifications.add(notification);
86                         }
87                 }
88                 return filteredNotifications;
89         }
90
91         /**
92          * Filters the new posts of the given notification. If {@code currentSone}
93          * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
94          * returned and the notification is subsequently removed. Otherwise only
95          * posts that are posted by friend Sones of the given Sone are retained; all
96          * other posts are removed.
97          *
98          * @param newPostNotification
99          *            The new-post notification
100          * @param currentSone
101          *            The current Sone, or {@code null} if not logged in
102          * @param soneRequired
103          *            Whether a non-{@code null} Sone in {@code currentSone} is
104          *            required
105          * @return The filtered new-post notification, or {@code null} if the
106          *         notification should be removed
107          */
108         public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone, boolean soneRequired) {
109                 if (soneRequired && (currentSone == null)) {
110                         return null;
111                 }
112                 List<Post> newPosts = new ArrayList<Post>();
113                 for (Post post : newPostNotification.getElements()) {
114                         if (isPostVisible(currentSone, post)) {
115                                 newPosts.add(post);
116                         }
117                 }
118                 if (newPosts.isEmpty()) {
119                         return null;
120                 }
121                 if (newPosts.size() == newPostNotification.getElements().size()) {
122                         return newPostNotification;
123                 }
124                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
125                 filteredNotification.setElements(newPosts);
126                 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
127                 return filteredNotification;
128         }
129
130         /**
131          * Filters the new replies of the given notification. If {@code currentSone}
132          * is {@code null}, {@code null} is returned and the notification is
133          * subsequently removed. Otherwise only replies that are replies to posts
134          * that are posted by friend Sones of the given Sone are retained; all other
135          * replies are removed.
136          *
137          * @param newReplyNotification
138          *            The new-reply notification
139          * @param currentSone
140          *            The current Sone, or {@code null} if not logged in
141          * @return The filtered new-reply notification, or {@code null} if the
142          *         notification should be removed
143          */
144         public static ListNotification<PostReply> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification, Sone currentSone) {
145                 if (currentSone == null) {
146                         return null;
147                 }
148                 List<PostReply> newReplies = new ArrayList<PostReply>();
149                 for (PostReply reply : newReplyNotification.getElements()) {
150                         if (isReplyVisible(currentSone, reply)) {
151                                 newReplies.add(reply);
152                         }
153                 }
154                 if (newReplies.isEmpty()) {
155                         return null;
156                 }
157                 if (newReplies.size() == newReplyNotification.getElements().size()) {
158                         return newReplyNotification;
159                 }
160                 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
161                 filteredNotification.setElements(newReplies);
162                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
163                 return filteredNotification;
164         }
165
166         /**
167          * Filters the given posts, using {@link #isPostVisible(Sone, Post)} to
168          * decide whether a post should be contained in the returned list. If
169          * {@code currentSone} is not {@code null} it is used to filter out posts
170          * that are from Sones that are not followed or not trusted by the given
171          * Sone.
172          *
173          * @param posts
174          *            The posts to filter
175          * @param currentSone
176          *            The current Sone (may be {@code null})
177          * @return The filtered posts
178          */
179         public static List<Post> filterPosts(Collection<Post> posts, Sone currentSone) {
180                 List<Post> filteredPosts = new ArrayList<Post>();
181                 for (Post post : posts) {
182                         if (isPostVisible(currentSone, post)) {
183                                 filteredPosts.add(post);
184                         }
185                 }
186                 return filteredPosts;
187         }
188
189         /**
190          * Checks whether a post is visible to the given Sone. A post is not
191          * considered visible if one of the following statements is true:
192          * <ul>
193          * <li>The post does not have a Sone.</li>
194          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
195          * </ul>
196          * <p>
197          * If {@code post} is not {@code null} more checks are performed, and the
198          * post will be invisible if:
199          * </p>
200          * <ul>
201          * <li>The Sone of the post is not the given Sone, the given Sone does not
202          * follow the post’s Sone, and the given Sone is not the recipient of the
203          * post.</li>
204          * <li>The trust relationship between the two Sones can not be retrieved.</li>
205          * <li>The given Sone has explicitely assigned negative trust to the post’s
206          * Sone.</li>
207          * <li>The given Sone has not explicitely assigned negative trust to the
208          * post’s Sone but the implicit trust is negative.</li>
209          * </ul>
210          * If none of these statements is true the post is considered visible.
211          *
212          * @param sone
213          *            The Sone that checks for a post’s visibility (may be
214          *            {@code null} to skip Sone-specific checks, such as trust)
215          * @param post
216          *            The post to check for visibility
217          * @return {@code true} if the post is considered visible, {@code false}
218          *         otherwise
219          */
220         public static boolean isPostVisible(Sone sone, Post post) {
221                 Validation.begin().isNotNull("Post", post).check();
222                 Sone postSone = post.getSone();
223                 if (postSone == null) {
224                         return false;
225                 }
226                 if (sone != null) {
227                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
228                         if (trust != null) {
229                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
230                                         return false;
231                                 }
232                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
233                                         return false;
234                                 }
235                         } else {
236                                 /*
237                                  * a null trust means that the trust updater has not yet
238                                  * received a trust value for this relation. if we return false,
239                                  * the post feed will stay empty until the trust updater has
240                                  * received trust values. to prevent this we simply assume that
241                                  * posts are visible if there is no trust.
242                                  */
243                                 return true;
244                         }
245                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
246                                 return false;
247                         }
248                 }
249                 if (post.getTime() > System.currentTimeMillis()) {
250                         return false;
251                 }
252                 return true;
253         }
254
255         /**
256          * Checks whether a reply is visible to the given Sone. A reply is not
257          * considered visible if one of the following statements is true:
258          * <ul>
259          * <li>The reply does not have a post.</li>
260          * <li>The reply’s post does not have a Sone.</li>
261          * <li>The Sone of the reply’s post is not the given Sone, the given Sone
262          * does not follow the reply’s post’s Sone, and the given Sone is not the
263          * recipient of the reply’s post.</li>
264          * <li>The trust relationship between the two Sones can not be retrieved.</li>
265          * <li>The given Sone has explicitely assigned negative trust to the post’s
266          * Sone.</li>
267          * <li>The given Sone has not explicitely assigned negative trust to the
268          * reply’s post’s Sone but the implicit trust is negative.</li>
269          * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
270          * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
271          * </ul>
272          * If none of these statements is true the reply is considered visible.
273          *
274          * @param sone
275          *            The Sone that checks for a post’s visibility (may be
276          *            {@code null} to skip Sone-specific checks, such as trust)
277          * @param reply
278          *            The reply to check for visibility
279          * @return {@code true} if the reply is considered visible, {@code false}
280          *         otherwise
281          */
282         public static boolean isReplyVisible(Sone sone, PostReply reply) {
283                 Validation.begin().isNotNull("Reply", reply).check();
284                 Post post = reply.getPost();
285                 if (post == null) {
286                         return false;
287                 }
288                 if (!isPostVisible(sone, post)) {
289                         return false;
290                 }
291                 if (reply.getTime() > System.currentTimeMillis()) {
292                         return false;
293                 }
294                 return true;
295         }
296
297 }