♻️ Move first-start notification into handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 15:55:44 +0000 (16:55 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 18:47:23 +0000 (19:47 +0100)
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/FirstStartHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt

index 4fd40f5..3f010f6 100644 (file)
@@ -208,7 +208,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
 
                /* start the web interface! */
                webInterface.start();
-               webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart"))));
                webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig"))));
 
                /* first start? */
index 9bb9250..7189af3 100644 (file)
@@ -387,22 +387,6 @@ public class WebInterface implements SessionProvider {
        }
 
        /**
-        * Sets whether the current start of the plugin is the first start. It is
-        * considered a first start if the configuration file does not exist.
-        *
-        * @param firstStart
-        *            {@code true} if no configuration file existed when Sone was
-        *            loaded, {@code false} otherwise
-        */
-       public void setFirstStart(boolean firstStart) {
-               if (firstStart) {
-                       Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
-                       Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
-                       notificationManager.addNotification(firstStartNotification);
-               }
-       }
-
-       /**
         * Sets whether Sone was started with a fresh configuration file.
         *
         * @param newConfig
index c698846..aceda7e 100644 (file)
@@ -20,11 +20,12 @@ package net.pterodactylus.sone.web.notification
 import com.google.common.eventbus.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.util.notify.*
+import javax.inject.*
 
 /**
  * Handles the notification shown on first start of Sone.
  */
-class FirstStartHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification) {
+class FirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("firstStart") private val notification: TemplateNotification) {
 
        @Subscribe
        fun firstStart(firstStart: FirstStart) {
index 783a9f6..2da6fc8 100644 (file)
@@ -31,5 +31,6 @@ class NotificationHandler @Inject constructor(
                soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
                soneLockedHandler: SoneLockedHandler,
                newVersionHandler: NewVersionHandler,
-               imageInsertHandler: ImageInsertHandler
+               imageInsertHandler: ImageInsertHandler,
+               firstStartHandler: FirstStartHandler
 )
index 3728620..711d902 100644 (file)
@@ -44,6 +44,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<LocalPostHandler>().asSingleton()
                bind<NewVersionHandler>().asSingleton()
                bind<ImageInsertHandler>().asSingleton()
+               bind<FirstStartHandler>().asSingleton()
        }
 
        @Provides
@@ -106,6 +107,12 @@ class NotificationHandlerModule : AbstractModule() {
        fun getImageInsertedNotification(loaders: Loaders) =
                        ListNotification<Image>("inserted-images-notification", "images", loaders.loadTemplate("/templates/notify/inserted-images-notification.html"), dismissable = true)
 
+       @Provides
+       @Singleton
+       @Named("firstStart")
+       fun getFirstStartNotification(loaders: Loaders) =
+                       TemplateNotification("first-start-notification", loaders.loadTemplate("/templates/notify/firstStartNotification.html"))
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index 1410ed3..8a4bcc9 100644 (file)
@@ -316,4 +316,31 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<ImageInsertHandler>()
        }
 
+       @Test
+       fun `first-start notification is created as singleton`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("firstStart"))
+       }
+
+       @Test
+       fun `first-start notification has correct ID`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).id, equalTo("first-start-notification"))
+       }
+
+       @Test
+       fun `first-start notification is dismissable`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("firstStart")).isDismissable, equalTo(true))
+       }
+
+       @Test
+       fun `first-start notification loads correct template`() {
+               loaders.templates += "/templates/notify/firstStartNotification.html" to "1".asTemplate()
+               val notification = injector.getInstance<TemplateNotification>(named("firstStart"))
+               assertThat(notification.render(), equalTo("1"))
+       }
+
+       @Test
+       fun `first-start handler is created as singleton`() {
+               injector.verifySingletonInstance<FirstStartHandler>()
+       }
+
 }