♻️ Extract interface from post visibility filter
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / DefaultPostVisibilityFilter.java
1 package net.pterodactylus.sone.notify;
2
3 import java.util.function.Predicate;
4
5 import static com.google.common.base.Preconditions.checkNotNull;
6
7 import javax.annotation.Nonnull;
8 import javax.annotation.Nullable;
9 import javax.inject.Singleton;
10
11 import net.pterodactylus.sone.data.Post;
12 import net.pterodactylus.sone.data.Sone;
13 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
14 import net.pterodactylus.sone.freenet.wot.Trust;
15 import net.pterodactylus.util.notify.Notification;
16
17 /**
18  * Filters {@link Notification}s involving {@link Post}s.
19  */
20 @Singleton
21 public class DefaultPostVisibilityFilter implements 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         @Override
55         public boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
56                 checkNotNull(post, "post must not be null");
57                 if (!post.isLoaded()) {
58                         return false;
59                 }
60                 Sone postSone = post.getSone();
61                 if (sone != null) {
62                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
63                         if (trust != null) {
64                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
65                                         return false;
66                                 }
67                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
68                                         return false;
69                                 }
70                         } else {
71                                 /*
72                                  * a null trust means that the trust updater has not yet
73                                  * received a trust value for this relation. if we return false,
74                                  * the post feed will stay empty until the trust updater has
75                                  * received trust values. to prevent this we simply assume that
76                                  * posts are visible if there is no trust.
77                                  */
78                         }
79                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
80                                 return false;
81                         }
82                 }
83                 return post.getTime() <= System.currentTimeMillis();
84         }
85
86         @Override
87         @Nonnull
88         public Predicate<Post> isVisible(@Nullable final Sone currentSone) {
89                 return post -> (post != null) && isPostVisible(currentSone, post);
90         }
91
92 }