From 05d126a69fbd464bea6aa974cff52dc4e5d5b2a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 1 Jul 2020 22:07:15 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Use=20new=20elements=20container?= =?utf8?q?=20in=20AJAX=20pages?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 4 ++-- .../sone/web/ajax/GetStatusAjaxPage.kt | 11 ++++++----- .../net/pterodactylus/sone/web/pages/NewPage.kt | 23 ++++++++++++++-------- .../net/pterodactylus/sone/web/AllPagesTest.kt | 3 ++- .../sone/web/ajax/GetStatusAjaxPageTest.kt | 7 ++++++- .../pterodactylus/sone/web/pages/NewPageTest.kt | 14 ++++++++----- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 41ff052..723a166 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -273,7 +273,7 @@ public class WebInterface implements SessionProvider { pageToadletRegistry.addPage(new RedirectPage("", "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("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)); 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 5d17262..0b31588 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt @@ -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() } } 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 940e6e8..027aa86 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -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 -> diff --git a/src/test/kotlin/net/pterodactylus/sone/web/AllPagesTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/AllPagesTest.kt index 842feb4..5f75108 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/AllPagesTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/AllPagesTest.kt @@ -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() )!! } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt index f270d71..fb06e19 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPageTest.kt @@ -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() private val l10nFilter = mock() - override var page: JsonPage = GetStatusAjaxPage(webInterface, elementLoader, timeTextConverter, l10nFilter, TimeZone.getTimeZone("UTC")) + private val newElements = mock().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() { 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 a4d7617..3cf2a75 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt @@ -13,7 +13,11 @@ import java.util.Arrays.* /** * Unit test for [NewPage]. */ -class NewPageTest : WebPageTest(::NewPage) { +class NewPageTest : WebPageTest() { + + private val newElements = mock() + 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(), 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>("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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) - whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts) + whenever(newElements.newPosts).thenReturn(posts) verifyNoRedirect { assertThat((templateContext["pagination"] as Pagination).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().withTime(2000), mock().withTime(3000), mock().withTime(1000)) - whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts) + whenever(newElements.newPosts).thenReturn(posts) verifyNoRedirect { assertThat((templateContext["pagination"] as Pagination).items, contains(posts[2])) } -- 2.7.4