@ImplementedBy(DefaultUpdateSoneProcessor::class)
interface UpdatedSoneProcessor {
+ var postFilter: (post: Post) -> Boolean
+ var postReplyFilter: (postReply: PostReply) -> Boolean
+
fun updateSone(sone: Sone)
}
private val logger = Logger.getLogger(UpdatedSoneProcessor::class.qualifiedName)!!
+ override var postFilter: (post: Post) -> Boolean = { _ -> true }
+ override var postReplyFilter: (postReply: PostReply) -> Boolean = { _ -> true }
+
override fun updateSone(sone: Sone) {
val storedSone = database.getSone(sone.id) ?: return
if (!soneCanBeUpdated(storedSone, sone)) {
logger.fine("Downloaded Sone $sone can not update stored Sone $storedSone.")
return
}
+ sone.setPosts(sone.posts.filter(postFilter))
+ sone.setReplies(sone.replies.filter(postReplyFilter))
SoneComparison(storedSone, sone).apply {
newPosts
import net.pterodactylus.sone.data.Post
import net.pterodactylus.sone.data.PostReply
import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions
import net.pterodactylus.sone.database.Database
import net.pterodactylus.sone.test.argumentCaptor
+import net.pterodactylus.sone.test.createRemoteSone
import net.pterodactylus.sone.test.getInstance
import net.pterodactylus.sone.test.isProvidedByMock
import net.pterodactylus.sone.test.mock
}
@Test
+ fun `updated Sone processor only stores posts that match the post filter`() {
+ whenever(storedSone.posts).thenReturn(emptyList<Post>())
+ whenever(storedSone.options).thenReturn(DefaultSoneOptions())
+ updatedSoneProcessor.postFilter = { post -> post.time > 2500 }
+ val newSone = createRemoteSone("sone", posts = posts, time = 9999)
+ updatedSoneProcessor.updateSone(newSone)
+ assertThat(newSone.posts, contains(posts[2]))
+ }
+
+ @Test
+ fun `updated Sone processor only stores post replies that match the post reply filter`() {
+ whenever(storedSone.posts).thenReturn(emptyList<Post>())
+ whenever(storedSone.options).thenReturn(DefaultSoneOptions())
+ updatedSoneProcessor.postReplyFilter = { postReply -> postReply.time > 2500 }
+ val newSone = createRemoteSone("sone", postReplies = postReplies.toSet(), time = 9999)
+ updatedSoneProcessor.updateSone(newSone)
+ assertThat(newSone.replies, contains(postReplies[2]))
+ }
+
+ @Test
fun `default updated Sone processor can be created by dependency injection`() {
assertThat(baseInjector.createChildInjector(
Database::class.isProvidedByMock()