X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FListNotificationFilters.java;h=207f4cc135c73dd64a104a551c433857244a2e61;hp=2a98ec8e0d172deac069551b2d562952edef678c;hb=d138151b18222c458eba19528a2a2a9f07ca5a9e;hpb=e1da0315a8a0f46412332465ed95484f97ee6d7b diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java index 2a98ec8..207f4cc 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java @@ -24,7 +24,10 @@ import java.util.List; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; 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 net.pterodactylus.util.validation.Validation; /** * Filter for {@link ListNotification}s. @@ -84,13 +87,13 @@ public class ListNotificationFilters { * @return The filtered new-post notification, or {@code null} if the * notification should be removed */ - private static ListNotification filterNewPostNotification(ListNotification newPostNotification, Sone currentSone) { + public static ListNotification filterNewPostNotification(ListNotification newPostNotification, Sone currentSone) { if (currentSone == null) { return null; } List newPosts = new ArrayList(); for (Post post : newPostNotification.getElements()) { - if (currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone()) || currentSone.equals(post.getRecipient())) { + if (isPostVisible(currentSone, post)) { newPosts.add(post); } } @@ -119,13 +122,13 @@ public class ListNotificationFilters { * @return The filtered new-reply notification, or {@code null} if the * notification should be removed */ - private static ListNotification filterNewReplyNotification(ListNotification newReplyNotification, Sone currentSone) { + public static ListNotification filterNewReplyNotification(ListNotification newReplyNotification, Sone currentSone) { if (currentSone == null) { return null; } List newReplies = new ArrayList(); for (Reply reply : newReplyNotification.getElements()) { - if (currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone()) || currentSone.equals(reply.getPost().getRecipient())) { + if (isPostVisible(currentSone, reply.getPost())) { newReplies.add(reply); } } @@ -166,4 +169,50 @@ public class ListNotificationFilters { return null; } + /** + * 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 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 + * @param post + * The post to check for visibility + * @return {@code true} if the post is considered visible, {@code false} + * otherwise + */ + public static boolean isPostVisible(Sone sone, Post post) { + Validation.begin().isNotNull("Sone", sone).isNotNull("Post", post).check().isNotNull("Sone’s Identity", sone.getIdentity()).check().isInstanceOf("Sone’s Identity", sone.getIdentity(), OwnIdentity.class).check(); + Sone postSone = post.getSone(); + if (postSone == null) { + return false; + } + 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 { + return false; + } + if ((!postSone.equals(sone)) && !sone.hasFriend(postSone.getId()) && !sone.equals(post.getRecipient())) { + return false; + } + return true; + } + }