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
};
- /** 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
//
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.
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);
--- /dev/null
+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() }
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 }
--- /dev/null
+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))
+ }
+
+}
}
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
}
}