1 package net.pterodactylus.sone.notify;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7 import javax.inject.Singleton;
9 import net.pterodactylus.sone.data.Post;
10 import net.pterodactylus.sone.data.Sone;
11 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
12 import net.pterodactylus.sone.freenet.wot.Trust;
13 import net.pterodactylus.util.notify.Notification;
15 import com.google.common.base.Predicate;
18 * Filters {@link Notification}s involving {@link Post}s.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public class PostVisibilityFilter {
26 * Checks whether a post is visible to the given Sone. A post is not
27 * considered visible if one of the following statements is true:
29 * <li>The post does not have a Sone.</li>
30 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
33 * If {@code post} is not {@code null} more checks are performed, and the
34 * post will be invisible if:
37 * <li>The Sone of the post is not the given Sone, the given Sone does not
38 * follow the post’s Sone, and the given Sone is not the recipient of the
40 * <li>The trust relationship between the two Sones can not be retrieved.</li>
41 * <li>The given Sone has explicitely assigned negative trust to the post’s
43 * <li>The given Sone has not explicitely assigned negative trust to the
44 * post’s Sone but the implicit trust is negative.</li>
46 * If none of these statements is true the post is considered visible.
49 * The Sone that checks for a post’s visibility (may be
50 * {@code null} to skip Sone-specific checks, such as trust)
52 * The post to check for visibility
53 * @return {@code true} if the post is considered visible, {@code false}
56 boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
57 checkNotNull(post, "post must not be null");
58 if (!post.isLoaded()) {
61 Sone postSone = post.getSone();
63 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
65 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
68 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
73 * a null trust means that the trust updater has not yet
74 * received a trust value for this relation. if we return false,
75 * the post feed will stay empty until the trust updater has
76 * received trust values. to prevent this we simply assume that
77 * posts are visible if there is no trust.
80 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
84 return post.getTime() <= System.currentTimeMillis();
88 public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
89 return new Predicate<Post>() {
91 public boolean apply(@Nullable Post post) {
92 return (post != null) && isPostVisible(currentSone, post);