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.
21 public class PostVisibilityFilter {
24 * Checks whether a post is visible to the given Sone. A post is not
25 * considered visible if one of the following statements is true:
27 * <li>The post does not have a Sone.</li>
28 * <li>The post’s {@link Post#getTime() time} is in the future.</li>
31 * If {@code post} is not {@code null} more checks are performed, and the
32 * post will be invisible if:
35 * <li>The Sone of the post is not the given Sone, the given Sone does not
36 * follow the post’s Sone, and the given Sone is not the recipient of the
38 * <li>The trust relationship between the two Sones can not be retrieved.</li>
39 * <li>The given Sone has explicitely assigned negative trust to the post’s
41 * <li>The given Sone has not explicitely assigned negative trust to the
42 * post’s Sone but the implicit trust is negative.</li>
44 * If none of these statements is true the post is considered visible.
47 * The Sone that checks for a post’s visibility (may be
48 * {@code null} to skip Sone-specific checks, such as trust)
50 * The post to check for visibility
51 * @return {@code true} if the post is considered visible, {@code false}
54 boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
55 checkNotNull(post, "post must not be null");
56 if (!post.isLoaded()) {
59 Sone postSone = post.getSone();
61 Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
63 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
66 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
71 * a null trust means that the trust updater has not yet
72 * received a trust value for this relation. if we return false,
73 * the post feed will stay empty until the trust updater has
74 * received trust values. to prevent this we simply assume that
75 * posts are visible if there is no trust.
78 if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
82 return post.getTime() <= System.currentTimeMillis();
86 public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
87 return new Predicate<Post>() {
89 public boolean apply(@Nullable Post post) {
90 return (post != null) && isPostVisible(currentSone, post);