From: David ‘Bombe’ Roden Date: Sat, 22 Feb 2020 22:44:47 +0000 (+0100) Subject: 🎨 Replace future reply filter with Kotlin version X-Git-Tag: v82^2~47 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=0ace0221fc20ef93457b8c62c6ac12286c1aa12c 🎨 Replace future reply filter with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/data/Reply.java b/src/main/java/net/pterodactylus/sone/data/Reply.java index 705b1e4..918fd01 100644 --- a/src/main/java/net/pterodactylus/sone/data/Reply.java +++ b/src/main/java/net/pterodactylus/sone/data/Reply.java @@ -17,10 +17,6 @@ package net.pterodactylus.sone.data; -import java.util.Comparator; - -import com.google.common.base.Predicate; - /** * Defines methods common for all replies. * @@ -29,19 +25,6 @@ import com.google.common.base.Predicate; */ public interface Reply> extends Identified { - /** Filter for replies with timestamps from the future. */ - public static final Predicate> FUTURE_REPLY_FILTER = new Predicate>() { - - /** - * {@inheritDoc} - */ - @Override - public boolean apply(Reply reply) { - return (reply != null) && (reply.getTime() <= System.currentTimeMillis()); - } - - }; - /** * Returns the ID of the reply. * diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt b/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt index 9a3ec64..cfc940a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt +++ b/src/main/kotlin/net/pterodactylus/sone/data/Reply.kt @@ -25,3 +25,10 @@ import java.util.Comparator.comparing @get:JvmName("newestReplyFirst") val newestReplyFirst: Comparator> = comparing(Reply<*>::getTime).reversed() + +/** + * Predicate that returns whether a reply is _not_ from the future, + * i.e. whether it should be visible now. + */ +val noFutureReply: (Reply<*>) -> Boolean = + { it.getTime() <= System.currentTimeMillis() } diff --git a/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt b/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt index ab7f6ba..42239aa 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/PostAccessor.kt @@ -51,5 +51,5 @@ class PostAccessor(private val core: Core) : ReflectionAccessor() { } -private fun Core.getReplies(post: Post) = getReplies(post.id).filter { Reply.FUTURE_REPLY_FILTER.apply(it) } +private fun Core.getReplies(post: Post) = getReplies(post.id).filter(noFutureReply) private val TemplateContext?.currentSone: Sone? get() = this?.get("currentSone") as? Sone diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index 7534bb4..9bbc3e5 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -88,7 +88,7 @@ class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: private fun Post.allText(soneNameCache: (Sone) -> String, getReplies: (String) -> Collection) = (text + recipient.orNull()?.let { " ${soneNameCache(it)}" } + getReplies(id) - .filter { PostReply.FUTURE_REPLY_FILTER.apply(it) } + .filter(noFutureReply) .map { "${soneNameCache(it.sone)} ${it.text}" }.joinToString(" ", " ")).toLowerCase() private fun Iterable.indicesFor(text: String, predicate: (Phrase) -> Boolean) = diff --git a/src/test/kotlin/net/pterodactylus/sone/data/ReplyTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/ReplyTest.kt index 3289b7d..d7f7138 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/ReplyTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/ReplyTest.kt @@ -22,6 +22,7 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.greaterThan import org.hamcrest.Matchers.lessThan +import java.util.concurrent.TimeUnit.DAYS import kotlin.test.Test class ReplyTest { @@ -47,4 +48,16 @@ class ReplyTest { assertThat(newestReplyFirst.compare(reply1, reply2), equalTo(0)) } + @Test + fun `noFutureReply filter recognizes reply from the future`() { + val futureReply = emptyPostReply(time = System.currentTimeMillis() + DAYS.toMillis(1)) + assertThat(noFutureReply(futureReply), equalTo(false)) + } + + @Test + fun `noFutureReply filter recognizes reply from the present`() { + val futureReply = emptyPostReply(time = System.currentTimeMillis()) + assertThat(noFutureReply(futureReply), equalTo(true)) + } + }