From fa971584d8bb5c0e49851187a76e129f29684c0e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 27 Jun 2020 23:12:45 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Extract=20interface=20for?= =?utf8?q?=20reply=20visibility=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/notify/DefaultReplyVisibilityFilter.java | 68 ++++++++++++++++++++++ .../sone/notify/ReplyVisibilityFilter.java | 57 ++---------------- .../sone/notify/ReplyVisibilityFilterTest.kt | 6 +- .../net/pterodactylus/sone/notify/Testing.kt | 2 +- 4 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/notify/DefaultReplyVisibilityFilter.java diff --git a/src/main/java/net/pterodactylus/sone/notify/DefaultReplyVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/DefaultReplyVisibilityFilter.java new file mode 100644 index 0000000..0b891af --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/notify/DefaultReplyVisibilityFilter.java @@ -0,0 +1,68 @@ +package net.pterodactylus.sone.notify; + +import java.util.function.Predicate; + +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; + +/** + * Filter that checks a {@link PostReply} for visibility. + */ +@Singleton +public class DefaultReplyVisibilityFilter implements ReplyVisibilityFilter { + + private final PostVisibilityFilter postVisibilityFilter; + + @Inject + public DefaultReplyVisibilityFilter(@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 + */ + @Override + public 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 + @Override + public Predicate isVisible(@Nullable final Sone currentSone) { + return postReply -> (postReply != null) && isReplyVisible(currentSone, postReply); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java b/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java index 67a8d7d..cd282b7 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java +++ b/src/main/java/net/pterodactylus/sone/notify/ReplyVisibilityFilter.java @@ -1,66 +1,19 @@ package net.pterodactylus.sone.notify; import java.util.function.Predicate; - -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; - -/** - * Filter that checks a {@link PostReply} for visibility. - */ -@Singleton -public class ReplyVisibilityFilter { - - private final PostVisibilityFilter postVisibilityFilter; +import com.google.inject.ImplementedBy; - @Inject - public ReplyVisibilityFilter(@Nonnull PostVisibilityFilter postVisibilityFilter) { - this.postVisibilityFilter = postVisibilityFilter; - } +@ImplementedBy(DefaultReplyVisibilityFilter.class) +public interface ReplyVisibilityFilter { - /** - * Checks whether a reply is visible to the given Sone. A reply is not - * considered visible if one of the following statements is true: - *
    - *
  • The reply does not have a post.
  • - *
  • The reply’s post {@link PostVisibilityFilter#isPostVisible(Sone, Post) is not visible}.
  • - *
  • The reply’s {@link PostReply#getTime() time} is in the future.
  • - *
- * 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(); - } + boolean isReplyVisible(@Nullable Sone sone, @Nonnull PostReply reply); - @Nonnull - public Predicate isVisible(@Nullable final Sone currentSone) { - return postReply -> (postReply != null) && isReplyVisible(currentSone, postReply); - } + Predicate isVisible(@Nullable final Sone currentSone); } diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt index 980dcd0..e91bd42 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/ReplyVisibilityFilterTest.kt @@ -14,20 +14,20 @@ import org.junit.Test */ class ReplyVisibilityFilterTest { - private val replyVisibilityFilter = ReplyVisibilityFilter(showAllPosts) + private val replyVisibilityFilter = DefaultReplyVisibilityFilter(showAllPosts) private val localSone = createLocalSone() private val post = createPost() @Test fun `reply visibility filter is only created once`() { val injector = Guice.createInjector() - injector.verifySingletonInstance() + injector.verifySingletonInstance() } @Test fun `reply is not visible if post is not visible`() { val postReply = createPostReply(post = post) - val replyVisibilityFilter = ReplyVisibilityFilter(showNoPosts) + val replyVisibilityFilter = DefaultReplyVisibilityFilter(showNoPosts) assertThat(replyVisibilityFilter.isReplyVisible(null, postReply), equalTo(false)) } diff --git a/src/test/kotlin/net/pterodactylus/sone/notify/Testing.kt b/src/test/kotlin/net/pterodactylus/sone/notify/Testing.kt index bec8a51..381ded4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/notify/Testing.kt +++ b/src/test/kotlin/net/pterodactylus/sone/notify/Testing.kt @@ -17,7 +17,7 @@ fun matchThisReply(reply: PostReply) = createReplyVisibilityFilter(showAllPosts) val showAllReplies = createReplyVisibilityFilter(showAllPosts) { _, _ -> true } val showNoReplies = createReplyVisibilityFilter(showAllPosts) { _, _ -> false } -private fun createReplyVisibilityFilter(postVisibilityFilter: PostVisibilityFilter, visible: (Sone?, PostReply) -> Boolean) = object : ReplyVisibilityFilter(postVisibilityFilter) { +private fun createReplyVisibilityFilter(postVisibilityFilter: PostVisibilityFilter, visible: (Sone?, PostReply) -> Boolean) = object : DefaultReplyVisibilityFilter(postVisibilityFilter) { override fun isReplyVisible(sone: Sone?, reply: PostReply) = visible(sone, reply) override fun isVisible(currentSone: Sone?) = Predicate { r -> r != null && isReplyVisible(currentSone, r) } } -- 2.7.4