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