X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FPostVisibilityFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FPostVisibilityFilter.java;h=edcd148d1d8de973fe4911bd0cce842ea6e99923;hp=0000000000000000000000000000000000000000;hb=3da1daa6488c642430858a4baacc0aa986a6ebc1;hpb=9f562b1024a26cd8841447c6e29c3d89039d7a22
diff --git a/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java
new file mode 100644
index 0000000..edcd148
--- /dev/null
+++ b/src/main/java/net/pterodactylus/sone/notify/PostVisibilityFilter.java
@@ -0,0 +1,98 @@
+package net.pterodactylus.sone.notify;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import javax.inject.Singleton;
+
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
+import net.pterodactylus.sone.freenet.wot.Trust;
+import net.pterodactylus.util.notify.Notification;
+
+import com.google.common.base.Predicate;
+
+/**
+ * Filters {@link Notification}s involving {@link Post}s.
+ *
+ * @author David âBombeâ Roden
+ */
+@Singleton
+public class PostVisibilityFilter {
+
+ /**
+ * Checks whether a post is visible to the given Sone. A post is not
+ * considered visible if one of the following statements is true:
+ *
+ * - The post does not have a Sone.
+ * - The postâs {@link Post#getTime() time} is in the future.
+ *
+ *
+ * If {@code post} is not {@code null} more checks are performed, and the
+ * post will be invisible if:
+ *
+ *
+ * - The Sone of the post is not the given Sone, the given Sone does not
+ * follow the postâs Sone, and the given Sone is not the recipient of the
+ * post.
+ * - The trust relationship between the two Sones can not be retrieved.
+ * - The given Sone has explicitely assigned negative trust to the postâs
+ * Sone.
+ * - The given Sone has not explicitely assigned negative trust to the
+ * postâs Sone but the implicit trust is negative.
+ *
+ * If none of these statements is true the post is considered visible.
+ *
+ * @param sone
+ * The Sone that checks for a postâs visibility (may be
+ * {@code null} to skip Sone-specific checks, such as trust)
+ * @param post
+ * The post to check for visibility
+ * @return {@code true} if the post is considered visible, {@code false}
+ * otherwise
+ */
+ boolean isPostVisible(@Nullable Sone sone, @Nonnull Post post) {
+ checkNotNull(post, "post must not be null");
+ if (!post.isLoaded()) {
+ return false;
+ }
+ Sone postSone = post.getSone();
+ if (sone != null) {
+ Trust trust = postSone.getIdentity().getTrust((OwnIdentity) sone.getIdentity());
+ if (trust != null) {
+ if ((trust.getExplicit() != null) && (trust.getExplicit() < 0)) {
+ return false;
+ }
+ if ((trust.getExplicit() == null) && (trust.getImplicit() != null) && (trust.getImplicit() < 0)) {
+ return false;
+ }
+ } else {
+ /*
+ * a null trust means that the trust updater has not yet
+ * received a trust value for this relation. if we return false,
+ * the post feed will stay empty until the trust updater has
+ * received trust values. to prevent this we simply assume that
+ * posts are visible if there is no trust.
+ */
+ }
+ if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.getId().equals(post.getRecipientId().orNull())) {
+ return false;
+ }
+ }
+ return post.getTime() <= System.currentTimeMillis();
+ }
+
+ @Nonnull
+ public Predicate isVisible(@Nullable final Sone currentSone) {
+ return new Predicate() {
+ @Nonnull
+ @Override
+ public boolean apply(@Nullable Post post) {
+ return isPostVisible(currentSone, post);
+ }
+ };
+ }
+
+}