X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FReplyVisibilityFilter.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fnotify%2FReplyVisibilityFilter.java;h=a76ae2501533dbff5abddb3f64f2f7dac0ed95bf;hb=3da1daa6488c642430858a4baacc0aa986a6ebc1;hp=0000000000000000000000000000000000000000;hpb=9f562b1024a26cd8841447c6e29c3d89039d7a22;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java new file mode 100644 index 0000000..a76ae25 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java @@ -0,0 +1,73 @@ +package net.pterodactylus.sone.notify; + +import static com.google.common.base.Preconditions.checkNotNull; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Inject; +import javax.inject.Singleton; + +import net.pterodactylus.sone.data.Post; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; + +import com.google.common.base.Optional; +import com.google.common.base.Predicate; + +/** + * Filter that checks a {@link PostReply} for visibility. + * + * @author David ‘Bombe’ Roden + */ +@Singleton +public class ReplyVisibilityFilter { + + private final PostVisibilityFilter postVisibilityFilter; + + @Inject + public ReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) { + this.postVisibilityFilter = postVisibilityFilter; + } + + /** + * Checks whether a reply is visible to the given Sone. A reply is not + * considered visible if one of the following statements is true: + * + * If none of these statements is true the reply 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 reply + * The reply to check for visibility + * @return {@code true} if the reply is considered visible, {@code false} + * otherwise + */ + boolean isReplyVisible(@Nullable Sone sone, @Nonnull PostReply reply) { + checkNotNull(reply, "reply must not be null"); + Optional post = reply.getPost(); + if (!post.isPresent()) { + return false; + } + if (!postVisibilityFilter.isPostVisible(sone, post.get())) { + return false; + } + return reply.getTime() <= System.currentTimeMillis(); + } + + @Nonnull + public Predicate isVisible(@Nullable final Sone currentSone) { + return new Predicate() { + @Nonnull + @Override + public boolean apply(@Nullable PostReply postReply) { + return isReplyVisible(currentSone, postReply); + } + }; + } + +}