From: David ‘Bombe’ Roden Date: Fri, 21 Feb 2020 11:19:43 +0000 (+0100) Subject: 🎨 Replace FUTURE_POSTS_FILTER with Kotlin version X-Git-Tag: v82^2~51^2~2 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ee39c03a6a811300728183552afcede1d9f8e79a;p=Sone.git 🎨 Replace FUTURE_POSTS_FILTER with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index a4a794e..9914185 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -22,7 +22,6 @@ import static com.google.common.base.Optional.absent; import java.util.Comparator; import com.google.common.base.Optional; -import com.google.common.base.Predicate; /** * A post is a short message that a user writes in his Sone to let other users @@ -40,16 +39,6 @@ public interface Post extends Identified { }; - /** Filter for posts with timestamps from the future. */ - public static final Predicate FUTURE_POSTS_FILTER = new Predicate() { - - @Override - public boolean apply(Post post) { - return (post != null) && (post.getTime() <= System.currentTimeMillis()); - } - - }; - // // ACCESSORS // diff --git a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java index 821c198..8197ba8 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java @@ -32,6 +32,8 @@ import com.google.common.collect.Collections2; import freenet.support.SimpleFieldSet; +import static net.pterodactylus.sone.data.PostKt.noFuturePost; + /** * Implementation of an FCP interface for other clients or plugins to * communicate with Sone. @@ -67,7 +69,7 @@ public class GetPostFeedCommand extends AbstractSoneCommand { allPosts.addAll(friendSone.getPosts()); } allPosts.addAll(getCore().getDirectedPosts(sone.getId())); - allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER); + allPosts = Collections2.filter(allPosts, noFuturePost()::invoke); List sortedPosts = new ArrayList<>(allPosts); Collections.sort(sortedPosts, Post.NEWEST_FIRST); diff --git a/src/main/kotlin/net/pterodactylus/sone/data/Post.kt b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt new file mode 100644 index 0000000..7041561 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/data/Post.kt @@ -0,0 +1,8 @@ +package net.pterodactylus.sone.data + +/** + * Predicate that returns whether a post is _not_ from the future, + * i.e. whether it should be visible now. + */ +@get:JvmName("noFuturePost") +val noFuturePost: (Post) -> Boolean = { it.time <= System.currentTimeMillis() } 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 19eba1c..7534bb4 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -58,7 +58,7 @@ class SearchPage(webInterface: WebInterface, loaders: Loaders, templateRenderer: val postPagination = cache.get(phrases) { soneRequest.core.sones .flatMap(Sone::getPosts) - .filter { Post.FUTURE_POSTS_FILTER.apply(it) } + .filter(noFuturePost) .scoreAndPaginate(phrases, soneRequest.core.preferences.postsPerPage) { it.allText(soneNameCache, soneRequest.core::getReplies) } }.apply { page = soneRequest.parameters["postPage"].emptyToNull?.toIntOrNull() ?: 0 } diff --git a/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt new file mode 100644 index 0000000..e5d9e97 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt @@ -0,0 +1,26 @@ +package net.pterodactylus.sone.data + +import net.pterodactylus.sone.test.createPost +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import java.util.concurrent.TimeUnit.DAYS +import kotlin.test.Test + +/** + * Unit test for the utilities in `Post.kt`. + */ +class PostTest { + + @Test + fun `noFuturePost filter recognizes post from future`() { + val post = createPost(time = System.currentTimeMillis() + DAYS.toMillis(1)) + assertThat(noFuturePost(post), equalTo(false)) + } + + @Test + fun `noFuturePost filter recognizes post not from future`() { + val post = createPost(time = System.currentTimeMillis()) + assertThat(noFuturePost(post), equalTo(true)) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt index 65f279a..17eae9a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt @@ -43,11 +43,12 @@ fun createLocalSone(id: String? = createId()) = object : IdOnlySone(id) { } fun createRemoteSone(id: String? = createId()) = IdOnlySone(id) -fun createPost(text: String = "", sone: Sone = remoteSone1, known: Boolean = false): Post.EmptyPost { +fun createPost(text: String = "", sone: Sone = remoteSone1, known: Boolean = false, time: Long = 1): Post.EmptyPost { return object : Post.EmptyPost("post-id") { override fun getSone() = sone override fun getText() = text override fun isKnown() = known + override fun getTime() = time } }