✨ Update Sone processor when max-age-of-posts-to-load is changed
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 20 Jul 2026 17:26:17 +0000 (19:26 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 22 Jul 2026 16:38:47 +0000 (18:38 +0200)
src/main/kotlin/net/pterodactylus/sone/core/UpdatedSoneProcessor.kt
src/test/kotlin/net/pterodactylus/sone/core/UpdatedSoneProcessorTest.kt

index 35052ba..da0315d 100644 (file)
@@ -9,6 +9,7 @@ import net.pterodactylus.sone.database.*
 import net.pterodactylus.sone.utils.*
 import java.util.logging.Logger
 import jakarta.inject.Inject
+import java.util.concurrent.TimeUnit.DAYS
 
 /**
  * An `UpdatedSoneProcessor` is called to process a [Sone] after it has been
@@ -67,6 +68,12 @@ abstract class BasicUpdateSoneProcessor(private val database: Database, private
 
        private val Sone.followingTime get() = database.getFollowingTime(id) ?: 0
 
+       @Subscribe
+       fun maxAgeOfPostsToLoadChanged(maxAgeOfPostsToLoadChangedEvent: MaxAgeOfPostsToLoadChangedEvent) {
+               postFilter = { post -> post.time > System.currentTimeMillis() - DAYS.toMillis(maxAgeOfPostsToLoadChangedEvent.newMaxAgeOfPostsToLoad.toLong()) }
+               postReplyFilter = { postReply -> postReply.time > System.currentTimeMillis() - DAYS.toMillis(maxAgeOfPostsToLoadChangedEvent.newMaxAgeOfPostsToLoad.toLong()) }
+       }
+
 }
 
 class DefaultUpdateSoneProcessor @Inject constructor(database: Database, eventBus: EventBus) :
index 3b498d1..61c928f 100644 (file)
@@ -1,6 +1,8 @@
 package net.pterodactylus.sone.core
 
 import com.google.common.eventbus.EventBus
+import java.util.concurrent.TimeUnit
+import net.pterodactylus.sone.core.event.MaxAgeOfPostsToLoadChangedEvent
 import net.pterodactylus.sone.core.event.NewPostFoundEvent
 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent
 import net.pterodactylus.sone.core.event.PostRemovedEvent
@@ -171,6 +173,30 @@ class UpdatedSoneProcessorTest {
        }
 
        @Test
+       fun `updated Sone processor sets post filter when max age of posts to load is updated`() {
+               whenever(storedSone.posts).thenReturn(emptyList<Post>())
+               whenever(storedSone.options).thenReturn(DefaultSoneOptions())
+               val now = System.currentTimeMillis()
+               posts.forEachIndexed { index, post -> whenever(post.time).thenReturn(now - TimeUnit.DAYS.toMillis(index.toLong() + 1) + TimeUnit.HOURS.toMillis(12)) }
+               updatedSoneProcessor.maxAgeOfPostsToLoadChanged(MaxAgeOfPostsToLoadChangedEvent(2))
+               val newSone = createRemoteSone("sone", posts = posts, time = 9999)
+               updatedSoneProcessor.updateSone(newSone)
+               assertThat(newSone.posts, contains(posts[0], posts[1]))
+       }
+
+       @Test
+       fun `updated Sone processor sets post reply filter when max age of posts to load is updated`() {
+               whenever(storedSone.posts).thenReturn(emptyList<Post>())
+               whenever(storedSone.options).thenReturn(DefaultSoneOptions())
+               val now = System.currentTimeMillis()
+               postReplies.forEachIndexed { index, postReply -> whenever(postReply.time).thenReturn(now - TimeUnit.DAYS.toMillis(index.toLong() + 1) + TimeUnit.HOURS.toMillis(12)) }
+               updatedSoneProcessor.maxAgeOfPostsToLoadChanged(MaxAgeOfPostsToLoadChangedEvent(2))
+               val newSone = createRemoteSone("sone", postReplies = postReplies.toSet(), time = 9999)
+               updatedSoneProcessor.updateSone(newSone)
+               assertThat(newSone.replies, contains(postReplies[0], postReplies[1]))
+       }
+
+       @Test
        fun `default updated Sone processor can be created by dependency injection`() {
                assertThat(baseInjector.createChildInjector(
                                Database::class.isProvidedByMock()