2 * Sone - ListNotificationFilters.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.notify;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
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;
35 * Filter for {@link ListNotification}s.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class ListNotificationFilters {
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.
49 * @param notifications
50 * The notifications to filter
52 * The current Sone, or {@code null} if not logged in
53 * @return The filtered notifications
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())) {
63 filteredNotifications.add(notification);
64 } else if (notification.getId().equals("new-post-notification")) {
65 if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get())) {
68 ListNotification<Post> filteredNotification = filterNewPostNotification((ListNotification<Post>) notification, currentSone, true);
69 if (filteredNotification != null) {
70 filteredNotifications.add(filteredNotification);
72 } else if (notification.getId().equals("new-reply-notification")) {
73 if ((currentSone != null) && (!currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get())) {
76 ListNotification<PostReply> filteredNotification = filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
77 if (filteredNotification != null) {
78 filteredNotifications.add(filteredNotification);
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);
86 filteredNotifications.add(notification);
89 return filteredNotifications;
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.
99 * @param newPostNotification
100 * The new-post notification
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
106 * @return The filtered new-post notification, or {@code null} if the
107 * notification should be removed
109 public static ListNotification<Post> filterNewPostNotification(ListNotification<Post> newPostNotification, Sone currentSone, boolean soneRequired) {
110 if (soneRequired && (currentSone == null)) {
113 List<Post> newPosts = new ArrayList<Post>();
114 for (Post post : newPostNotification.getElements()) {
115 if (isPostVisible(currentSone, post)) {
119 if (newPosts.isEmpty()) {
122 if (newPosts.size() == newPostNotification.getElements().size()) {
123 return newPostNotification;
125 ListNotification<Post> filteredNotification = new ListNotification<Post>(newPostNotification);
126 filteredNotification.setElements(newPosts);
127 filteredNotification.setLastUpdateTime(newPostNotification.getLastUpdatedTime());
128 return filteredNotification;
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.
138 * @param newReplyNotification
139 * The new-reply notification
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
145 public static ListNotification<PostReply> filterNewReplyNotification(ListNotification<PostReply> newReplyNotification, Sone currentSone) {
146 if (currentSone == null) {
149 List<PostReply> newReplies = new ArrayList<PostReply>();
150 for (PostReply reply : newReplyNotification.getElements()) {
151 if (isReplyVisible(currentSone, reply)) {
152 newReplies.add(reply);
155 if (newReplies.isEmpty()) {
158 if (newReplies.size() == newReplyNotification.getElements().size()) {
159 return newReplyNotification;
161 ListNotification<PostReply> filteredNotification = new ListNotification<PostReply>(newReplyNotification);
162 filteredNotification.setElements(newReplies);
163 filteredNotification.setLastUpdateTime(newReplyNotification.getLastUpdatedTime());
164 return filteredNotification;
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
175 * The posts to filter
177 * The current Sone (may be {@code null})
178 * @return The filtered posts
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);
187 return filteredPosts;
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:
194 * <li>The post does not have a Sone.</li>
195 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
198 * If {@code post} is not {@code null} more checks are performed, and the
199 * post will be invisible if:
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
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
208 * <li>The given Sone has not explicitely assigned negative trust to the
209 * post’s Sone but the implicit trust is negative.</li>
211 * If none of these statements is true the post is considered visible.
214 * The Sone that checks for a post’s visibility (may be
215 * {@code null} to skip Sone-specific checks, such as trust)
217 * The post to check for visibility
218 * @return {@code true} if the post is considered visible, {@code false}
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) {
228 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
230 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
233 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
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.
246 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) {
250 if (post.getTime() > System.currentTimeMillis()) {
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:
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
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>
273 * If none of these statements is true the reply is considered visible.
276 * The Sone that checks for a post’s visibility (may be
277 * {@code null} to skip Sone-specific checks, such as trust)
279 * The reply to check for visibility
280 * @return {@code true} if the reply is considered visible, {@code false}
283 public static boolean isReplyVisible(Sone sone, PostReply reply) {
284 checkNotNull(reply, "reply must not be null");
285 Post post = reply.getPost();
289 if (!isPostVisible(sone, post)) {
292 if (reply.getTime() > System.currentTimeMillis()) {