Filter notifications that the user is not interested in.
[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.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") && ((currentSone == null) || (currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get()))) {
59                                 filteredNotifications.add(notification);
60                         } else if (notification.getId().equals("new-post-notification") && ((currentSone == null) || (currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get()))) {
61                                 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone, true);
62                                 if (filteredNotification != null) {
63                                         filteredNotifications.add(filteredNotification);
64                                 }
65                         } else if (notification.getId().equals("new-reply-notification") && ((currentSone == null) || (currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get()))) {
66                                 ListNotification<PostReply> filteredNotification = filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
67                                 if (filteredNotification != null) {
68                                         filteredNotifications.add(filteredNotification);
69                                 }
70                         } else if (notification.getId().equals("mention-notification")) {
71                                 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, null, false);
72                                 if (filteredNotification != null) {
73                                         filteredNotifications.add(filteredNotification);
74                                 }
75                         } else {
76                                 filteredNotifications.add(notification);
77                         }
78                 }
79                 return filteredNotifications;
80         }
81
82         /**
83          * Filters the new posts of the given notification. If {@code currentSone}
84          * is {@code null} and {@code soneRequired} is {@code true}, {@code null} is
85          * returned and the notification is subsequently removed. Otherwise only
86          * posts that are posted by friend Sones of the given Sone are retained; all
87          * other posts are removed.
88          *
89          * @param newPostNotification
90          *            The new-post notification
91          * @param currentSone
92          *            The current Sone, or {@code null} if not logged in
93          * @param soneRequired
94          *            Whether a non-{@code null} Sone in {@code currentSone} is
95          *            required
96          * @return The filtered new-post notification, or {@code null} if the
97          *         notification should be removed
98          */
99         public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone, boolean soneRequired) {
100                 if (soneRequired && (currentSone == null)) {
101                         return null;
102                 }
103                 List<Post> newPosts = new ArrayList<Post>();
104                 for (Post post : newPostNotification.getElements()) {
105                         if (isPostVisible(currentSone, post)) {
106                                 newPosts.add(post);
107                         }
108                 }
109                 if (newPosts.isEmpty()) {
110                         return null;
111                 }
112                 if (newPosts.size() == newPostNotification.getElements().size()) {
113                         return newPostNotification;
114                 }
115                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
116                 filteredNotification.setElements(newPosts);
117                 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
118                 return filteredNotification;
119         }
120
121         /**
122          * Filters the new replies of the given notification. If {@code currentSone}
123          * is {@code null}, {@code null} is returned and the notification is
124          * subsequently removed. Otherwise only replies that are replies to posts
125          * that are posted by friend Sones of the given Sone are retained; all other
126          * replies are removed.
127          *
128          * @param newReplyNotification
129          *            The new-reply notification
130          * @param currentSone
131          *            The current Sone, or {@code null} if not logged in
132          * @return The filtered new-reply notification, or {@code null} if the
133          *         notification should be removed
134          */
135         public static ListNotification<PostReply> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification, Sone currentSone) {
136                 if (currentSone == null) {
137                         return null;
138                 }
139                 List<PostReply> newReplies = new ArrayList<PostReply>();
140                 for (PostReply reply : newReplyNotification.getElements()) {
141                         if (isReplyVisible(currentSone, reply)) {
142                                 newReplies.add(reply);
143                         }
144                 }
145                 if (newReplies.isEmpty()) {
146                         return null;
147                 }
148                 if (newReplies.size() == newReplyNotification.getElements().size()) {
149                         return newReplyNotification;
150                 }
151                 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
152                 filteredNotification.setElements(newReplies);
153                 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
154                 return filteredNotification;
155         }
156
157         /**
158          * Checks whether a post is visible to the given Sone. A post is not
159          * considered visible if one of the following statements is true:
160          * <ul>
161          * <li>The post does not have a Sone.</li>
162          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
163          * </ul>
164          * <p>
165          * If {@code post} is not {@code null} more checks are performed, and the
166          * post will be invisible if:
167          * </p>
168          * <ul>
169          * <li>The Sone of the post is not the given Sone, the given Sone does not
170          * follow the post’s Sone, and the given Sone is not the recipient of the
171          * post.</li>
172          * <li>The trust relationship between the two Sones can not be retrieved.</li>
173          * <li>The given Sone has explicitely assigned negative trust to the post’s
174          * Sone.</li>
175          * <li>The given Sone has not explicitely assigned negative trust to the
176          * post’s Sone but the implicit trust is negative.</li>
177          * </ul>
178          * If none of these statements is true the post is considered visible.
179          *
180          * @param sone
181          *            The Sone that checks for a post’s visibility (may be
182          *            {@code null} to skip Sone-specific checks, such as trust)
183          * @param post
184          *            The post to check for visibility
185          * @return {@code true} if the post is considered visible, {@code false}
186          *         otherwise
187          */
188         public static boolean isPostVisible(Sone sone, Post post) {
189                 Validation.begin().isNotNull("Post", post).check();
190                 Sone postSone = post.getSone();
191                 if (postSone == null) {
192                         return false;
193                 }
194                 if (sone != null) {
195                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
196                         if (trust != null) {
197                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
198                                         return false;
199                                 }
200                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
201                                         return false;
202                                 }
203                         } else {
204                                 return false;
205                         }
206                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
207                                 return false;
208                         }
209                 }
210                 if (post.getTime() > System.currentTimeMillis()) {
211                         return false;
212                 }
213                 return true;
214         }
215
216         /**
217          * Checks whether a reply is visible to the given Sone. A reply is not
218          * considered visible if one of the following statements is true:
219          * <ul>
220          * <li>The reply does not have a post.</li>
221          * <li>The reply’s post does not have a Sone.</li>
222          * <li>The Sone of the reply’s post is not the given Sone, the given Sone
223          * does not follow the reply’s post’s Sone, and the given Sone is not the
224          * recipient of the reply’s post.</li>
225          * <li>The trust relationship between the two Sones can not be retrieved.</li>
226          * <li>The given Sone has explicitely assigned negative trust to the post’s
227          * Sone.</li>
228          * <li>The given Sone has not explicitely assigned negative trust to the
229          * reply’s post’s Sone but the implicit trust is negative.</li>
230          * <li>The reply’s post’s {@link Post#getTime() time} is in the future.</li>
231          * <li>The reply’s {@link Reply#getTime() time} is in the future.</li>
232          * </ul>
233          * If none of these statements is true the reply is considered visible.
234          *
235          * @param sone
236          *            The Sone that checks for a post’s visibility (may be
237          *            {@code null} to skip Sone-specific checks, such as trust)
238          * @param reply
239          *            The reply to check for visibility
240          * @return {@code true} if the reply is considered visible, {@code false}
241          *         otherwise
242          */
243         public static boolean isReplyVisible(Sone sone, PostReply reply) {
244                 Validation.begin().isNotNull("Reply", reply).check();
245                 Post post = reply.getPost();
246                 if (post == null) {
247                         return false;
248                 }
249                 if (!isPostVisible(sone, post)) {
250                         return false;
251                 }
252                 if (reply.getTime() > System.currentTimeMillis()) {
253                         return false;
254                 }
255                 return true;
256         }
257
258 }