Filter mention notifications separately.
[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 if (notification.getId().equals("mention-notification")) {
68                                 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null);
69                                 if (filteredNotification != null) {
70                                         filteredNotifications.add(filteredNotification);
71                                 }
72                         } else {
73                                 filteredNotifications.add(notification);
74                         }
75                 }
76                 return filteredNotifications;
77         }
78
79         /**
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.
84          *
85          * @param newPostNotification
86          *            The new-post notification
87          * @param currentSone
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
91          */
92         public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
93                 if (currentSone == null) {
94                         return null;
95                 }
96                 List<Post> newPosts = new ArrayList<Post>();
97                 for (Post post : newPostNotification.getElements()) {
98                         if (isPostVisible(currentSone, post)) {
99                                 newPosts.add(post);
100                         }
101                 }
102                 if (newPosts.isEmpty()) {
103                         return null;
104                 }
105                 if (newPosts.size() == newPostNotification.getElements().size()) {
106                         return newPostNotification;
107                 }
108                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
109                 filteredNotification.setElements(newPosts);
110                 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
111                 return filteredNotification;
112         }
113
114         /**
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.
120          *
121          * @param newReplyNotification
122          *            The new-reply notification
123          * @param currentSone
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
127          */
128         public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
129                 if (currentSone == null) {
130                         return null;
131                 }
132                 List<Reply> newReplies = new ArrayList<Reply>();
133                 for (Reply reply : newReplyNotification.getElements()) {
134                         if (isReplyVisible(currentSone, reply)) {
135                                 newReplies.add(reply);
136                         }
137                 }
138                 if (newReplies.isEmpty()) {
139                         return null;
140                 }
141                 if (newReplies.size() == newReplyNotification.getElements().size()) {
142                         return newReplyNotification;
143                 }
144                 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
145                 filteredNotification.setElements(newReplies);
146                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
147                 return filteredNotification;
148         }
149
150         /**
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:
153          * <ul>
154          * <li>The post does not have a Sone.</li>
155          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
156          * </ul>
157          * <p>
158          * If {@code post} is not {@code null} more checks are performed, and the
159          * post will be invisible if:
160          * </p>
161          * <ul>
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
164          * post.</li>
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
167          * Sone.</li>
168          * <li>The given Sone has not explicitely assigned negative trust to the
169          * post’s Sone but the implicit trust is negative.</li>
170          * </ul>
171          * If none of these statements is true the post is considered visible.
172          *
173          * @param sone
174          *            The Sone that checks for a post’s visibility (may be
175          *            {@code null} to skip Sone-specific checks, such as trust)
176          * @param post
177          *            The post to check for visibility
178          * @return {@code true} if the post is considered visible, {@code false}
179          *         otherwise
180          */
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) {
185                         return false;
186                 }
187                 if (sone != null) {
188                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
189                         if (trust != null) {
190                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
191                                         return false;
192                                 }
193                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
194                                         return false;
195                                 }
196                         } else {
197                                 return false;
198                         }
199                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
200                                 return false;
201                         }
202                 }
203                 if (post.getTime() > System.currentTimeMillis()) {
204                         return false;
205                 }
206                 return true;
207         }
208
209         /**
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:
212          * <ul>
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
220          * Sone.</li>
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>
225          * </ul>
226          * If none of these statements is true the reply is considered visible.
227          *
228          * @param sone
229          *            The Sone that checks for a post’s visibility (may be
230          *            {@code null} to skip Sone-specific checks, such as trust)
231          * @param reply
232          *            The reply to check for visibility
233          * @return {@code true} if the reply is considered visible, {@code false}
234          *         otherwise
235          */
236         public static boolean isReplyVisible(Sone sone, Reply reply) {
237                 Validation.begin().isNotNull("Reply", reply).check();
238                 Post post = reply.getPost();
239                 if (post == null) {
240                         return false;
241                 }
242                 if (!isPostVisible(sone, post)) {
243                         return false;
244                 }
245                 if (reply.getTime() > System.currentTimeMillis()) {
246                         return false;
247                 }
248                 return true;
249         }
250
251 }