♻️ Move predicate method into interface
[Sone.git] / src / main / java / net / pterodactylus / sone / notify / DefaultPostVisibilityFilter.java
1 package net.pterodactylus.sone.notify;
2
3 import javax.annotation.Nonnull;
4 import javax.annotation.Nullable;
5 import javax.inject.Singleton;
6
7 import net.pterodactylus.sone.data.Post;
8 import net.pterodactylus.sone.data.Sone;
9 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
10 import net.pterodactylus.sone.freenet.wot.Trust;
11 import net.pterodactylus.util.notify.Notification;
12
13 /**
14  * Filters {@link Notification}s involving {@link Post}s.
15  */
16 @Singleton
17 public class DefaultPostVisibilityFilter implements PostVisibilityFilter {
18
19         /**
20          * Checks whether a post is visible to the given Sone. A post is not
21          * considered visible if one of the following statements is true:
22          * <ul>
23          * <li>The post does not have a Sone.</li>
24          * <li>The post’s {@link Post#getTime() time} is in the future.</li>
25          * </ul>
26          * <p>
27          * If {@code post} is not {@code null} more checks are performed, and the
28          * post will be invisible if:
29          * </p>
30          * <ul>
31          * <li>The Sone of the post is not the given Sone, the given Sone does not
32          * follow the post’s Sone, and the given Sone is not the recipient of the
33          * post.</li>
34          * <li>The trust relationship between the two Sones can not be retrieved.</li>
35          * <li>The given Sone has explicitely assigned negative trust to the post’s
36          * Sone.</li>
37          * <li>The given Sone has not explicitely assigned negative trust to the
38          * post’s Sone but the implicit trust is negative.</li>
39          * </ul>
40          * If none of these statements is true the post is considered visible.
41          *
42          * @param sone
43          *              The Sone that checks for a post’s visibility (may be
44          *              {@code null} to skip Sone-specific checks, such as trust)
45          * @param post
46          *              The post to check for visibility
47          * @return {@code true} if the post is considered visible, {@code false}
48          * otherwise
49          */
50         @Override
51         public boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
52                 if (!post.isLoaded()) {
53                         return false;
54                 }
55                 Sone postSone = post.getSone();
56                 if (sone != null) {
57                         Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
58                         if (trust != null) {
59                                 if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
60                                         return false;
61                                 }
62                                 if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
63                                         return false;
64                                 }
65                         } else {
66                                 /*
67                                  * a null trust means that the trust updater has not yet
68                                  * received a trust value for this relation. if we return false,
69                                  * the post feed will stay empty until the trust updater has
70                                  * received trust values. to prevent this we simply assume that
71                                  * posts are visible if there is no trust.
72                                  */
73                         }
74                         if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
75                                 return false;
76                         }
77                 }
78                 return post.getTime() <= System.currentTimeMillis();
79         }
80
81 }