1 package net.pterodactylus.sone.notify;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import javax.annotation.Nonnull;
6 import javax.annotation.Nullable;
7 import javax.inject.Inject;
8 import javax.inject.Singleton;
10 import net.pterodactylus.sone.data.Post;
11 import net.pterodactylus.sone.data.PostReply;
12 import net.pterodactylus.sone.data.Sone;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Predicate;
18 * Filter that checks a {@link PostReply} for visibility.
21 public class ReplyVisibilityFilter {
23 private final PostVisibilityFilter postVisibilityFilter;
26 public ReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) {
27 this.postVisibilityFilter = postVisibilityFilter;
31 * Checks whether a reply is visible to the given Sone. A reply is not
32 * considered visible if one of the following statements is true:
34 * <li>The reply does not have a post.</li>
35 * <li>The reply’s post {@link PostVisibilityFilter#isPostVisible(Sone, Post) is not visible}.</li>
36 * <li>The reply’s {@link PostReply#getTime() time} is in the future.</li>
38 * If none of these statements is true the reply is considered visible.
41 * The Sone that checks for a post’s visibility (may be
42 * {@code null} to skip Sone-specific checks, such as trust)
44 * The reply to check for visibility
45 * @return {@code true} if the reply is considered visible, {@code false}
48 boolean isReplyVisible(@Nullable Sone sone, @Nonnull PostReply reply) {
49 checkNotNull(reply, "reply must not be null");
50 Optional<Post> post = reply.getPost();
51 if (!post.isPresent()) {
54 if (!postVisibilityFilter.isPostVisible(sone, post.get())) {
57 return reply.getTime() <= System.currentTimeMillis();
61 public Predicate<PostReply> isVisible(@Nullable final Sone currentSone) {
62 return new Predicate<PostReply>() {
64 public boolean apply(@Nullable PostReply postReply) {
65 return (postReply != null) && isReplyVisible(currentSone, postReply);