✨ Return filtered elements for get-status request
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 10:36:29 +0000 (11:36 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 22 Jan 2023 10:36:29 +0000 (11:36 +0100)
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt

index 0c77c38..c0a2c4e 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(currentSone).toJsonPosts()
+                                       this["newReplies"] = newElements.newReplies(currentSone).toJsonReplies()
                                        this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
                                }
                        }
index 601ce38..900c4df 100644 (file)
@@ -16,7 +16,6 @@ import net.pterodactylus.sone.test.whenever
 import net.pterodactylus.sone.text.TimeText
 import net.pterodactylus.sone.text.TimeTextConverter
 import net.pterodactylus.sone.utils.jsonArray
-import net.pterodactylus.sone.web.NewElements
 import net.pterodactylus.sone.web.baseInjector
 import net.pterodactylus.util.notify.Notification
 import org.hamcrest.MatcherAssert.assertThat
@@ -31,6 +30,8 @@ import org.junit.Rule
 import org.junit.Test
 import org.mockito.ArgumentMatchers.any
 import org.mockito.ArgumentMatchers.anyLong
+import org.mockito.ArgumentMatchers.isNull
+import org.mockito.Mockito.verify
 import java.util.Locale.ENGLISH
 import java.util.TimeZone
 
@@ -114,23 +115,47 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals
        }
 
        @Test
-       fun `page returns new posts`() {
+       fun `page returns new posts without current Sone`() {
                addNewPost("post1", "sone1", 1000)
                addNewPost("post2", "sone2", 2000, "sone1")
+               unsetCurrentSone()
                assertThat(json.get("newPosts")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
                                mapOf("id" to "post1", "sone" to "sone1", "time" to "1000", "recipient" to null),
                                mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1")
                ))
+               verify(newElements).newPosts(isNull())
+       }
+
+       @Test
+       fun `page returns new posts with current Sone`() {
+               addNewPost("post1", "sone1", 1000)
+               addNewPost("post2", "sone2", 2000, "sone1")
+               whenever(newElements.newPosts(currentSone)).thenReturn(listOf(newPosts["post2"]!!))
+               assertThat(json.get("newPosts")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
+                               mapOf("id" to "post2", "sone" to "sone2", "time" to "2000", "recipient" to "sone1")
+               ))
        }
 
        @Test
-       fun `page returns new replies`() {
+       fun `page returns new replies without current Sone`() {
                addNewReply("reply1", "sone1", "post1", "sone11")
                addNewReply("reply2", "sone2", "post2", "sone22")
+               unsetCurrentSone()
                assertThat(json.get("newReplies")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
                                mapOf<String, String?>("id" to "reply1", "sone" to "sone1", "post" to "post1", "postSone" to "sone11"),
                                mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22")
                ))
+               verify(newElements).newPosts(isNull())
+       }
+
+       @Test
+       fun `page returns new replies with current Sone`() {
+               addNewReply("reply1", "sone1", "post1", "sone11")
+               addNewReply("reply2", "sone2", "post2", "sone22")
+               whenever(newElements.newReplies(currentSone)).thenReturn(listOf(newReplies["reply2"]!!))
+               assertThat(json.get("newReplies")!!.elements().asSequence().map { it.toMap() }.toList(), containsInAnyOrder(
+                       mapOf("id" to "reply2", "sone" to "sone2", "post" to "post2", "postSone" to "sone22")
+               ))
        }
 
        @Test