🚧 Filter new posts using the current Sone
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 09:23:55 +0000 (10:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 09:23:55 +0000 (10:23 +0100)
src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt
src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt

index 28e410c..c45d15e 100644 (file)
@@ -18,6 +18,7 @@ package net.pterodactylus.sone.web
 
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.notify.ListNotification
 import net.pterodactylus.sone.notify.PostVisibilityFilter
 import net.pterodactylus.sone.notify.ReplyVisibilityFilter
@@ -38,10 +39,10 @@ class NewElements @Inject constructor(
                private val replyVisibilityFilter: ReplyVisibilityFilter
 ) {
 
-       fun newPosts(): Collection<Post> =
+       fun newPosts(currentSone: Sone? = null): Collection<Post> =
                listOf(newPostNotification, localPostNotification)
                        .flatMap(ListNotification<Post>::elements)
-                       .filter { postVisibilityFilter.isPostVisible(null, it) }
+                       .filter { postVisibilityFilter.isPostVisible(currentSone, it) }
 
        fun newReplies(): Collection<PostReply> =
                listOf(newReplyNotification, localReplyNotification)
index ccda1c7..2b4436d 100644 (file)
@@ -20,11 +20,14 @@ import com.google.inject.Guice
 import com.google.inject.name.Names.named
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
+import net.pterodactylus.sone.data.Sone
 import net.pterodactylus.sone.notify.ListNotification
+import net.pterodactylus.sone.notify.PostVisibilityFilter
 import net.pterodactylus.sone.notify.matchThisPost
 import net.pterodactylus.sone.notify.matchThisReply
 import net.pterodactylus.sone.notify.showAllPosts
 import net.pterodactylus.sone.notify.showAllReplies
+import net.pterodactylus.sone.test.createLocalSone
 import net.pterodactylus.sone.test.createPost
 import net.pterodactylus.sone.test.createPostReply
 import net.pterodactylus.sone.test.getInstance
@@ -84,9 +87,19 @@ class NewElementsTest {
                assertThat(newElements.newReplies(), containsInAnyOrder(reply2))
        }
 
+       @Test
+       fun `new posts are filtered using the given Sone`() {
+               val postVisibilityFilter = object : PostVisibilityFilter {
+                       override fun isPostVisible(sone: Sone?, post: Post) = (sone == localSone) && (post == post2)
+               }
+               val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, postVisibilityFilter, showAllReplies)
+               assertThat(newElements.newPosts(localSone), contains(post2))
+       }
+
 }
 
 private val post1 = createPost()
 private val post2 = createPost()
 private val reply1 = createPostReply()
 private val reply2 = createPostReply()
+private val localSone = createLocalSone()