✨ Return filtered elements for new-posts page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 10:54:39 +0000 (11:54 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 10:54:39 +0000 (11:54 +0100)
src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt

index 070fc4c..a3a8461 100644 (file)
@@ -26,7 +26,7 @@ class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders,
 
        override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
                        getCurrentSone(soneRequest.toadletContext).let { currentSone ->
-                               (newElements.newPosts() + newElements.newReplies().mapPresent { it.post })
+                               (newElements.newPosts(currentSone) + newElements.newReplies(currentSone).mapPresent { it.post })
                                                .distinct()
                                                .sortedByDescending { it.time }
                                                .let { posts ->
index fa84e5d..3b6f316 100644 (file)
@@ -2,6 +2,7 @@ package net.pterodactylus.sone.web.pages
 
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.test.*
+import net.pterodactylus.sone.test.Matchers.isPostWithId
 import net.pterodactylus.sone.utils.*
 import net.pterodactylus.sone.web.*
 import net.pterodactylus.sone.web.page.*
@@ -42,6 +43,7 @@ class NewPageTest : WebPageTest() {
 
        @Test
        fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
+               unsetCurrentSone()
                val extraPost = mock<Post>().withTime(2000)
                val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
                val postReplies = asList(mock<PostReply>(), mock())
@@ -61,6 +63,7 @@ class NewPageTest : WebPageTest() {
        @Test
        @Suppress("UNCHECKED_CAST")
        fun `posts are paginated properly`() {
+               unsetCurrentSone()
                webInterface.core.preferences.newPostsPerPage = 2
                val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
                whenever(newElements.newPosts()).thenReturn(posts)
@@ -72,6 +75,7 @@ class NewPageTest : WebPageTest() {
        @Test
        @Suppress("UNCHECKED_CAST")
        fun `posts are paginated properly on second page`() {
+               unsetCurrentSone()
                webInterface.core.preferences.newPostsPerPage = 2
                addHttpRequestParameter("page", "1")
                val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
@@ -82,6 +86,44 @@ class NewPageTest : WebPageTest() {
        }
 
        @Test
+       fun `posts are unfiltered if no current Sone is set`() {
+               unsetCurrentSone()
+               val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
+               whenever(newElements.newPosts()).thenReturn(posts)
+               verifyNoRedirect {
+                       assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
+               }
+       }
+
+       @Test
+       fun `posts are filtered if a current Sone is set`() {
+               val posts = listOf(createPost(id = "post1"), createPost(id = "post2"))
+               whenever(newElements.newPosts(currentSone)).thenReturn(posts.last().asList())
+               verifyNoRedirect {
+                       assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
+               }
+       }
+
+       @Test
+       fun `replies are unfiltered if no current Sone is set`() {
+               unsetCurrentSone()
+               val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
+               whenever(newElements.newReplies()).thenReturn(replies)
+               verifyNoRedirect {
+                       assertThat(templateContext["posts"] as Collection<Post>, containsInAnyOrder(isPostWithId("post1"), isPostWithId("post2")))
+               }
+       }
+
+       @Test
+       fun `replies are filtered if a current Sone is set`() {
+               val replies = listOf(createPostReply(id = "reply1", post = createPost(id = "post1")), createPostReply(id = "reply2", post = createPost(id = "post2")))
+               whenever(newElements.newReplies(currentSone)).thenReturn(replies.last().asList())
+               verifyNoRedirect {
+                       assertThat(templateContext["posts"] as Collection<Post>, contains(isPostWithId("post2")))
+               }
+       }
+
+       @Test
        fun `page can be created by dependency injection`() {
                assertThat(baseInjector.getInstance<NewPage>(), notNullValue())
        }