♻️ Move config-not-read notification into handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 17:22:33 +0000 (18:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 18:47:28 +0000 (19:47 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/ConfigNotReadHandler.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 7189af3..cefbfa9 100644 (file)
@@ -386,21 +386,6 @@ public class WebInterface implements SessionProvider {
                return from(allNewReplies).filter(replyVisibilityFilter.isVisible(currentSone)).toSet();
        }
 
-       /**
-        * Sets whether Sone was started with a fresh configuration file.
-        *
-        * @param newConfig
-        *            {@code true} if Sone was started with a fresh configuration,
-        *            {@code false} if the existing configuration could be read
-        */
-       public void setNewConfig(boolean newConfig) {
-               if (newConfig && !hasFirstStartNotification()) {
-                       Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
-                       Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
-                       notificationManager.addNotification(configNotReadNotification);
-               }
-       }
-
        //
        // PRIVATE ACCESSORS
        //
index e2a1ada..46961c0 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.*
 
 /**
  * Handler for [ConfigNotRead] events.
  */
-class ConfigNotReadHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification) {
+class ConfigNotReadHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("configNotRead") private val notification: TemplateNotification) {
 
        @Subscribe
        fun configNotRead(configNotRead: ConfigNotRead) {
index 2da6fc8..dc60079 100644 (file)
@@ -32,5 +32,6 @@ class NotificationHandler @Inject constructor(
                soneLockedHandler: SoneLockedHandler,
                newVersionHandler: NewVersionHandler,
                imageInsertHandler: ImageInsertHandler,
-               firstStartHandler: FirstStartHandler
+               firstStartHandler: FirstStartHandler,
+               configNotReadHandler: ConfigNotReadHandler
 )
index 711d902..a3c10a6 100644 (file)
@@ -45,6 +45,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<NewVersionHandler>().asSingleton()
                bind<ImageInsertHandler>().asSingleton()
                bind<FirstStartHandler>().asSingleton()
+               bind<ConfigNotReadHandler>().asSingleton()
        }
 
        @Provides
@@ -113,6 +114,12 @@ class NotificationHandlerModule : AbstractModule() {
        fun getFirstStartNotification(loaders: Loaders) =
                        TemplateNotification("first-start-notification", loaders.loadTemplate("/templates/notify/firstStartNotification.html"))
 
+       @Provides
+       @Singleton
+       @Named("configNotRead")
+       fun getConfigNotReadNotification(loaders: Loaders) =
+                       TemplateNotification("config-not-read-notification", loaders.loadTemplate("/templates/notify/configNotReadNotification.html"))
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index 8a4bcc9..a061230 100644 (file)
@@ -343,4 +343,31 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<FirstStartHandler>()
        }
 
+       @Test
+       fun `config-not-read notification is created as singleton`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("configNotRead"))
+       }
+
+       @Test
+       fun `config-not-read notification has correct ID `() {
+               assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).id, equalTo("config-not-read-notification"))
+       }
+
+       @Test
+       fun `config-not-read notification is dismissable`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("configNotRead")).isDismissable, equalTo(true))
+       }
+
+       @Test
+       fun `config-not-read notification loads correct template`() {
+               loaders.templates += "/templates/notify/configNotReadNotification.html" to "1".asTemplate()
+               val notification = injector.getInstance<TemplateNotification>(named("configNotRead"))
+               assertThat(notification.render(), equalTo("1"))
+       }
+
+       @Test
+       fun `config-not-read handler is created as singleton`() {
+               injector.verifySingletonInstance<ConfigNotReadHandler>()
+       }
+
 }