♻️ Turn new elements properties into methods
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2023 22:52:14 +0000 (23:52 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 21 Jan 2023 22:52:14 +0000 (23:52 +0100)
We need that because in the next step a Sone will be handed in to that method
so the new elements can be filtered.

src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt
src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt

index 6799ada..28e410c 100644 (file)
@@ -38,13 +38,14 @@ class NewElements @Inject constructor(
                private val replyVisibilityFilter: ReplyVisibilityFilter
 ) {
 
-       val newPosts: Collection<Post>
-               get() = listOf(newPostNotification, localPostNotification)
-                               .flatMap(ListNotification<Post>::elements)
-                               .filter { postVisibilityFilter.isPostVisible(null, it) }
+       fun newPosts(): Collection<Post> =
+               listOf(newPostNotification, localPostNotification)
+                       .flatMap(ListNotification<Post>::elements)
+                       .filter { postVisibilityFilter.isPostVisible(null, it) }
+
+       fun newReplies(): Collection<PostReply> =
+               listOf(newReplyNotification, localReplyNotification)
+                       .flatMap(ListNotification<PostReply>::elements)
+                       .filter { replyVisibilityFilter.isReplyVisible(null, it) }
 
-       val newReplies: Collection<PostReply>
-               get() = listOf(newReplyNotification, localReplyNotification)
-                               .flatMap(ListNotification<PostReply>::elements)
-                               .filter { replyVisibilityFilter.isReplyVisible(null, it) }
 }
index 19cd7f9..0c77c38 100644 (file)
@@ -44,8 +44,8 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: E
                                        this["options"] = currentSone?.options?.toJsonOptions() ?: jsonObject {}
                                        this["notificationHash"] = webInterface.getNotifications(currentSone).sortedBy { it.createdTime }.hashCode()
                                        this["sones"] = request.httpRequest.getParam("soneIds").split(',').mapNotNull(core::getSone).plus(currentSone).filterNotNull().toJsonSones()
-                                       this["newPosts"] = newElements.newPosts.toJsonPosts()
-                                       this["newReplies"] = newElements.newReplies.toJsonReplies()
+                                       this["newPosts"] = newElements.newPosts().toJsonPosts()
+                                       this["newReplies"] = newElements.newReplies().toJsonReplies()
                                        this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
                                }
                        }
index 027aa86..070fc4c 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() + newElements.newReplies().mapPresent { it.post })
                                                .distinct()
                                                .sortedByDescending { it.time }
                                                .let { posts ->
index 34dfe66..ccda1c7 100644 (file)
@@ -64,24 +64,24 @@ class NewElementsTest {
 
        @Test
        fun `new posts include new and local posts`() {
-               assertThat(newElements.newPosts, containsInAnyOrder(post1, post2))
+               assertThat(newElements.newPosts(), containsInAnyOrder(post1, post2))
        }
 
        @Test
        fun `new posts are filtered`() {
                val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, matchThisPost(post2), showAllReplies)
-               assertThat(newElements.newPosts, contains(post2))
+               assertThat(newElements.newPosts(), contains(post2))
        }
 
        @Test
        fun `new replies include new and local replies`() {
-               assertThat(newElements.newReplies, containsInAnyOrder(reply1, reply2))
+               assertThat(newElements.newReplies(), containsInAnyOrder(reply1, reply2))
        }
 
        @Test
        fun `new replies are filtered`() {
                val newElements = NewElements(newPostNotification, newReplyNotification, localPostNotification, localReplyNotification, showAllPosts, matchThisReply(reply2))
-               assertThat(newElements.newReplies, containsInAnyOrder(reply2))
+               assertThat(newElements.newReplies(), containsInAnyOrder(reply2))
        }
 
 }
index 17fc9a9..e01b715 100644 (file)
@@ -104,8 +104,8 @@ open class TestObjects {
                        linkedElements[it.getArgument(0)] ?: LinkedElement(it.getArgument(0), loading = true)
                }
 
-               whenever(newElements.newPosts).then { newPosts.values }
-               whenever(newElements.newReplies).then { newReplies.values }
+               whenever(newElements.newPosts()).then { newPosts.values }
+               whenever(newElements.newReplies()).then { newReplies.values }
 
                whenever(currentSone.options).thenReturn(DefaultSoneOptions())
                currentSone.mock("soneId", "Sone_Id", true, 1000, idle)
index 3cf2a75..fa84e5d 100644 (file)
@@ -47,8 +47,8 @@ class NewPageTest : WebPageTest() {
                val postReplies = asList(mock<PostReply>(), mock())
                whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
                whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
-               whenever(newElements.newPosts).thenReturn(posts)
-               whenever(newElements.newReplies).thenReturn(postReplies)
+               whenever(newElements.newPosts()).thenReturn(posts)
+               whenever(newElements.newReplies()).thenReturn(postReplies)
 
                verifyNoRedirect {
                        val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
@@ -63,7 +63,7 @@ class NewPageTest : WebPageTest() {
        fun `posts are paginated properly`() {
                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)
+               whenever(newElements.newPosts()).thenReturn(posts)
                verifyNoRedirect {
                        assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
                }
@@ -75,7 +75,7 @@ class NewPageTest : WebPageTest() {
                webInterface.core.preferences.newPostsPerPage = 2
                addHttpRequestParameter("page", "1")
                val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
-               whenever(newElements.newPosts).thenReturn(posts)
+               whenever(newElements.newPosts()).thenReturn(posts)
                verifyNoRedirect {
                        assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
                }