🎨 Replace FUTURE_POSTS_FILTER with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 21 Feb 2020 11:19:43 +0000 (12:19 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 21 Feb 2020 11:19:43 +0000 (12:19 +0100)
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java
src/main/kotlin/net/pterodactylus/sone/data/Post.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt
src/test/kotlin/net/pterodactylus/sone/data/PostTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt

index a4a794e..9914185 100644 (file)
@@ -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<Post> FUTURE_POSTS_FILTER = new Predicate<Post>() {
-
-               @Override
-               public boolean apply(Post post) {
-                       return (post != null) && (post.getTime() <= System.currentTimeMillis());
-               }
-
-       };
-
        //
        // ACCESSORS
        //
index 821c198..8197ba8 100644 (file)
@@ -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<Post> 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 (file)
index 0000000..7041561
--- /dev/null
@@ -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() }
index 19eba1c..7534bb4 100644 (file)
@@ -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 (file)
index 0000000..e5d9e97
--- /dev/null
@@ -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))
+       }
+
+}
index 65f279a..17eae9a 100644 (file)
@@ -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
        }
 }