From 5cfe9916e6b52b68a403873e35683ce242be4793 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 11 Dec 2019 19:46:11 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Move=20startup=20notificati?= =?utf8?q?on=20into=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 13 ----------- .../sone/web/notification/NotificationHandler.kt | 3 ++- .../web/notification/NotificationHandlerModule.kt | 7 ++++++ .../sone/web/notification/StartupHandler.kt | 6 ++++- .../notification/NotificationHandlerModuleTest.kt | 27 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index cefbfa9..8e21ea5 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -411,19 +411,6 @@ public class WebInterface implements SessionProvider { registerToadlets(); /* notification templates. */ - Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html"); - - final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate); - notificationManager.addNotification(startupNotification); - - ticker.schedule(new Runnable() { - - @Override - public void run() { - startupNotification.dismiss(); - } - }, 2, TimeUnit.MINUTES); - Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html"); final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate); ticker.scheduleAtFixedRate(new Runnable() { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt index dc60079..41e67bd 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -33,5 +33,6 @@ class NotificationHandler @Inject constructor( newVersionHandler: NewVersionHandler, imageInsertHandler: ImageInsertHandler, firstStartHandler: FirstStartHandler, - configNotReadHandler: ConfigNotReadHandler + configNotReadHandler: ConfigNotReadHandler, + startupHandler: StartupHandler ) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt index e94be5b..539f001 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -47,6 +47,7 @@ class NotificationHandlerModule : AbstractModule() { bind().asSingleton() bind().asSingleton() bind().asSingleton() + bind().asSingleton() } @Provides @@ -121,6 +122,12 @@ class NotificationHandlerModule : AbstractModule() { fun getConfigNotReadNotification(loaders: Loaders) = TemplateNotification("config-not-read-notification", loaders.loadTemplate("/templates/notify/configNotReadNotification.html")) + @Provides + @Singleton + @Named("startup") + fun getStartupNotification(loaders: Loaders) = + TemplateNotification("startup-notification", loaders.loadTemplate("/templates/notify/startupNotification.html")) + private inline fun bind(): AnnotatedBindingBuilder = bind(T::class.java) private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/StartupHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/StartupHandler.kt index 392aa5c..c3b69f0 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/StartupHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/StartupHandler.kt @@ -22,11 +22,15 @@ import net.pterodactylus.sone.core.event.* import net.pterodactylus.util.notify.* import java.util.concurrent.* import java.util.concurrent.TimeUnit.* +import javax.inject.* /** * Handler for the [Startup] event notification. */ -class StartupHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification, val ticker: ScheduledExecutorService) { +class StartupHandler @Inject constructor( + private val notificationManager: NotificationManager, + @Named("startup") private val notification: TemplateNotification, + private val ticker: ScheduledExecutorService) { @Subscribe fun startup(startup: Startup) { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt index a061230..477e6e3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt @@ -370,4 +370,31 @@ class NotificationHandlerModuleTest { injector.verifySingletonInstance() } + @Test + fun `startup notification can be created`() { + injector.verifySingletonInstance(named("startup")) + } + + @Test + fun `startup notification has correct ID`() { + assertThat(injector.getInstance(named("startup")).id, equalTo("startup-notification")) + } + + @Test + fun `startup notification is dismissable`() { + assertThat(injector.getInstance(named("startup")).isDismissable, equalTo(true)) + } + + @Test + fun `startup notification loads correct template`() { + loaders.templates += "/templates/notify/startupNotification.html" to "1".asTemplate() + val notification = injector.getInstance(named("startup")) + assertThat(notification.render(), equalTo("1")) + } + + @Test + fun `startup handler is created as singleton`() { + injector.verifySingletonInstance() + } + } -- 2.7.4