Use method.
[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         public static List<Notification> filterNotifications(List<Notification> notifications, Sone currentSone) {
54                 ListNotification<Post> newPostNotification = getNotification(notifications, "new-post-notification", Post.class);
55                 if (newPostNotification != null) {
56                         ListNotification<Post> filteredNotification = filterNewPostNotification(newPostNotification, currentSone);
57                         int notificationIndex = notifications.indexOf(newPostNotification);
58                         if (filteredNotification == null) {
59                                 notifications.remove(notificationIndex);
60                         } else {
61                                 notifications.set(notificationIndex, filteredNotification);
62                         }
63                 }
64                 ListNotification<Reply> newReplyNotification = getNotification(notifications, "new-replies-notification", Reply.class);
65                 if (newReplyNotification != null) {
66                         ListNotification<Reply> filteredNotification = filterNewReplyNotification(newReplyNotification, currentSone);
67                         int notificationIndex = notifications.indexOf(newReplyNotification);
68                         if (filteredNotification == null) {
69                                 notifications.remove(notificationIndex);
70                         } else {
71                                 notifications.set(notificationIndex, filteredNotification);
72                         }
73                 }
74                 return notifications;
75         }
76
77         /**
78          * Filters the new posts of the given notification. If {@code currentSone}
79          * is {@code null}, {@code null} is returned and the notification is
80          * subsequently removed. Otherwise only posts that are posted by friend
81          * Sones of the given Sone are retained; all other posts are removed.
82          *
83          * @param newPostNotification
84          *            The new-post notification
85          * @param currentSone
86          *            The current Sone, or {@code null} if not logged in
87          * @return The filtered new-post notification, or {@code null} if the
88          *         notification should be removed
89          */
90         public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone) {
91                 if (currentSone == null) {
92                         return null;
93                 }
94                 List<Post> newPosts = new ArrayList<Post>();
95                 for (Post post : newPostNotification.getElements()) {
96                         if (isPostVisible(currentSone, post)) {
97                                 newPosts.add(post);
98                         }
99                 }
100                 if (newPosts.isEmpty()) {
101                         return null;
102                 }
103                 if (newPosts.size() == newPostNotification.getElements().size()) {
104                         return newPostNotification;
105                 }
106                 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
107                 filteredNotification.setElements(newPosts);
108                 return filteredNotification;
109         }
110
111         /**
112          * Filters the new replies of the given notification. If {@code currentSone}
113          * is {@code null}, {@code null} is returned and the notification is
114          * subsequently removed. Otherwise only replies that are replies to posts
115          * that are posted by friend Sones of the given Sone are retained; all other
116          * replies are removed.
117          *
118          * @param newReplyNotification
119          *            The new-reply notification
120          * @param currentSone
121          *            The current Sone, or {@code null} if not logged in
122          * @return The filtered new-reply notification, or {@code null} if the
123          *         notification should be removed
124          */
125         public static ListNotification<Reply> filterNewReplyNotification(ListNotification<Reply> newReplyNotification, Sone currentSone) {
126                 if (currentSone == null) {
127                         return null;
128                 }
129                 List<Reply> newReplies = new ArrayList<Reply>();
130                 for (Reply reply : newReplyNotification.getElements()) {
131                         if (isPostVisible(currentSone, reply.getPost())) {
132                                 newReplies.add(reply);
133                         }
134                 }
135                 if (newReplies.isEmpty()) {
136                         return null;
137                 }
138                 if (newReplies.size() == newReplyNotification.getElements().size()) {
139                         return newReplyNotification;
140                 }
141                 ListNotification<Reply> filteredNotification = new ListNotification<Reply>(newReplyNotification);
142                 filteredNotification.setElements(newReplies);
143                 return filteredNotification;
144         }
145
146         /**
147          * Finds the notification with the given ID in the list of notifications and
148          * returns it.
149          *
150          * @param <T>
151          *            The type of the item in the notification
152          * @param notifications
153          *            The notification to search
154          * @param notificationId
155          *            The ID of the requested notification
156          * @param notificationElementClass
157          *            The class of the notification item
158          * @return The requested notification, or {@code null} if no notification
159          *         with the given ID could be found
160          */
161         @SuppressWarnings("unchecked")
162         private static <T> ListNotification<T> getNotification(Collection<? extends Notification> notifications, String notificationId, Class<T> notificationElementClass) {
163                 for (Notification notification : notifications) {
164                         if (!notificationId.equals(notification.getId())) {
165                                 continue;
166                         }
167                         return (ListNotification<T>) notification;
168                 }
169                 return null;
170         }
171
172         /**
173          * Checks whether a post is visible to the given Sone. A post is not
174          * considered visible if one of the following statements is true:
175          * <ul>
176          * <li>The post does not have a Sone.</li>
177          * <li>The Sone of the post is not the given Sone, the given Sone does not
178          * follow the post’s Sone, and the given Sone is not the recipient of the
179          * post.</li>
180          * <li>The trust relationship between the two Sones can not be retrieved.</li>
181          * <li>The given Sone has explicitely assigned negative trust to the post’s
182          * Sone.</li>
183          * <li>The given Sone has not explicitely assigned negative trust to the
184          * post’s Sone but the implicit trust is negative.</li>
185          * </ul>
186          * If none of these statements is true the post is considered visible.
187          *
188          * @param sone
189          *            The Sone that checks for a post’s visibility
190          * @param post
191          *            The post to check for visibility
192          * @return {@code true} if the post is considered visible, {@code false}
193          *         otherwise
194          */
195         public static boolean isPostVisible(Sone sone, Post post) {
196                 Validation.begin().isNotNull("Sone", sone).isNotNull("Post", post).check().isNotNull("Sone’s Identity", sone.getIdentity()).check().isInstanceOf("Sone’s Identity", sone.getIdentity(), OwnIdentity.class).check();
197                 Sone postSone = post.getSone();
198                 if (postSone == null) {
199                         return false;
200                 }
201                 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
202                 if (trust != null) {
203                         if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
204                                 return false;
205                         }
206                         if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
207                                 return false;
208                         }
209                 } else {
210                         return false;
211                 }
212                 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
213                         return false;
214                 }
215                 return true;
216         }
217
218 }