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