🎨 Use new elements container in AJAX pages
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 1 Jul 2020 20:07:15 +0000 (22:07 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 1 Jul 2020 20:15:31 +0000 (22:15 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
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/AllPagesTest.kt
src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt

index 41ff052..723a166 100644 (file)
@@ -273,7 +273,7 @@ public class WebInterface implements SessionProvider {
 
                pageToadletRegistry.addPage(new RedirectPage<FreenetRequest>("", "index.html"));
                pageToadletRegistry.addPage(new IndexPage(this, loaders, templateRenderer, postVisibilityFilter));
-               pageToadletRegistry.addPage(new NewPage(this, loaders, templateRenderer));
+               pageToadletRegistry.addPage(new NewPage(this, loaders, templateRenderer, newElements));
                pageToadletRegistry.addPage(new CreateSonePage(this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new KnownSonesPage(this, loaders, templateRenderer));
                pageToadletRegistry.addPage(new EditProfilePage(this, loaders, templateRenderer));
@@ -322,7 +322,7 @@ public class WebInterface implements SessionProvider {
                pageToadletRegistry.addPage(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate));
                pageToadletRegistry.addPage(new GetImagePage(this));
                pageToadletRegistry.addPage(new GetTranslationAjaxPage(this));
-               pageToadletRegistry.addPage(new GetStatusAjaxPage(this, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault()));
+               pageToadletRegistry.addPage(new GetStatusAjaxPage(this, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault()));
                pageToadletRegistry.addPage(new GetNotificationsAjaxPage(this));
                pageToadletRegistry.addPage(new DismissNotificationAjaxPage(this));
                pageToadletRegistry.addPage(new CreatePostAjaxPage(this));
index 5d17262..0b31588 100644 (file)
@@ -13,6 +13,7 @@ import net.pterodactylus.sone.template.SoneAccessor
 import net.pterodactylus.sone.text.TimeTextConverter
 import net.pterodactylus.sone.utils.jsonObject
 import net.pterodactylus.sone.utils.toArray
+import net.pterodactylus.sone.web.NewElements
 import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.*
 import java.text.SimpleDateFormat
@@ -24,11 +25,11 @@ import javax.inject.Inject
  * update the web interface in real-time.
  */
 @ToadletPath("getStatus.ajax")
-class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone):
+class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val newElements: NewElements, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone):
                JsonPage(webInterface) {
 
-       @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter):
-                       this(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault())
+       @Inject constructor(webInterface: WebInterface, elementLoader: ElementLoader, newElements: NewElements, timeTextConverter: TimeTextConverter, l10nFilter: L10nFilter):
+                       this(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getDefault())
 
        private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
                this.timeZone = timeZone
@@ -41,8 +42,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"] = webInterface.getNewPosts(currentSone).toJsonPosts()
-                                       this["newReplies"] = webInterface.getNewReplies(currentSone).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 940e6e8..027aa86 100644 (file)
@@ -1,11 +1,18 @@
 package net.pterodactylus.sone.web.pages
 
-import net.pterodactylus.sone.main.*
-import net.pterodactylus.sone.utils.*
-import net.pterodactylus.sone.web.*
-import net.pterodactylus.sone.web.page.*
-import net.pterodactylus.util.template.*
-import javax.inject.*
+import net.pterodactylus.sone.main.Loaders
+import net.pterodactylus.sone.utils.mapPresent
+import net.pterodactylus.sone.utils.paginate
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.NewElements
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.MenuName
+import net.pterodactylus.sone.web.page.SoneRequest
+import net.pterodactylus.sone.web.page.TemplatePath
+import net.pterodactylus.sone.web.page.TemplateRenderer
+import net.pterodactylus.sone.web.page.ToadletPath
+import net.pterodactylus.util.template.TemplateContext
+import javax.inject.Inject
 
 /**
  * Page that displays all new posts and replies. The posts are filtered using
@@ -14,12 +21,12 @@ import javax.inject.*
 @MenuName("New")
 @TemplatePath("/templates/new.html")
 @ToadletPath("new.html")
-class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
+class NewPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val newElements: NewElements) :
                SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.New.Title") {
 
        override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) =
                        getCurrentSone(soneRequest.toadletContext).let { currentSone ->
-                               (soneRequest.webInterface.getNewPosts(currentSone) + soneRequest.webInterface.getNewReplies(currentSone).mapPresent { it.post })
+                               (newElements.newPosts + newElements.newReplies.mapPresent { it.post })
                                                .distinct()
                                                .sortedByDescending { it.time }
                                                .let { posts ->
index 842feb4..5f75108 100644 (file)
@@ -233,6 +233,7 @@ val baseInjector by lazy {
                        FreenetInterface::class.isProvidedByMock(),
                        Template::class.isProvidedByMock(),
                        WebInterface::class.isProvidedByDeepMock(),
-                       TemplateRenderer::class.isProvidedByMock()
+                       TemplateRenderer::class.isProvidedByMock(),
+                       NewElements::class.isProvidedByMock()
        )!!
 }
index f270d71..fb06e19 100644 (file)
@@ -15,6 +15,7 @@ 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
@@ -37,7 +38,11 @@ class GetStatusAjaxPageTest: JsonPageTest("getStatus.ajax", requiresLogin = fals
 
        private val timeTextConverter = mock<TimeTextConverter>()
        private val l10nFilter = mock<L10nFilter>()
-       override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC"))
+       private val newElements = mock<NewElements>().apply {
+               whenever(newPosts).then { this@GetStatusAjaxPageTest.newPosts.values }
+               whenever(newReplies).then { this@GetStatusAjaxPageTest.newReplies.values }
+       }
+       override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, newElements, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC"))
 
        @Before
        fun setupTimeTextConverter() {
index a4d7617..3cf2a75 100644 (file)
@@ -13,7 +13,11 @@ import java.util.Arrays.*
 /**
  * Unit test for [NewPage].
  */
-class NewPageTest : WebPageTest(::NewPage) {
+class NewPageTest : WebPageTest() {
+
+       private val newElements = mock<NewElements>()
+       override val page: SoneTemplatePage
+               get() = NewPage(webInterface, loaders, templateRenderer, newElements)
 
        @Before
        fun setupNumberOfPostsPerPage() {
@@ -43,8 +47,8 @@ class NewPageTest : WebPageTest(::NewPage) {
                val postReplies = asList(mock<PostReply>(), mock())
                whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
                whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
-               whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
-               whenever(webInterface.getNewReplies(currentSone)).thenReturn(postReplies)
+               whenever(newElements.newPosts).thenReturn(posts)
+               whenever(newElements.newReplies).thenReturn(postReplies)
 
                verifyNoRedirect {
                        val renderedPosts = templateContext.get<List<Post>>("posts", List::class.java)
@@ -59,7 +63,7 @@ class NewPageTest : WebPageTest(::NewPage) {
        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(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+               whenever(newElements.newPosts).thenReturn(posts)
                verifyNoRedirect {
                        assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[1], posts[0]))
                }
@@ -71,7 +75,7 @@ class NewPageTest : WebPageTest(::NewPage) {
                webInterface.core.preferences.newPostsPerPage = 2
                addHttpRequestParameter("page", "1")
                val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
-               whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
+               whenever(newElements.newPosts).thenReturn(posts)
                verifyNoRedirect {
                        assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(posts[2]))
                }