Update year in copyright lines
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / PostVisibilityFilter.java
1 package net.pterodactylus.sone.notify;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7 import javax.inject.Singleton;
8
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;
14
15 import com.google.common.base.Predicate;
16
17 /**
18  * Filters {@link Notification}s involving {@link Post}s.
19  */
20 @Singleton
21 public class PostVisibilityFilter {
22
23         /**
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:
26          * <ul>
27          * <li>The post does not have a Sone.</li>
28          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
29          * </ul>
30          * <p>
31          * If {@code post} is not {@code null} more checks are performed, and the
32          * post will be invisible if:
33          * </p>
34          * <ul>
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
37          * post.</li>
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
40          * Sone.</li>
41          * <li>The given Sone has not explicitely assigned negative trust to the
42          * post’s Sone but the implicit trust is negative.</li>
43          * </ul>
44          * If none of these statements is true the post is considered visible.
45          *
46          * @param sone
47          *              The Sone that checks for a post’s visibility (may be
48          *              {@code null} to skip Sone-specific checks, such as trust)
49          * @param post
50          *              The post to check for visibility
51          * @return {@code true} if the post is considered visible, {@code false}
52          * otherwise
53          */
54         boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
55                 checkNotNull(post, "post must not be null");
56                 if (!post.isLoaded()) {
57                         return false;
58                 }
59                 Sone postSone = post.getSone();
60                 if (sone != null) {
61                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
62                         if (trust != null) {
63                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
64                                         return false;
65                                 }
66                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
67                                         return false;
68                                 }
69                         } else {
70                                 /*
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.
76                                  */
77                         }
78                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
79                                 return false;
80                         }
81                 }
82                 return post.getTime() <= System.currentTimeMillis();
83         }
84
85         @Nonnull
86         public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
87                 return new Predicate<Post>() {
88                         @Override
89                         public boolean apply(@Nullable Post post) {
90                                 return (post != null) && isPostVisible(currentSone, post);
91                         }
92                 };
93         }
94
95 }