Replace Fetched with Kotlin version
[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  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 @Singleton
23 public class PostVisibilityFilter {
24
25         /**
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:
28          * <ul>
29          * <li>The post does not have a Sone.</li>
30          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
31          * </ul>
32          * <p>
33          * If {@code post} is not {@code null} more checks are performed, and the
34          * post will be invisible if:
35          * </p>
36          * <ul>
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
39          * post.</li>
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
42          * Sone.</li>
43          * <li>The given Sone has not explicitely assigned negative trust to the
44          * post’s Sone but the implicit trust is negative.</li>
45          * </ul>
46          * If none of these statements is true the post is considered visible.
47          *
48          * @param sone
49          *              The Sone that checks for a post’s visibility (may be
50          *              {@code null} to skip Sone-specific checks, such as trust)
51          * @param post
52          *              The post to check for visibility
53          * @return {@code true} if the post is considered visible, {@code false}
54          * otherwise
55          */
56         boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
57                 checkNotNull(post, "post must not be null");
58                 if (!post.isLoaded()) {
59                         return false;
60                 }
61                 Sone postSone = post.getSone();
62                 if (sone != null) {
63                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
64                         if (trust != null) {
65                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
66                                         return false;
67                                 }
68                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
69                                         return false;
70                                 }
71                         } else {
72                                 /*
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.
78                                  */
79                         }
80                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
81                                 return false;
82                         }
83                 }
84                 return post.getTime() <= System.currentTimeMillis();
85         }
86
87         @Nonnull
88         public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
89                 return new Predicate<Post>() {
90                         @Override
91                         public boolean apply(@Nullable Post post) {
92                                 return (post != null) && isPostVisible(currentSone, post);
93                         }
94                 };
95         }
96
97 }