From c2749a08af5f846c7285c0bb6a930f372bd7f9cb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Nov 2019 16:57:41 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Add=20handler=20for=20all=20notifica?= =?utf8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/notification/NotificationHandler.kt | 36 ++++++++ src/main/resources/i18n/sone.de.properties | 1 + src/main/resources/i18n/sone.en.properties | 1 + src/main/resources/i18n/sone.es.properties | 3 +- src/main/resources/i18n/sone.fr.properties | 3 +- src/main/resources/i18n/sone.ja.properties | 3 +- src/main/resources/i18n/sone.no.properties | 3 +- src/main/resources/i18n/sone.pl.properties | 3 +- src/main/resources/i18n/sone.ru.properties | 3 +- .../notify/soneLockedOnStartupNotification.html | 6 ++ .../web/notification/NotificationHandlerTest.kt | 95 ++++++++++++++++++++++ 11 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt create mode 100644 src/main/resources/templates/notify/soneLockedOnStartupNotification.html create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt new file mode 100644 index 0000000..7f81f9a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -0,0 +1,36 @@ +/** + * Sone - NotificationHandler.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +import com.google.common.eventbus.* +import net.pterodactylus.sone.main.* +import net.pterodactylus.util.notify.* +import javax.inject.* + +/** + * Handler for notifications that can create notifications and register them with an event bus. + */ +@Suppress("UnstableApiUsage") +class NotificationHandler @Inject constructor(private val eventBus: EventBus, private val loaders: Loaders, private val notificationManager: NotificationManager) { + + fun start() { + SoneLockedOnStartupHandler(notificationManager, loaders.loadTemplate("/templates/notify/soneLockedOnStartupNotification.html")) + .also(eventBus::register) + } + +} diff --git a/src/main/resources/i18n/sone.de.properties b/src/main/resources/i18n/sone.de.properties index a2ff8b1..6c91fcd 100644 --- a/src/main/resources/i18n/sone.de.properties +++ b/src/main/resources/i18n/sone.de.properties @@ -461,3 +461,4 @@ Notification.Mention.Text=Sie wurden in diesen Nachrichten erwähnt: Notification.SoneIsInserting.Text=Ihre Sone sone://{0} wird jetzt hoch geladen. Notification.SoneIsInserted.Text=Ihre Sone sone://{0} wurde in {1,number} {1,choice,0#Sekunden|1#Sekunde|1 + <%= Notification.SoneLockedOnStartup.Text|l10n|html> + <%foreach sones sone> + <% sone.niceName|html><%notlast>,<%/notlast><%last>.<%/last> + <%/foreach> + diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTest.kt new file mode 100644 index 0000000..d145038 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTest.kt @@ -0,0 +1,95 @@ +/** + * Sone - NotificationHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +import com.google.common.eventbus.* +import com.google.inject.* +import com.google.inject.Guice.* +import net.pterodactylus.sone.main.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.notify.* +import net.pterodactylus.util.template.* +import net.pterodactylus.util.web.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit test for [NotificationHandler]. + */ +class NotificationHandlerTest { + + private val eventBus = TestEventBus() + private val loaders = TestLoaders() + private val notificationManager = NotificationManager() + private val handler = NotificationHandler(eventBus, loaders, notificationManager) + + @Test + fun `notification handler can be created by guice`() { + val injector = createInjector( + EventBus::class.isProvidedBy(eventBus), + NotificationManager::class.isProvidedBy(notificationManager), + Loaders::class.isProvidedBy(loaders) + ) + assertThat(injector.getInstance(), notNullValue()) + } + + @Test + fun `notification handler registers handler for sone-locked event`() { + handler.start() + assertThat(eventBus.registeredObjects.any { it.javaClass == SoneLockedOnStartupHandler::class.java }, equalTo(true)) + } + + @Test + fun `notification handler loads sone-locked notification template`() { + handler.start() + assertThat(loaders.requestedTemplatePaths.any { it == "/templates/notify/soneLockedOnStartupNotification.html" }, equalTo(true)) + } + +} + +@Suppress("UnstableApiUsage") +private class TestEventBus : EventBus() { + private val _registeredObjects = mutableListOf() + val registeredObjects: List + get() = _registeredObjects + + override fun register(`object`: Any) { + super.register(`object`) + _registeredObjects += `object` + } + +} + +private class TestLoaders : Loaders { + val requestedTemplatePaths = mutableListOf() + + override fun loadTemplate(path: String) = + Template().also { requestedTemplatePaths += path } + + override fun loadStaticPage(basePath: String, prefix: String, mimeType: String) = object : Page { + + override fun getPath() = "" + override fun isPrefixPage() = false + override fun handleRequest(request: REQ, response: Response) = response + + } + + override fun getTemplateProvider() = TemplateProvider { _, _ -> Template() } + +} -- 2.7.4