From: David ‘Bombe’ Roden Date: Sat, 21 Jan 2023 22:52:14 +0000 (+0100) Subject: ♻️ Turn new elements properties into methods X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fda2239d4bd30e90a397d4cc3726250270876856;p=Sone.git ♻️ Turn new elements properties into methods We need that because in the next step a Sone will be handed in to that method so the new elements can be filtered. --- diff --git a/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt b/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt index 6799ada..28e410c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/NewElements.kt @@ -38,13 +38,14 @@ class NewElements @Inject constructor( private val replyVisibilityFilter: ReplyVisibilityFilter ) { - val newPosts: Collection - get() = listOf(newPostNotification, localPostNotification) - .flatMap(ListNotification::elements) - .filter { postVisibilityFilter.isPostVisible(null, it) } + fun newPosts(): Collection = + listOf(newPostNotification, localPostNotification) + .flatMap(ListNotification::elements) + .filter { postVisibilityFilter.isPostVisible(null, it) } + + fun newReplies(): Collection = + listOf(newReplyNotification, localReplyNotification) + .flatMap(ListNotification::elements) + .filter { replyVisibilityFilter.isReplyVisible(null, it) } - val newReplies: Collection - get() = listOf(newReplyNotification, localReplyNotification) - .flatMap(ListNotification::elements) - .filter { replyVisibilityFilter.isReplyVisible(null, it) } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt index 19cd7f9..0c77c38 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt @@ -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() } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt index 027aa86..070fc4c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -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 -> diff --git a/src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt index 34dfe66..ccda1c7 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/NewElementsTest.kt @@ -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)) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt index 17fc9a9..e01b715 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/TestObjects.kt @@ -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) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt index 3cf2a75..fa84e5d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt @@ -47,8 +47,8 @@ class NewPageTest : WebPageTest() { val postReplies = asList(mock(), 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>("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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) - whenever(newElements.newPosts).thenReturn(posts) + whenever(newElements.newPosts()).thenReturn(posts) verifyNoRedirect { assertThat((templateContext["pagination"] as Pagination).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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) - whenever(newElements.newPosts).thenReturn(posts) + whenever(newElements.newPosts()).thenReturn(posts) verifyNoRedirect { assertThat((templateContext["pagination"] as Pagination).items, contains(posts[2])) }