♻️ Extract new-version notification handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 10:47:25 +0000 (11:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 15:59:23 +0000 (16:59 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/NewVersionHandler.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 1170809..96d9d9a 100644 (file)
@@ -179,9 +179,6 @@ public class WebInterface implements SessionProvider {
        /** Sone locked notification ticker objects. */
        private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
 
-       /** The “new version” notification. */
-       private final TemplateNotification newVersionNotification;
-
        /** The “inserting images” notification. */
        private final ListNotification<Image> insertingImagesNotification;
 
@@ -240,9 +237,6 @@ public class WebInterface implements SessionProvider {
                Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
                mentionNotification = new ListNotification<>("mention-notification", "posts", mentionNotificationTemplate, false);
 
-               Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
-               newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
-
                Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
                insertingImagesNotification = new ListNotification<>("inserting-images-notification", "images", insertingImagesTemplate);
 
@@ -792,21 +786,6 @@ public class WebInterface implements SessionProvider {
        }
 
        /**
-        * Notifies the web interface that a new Sone version was found.
-        *
-        * @param updateFoundEvent
-        *            The event
-        */
-       @Subscribe
-       public void updateFound(UpdateFoundEvent updateFoundEvent) {
-               newVersionNotification.set("latestVersion", updateFoundEvent.getVersion());
-               newVersionNotification.set("latestEdition", updateFoundEvent.getLatestEdition());
-               newVersionNotification.set("releaseTime", updateFoundEvent.getReleaseTime());
-               newVersionNotification.set("disruptive", updateFoundEvent.isDisruptive());
-               notificationManager.addNotification(newVersionNotification);
-       }
-
-       /**
         * Notifies the web interface that an image insert was started
         *
         * @param imageInsertStartedEvent
index da6375e..95ef0ef 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 the “new version” notification.
  */
-class NewVersionHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification) {
+class NewVersionHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("newVersion") private val notification: TemplateNotification) {
 
        @Subscribe
        fun newVersionFound(updateFoundEvent: UpdateFoundEvent) {
index 04449e7..631b026 100644 (file)
@@ -29,5 +29,6 @@ class NotificationHandler @Inject constructor(
                newSoneHandler: NewSoneHandler,
                newRemotePostHandler: NewRemotePostHandler,
                soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
-               soneLockedHandler: SoneLockedHandler
+               soneLockedHandler: SoneLockedHandler,
+               newVersionHandler: NewVersionHandler
 )
index 8161cfd..09cf0cb 100644 (file)
@@ -23,6 +23,7 @@ import net.pterodactylus.sone.core.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.main.*
 import net.pterodactylus.sone.notify.*
+import net.pterodactylus.util.notify.*
 import java.util.concurrent.Executors.*
 import java.util.function.*
 import javax.inject.*
@@ -41,6 +42,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<NewRemotePostHandler>().asSingleton()
                bind<SoneLockedHandler>().asSingleton()
                bind<LocalPostHandler>().asSingleton()
+               bind<NewVersionHandler>().asSingleton()
        }
 
        @Provides
@@ -79,6 +81,12 @@ class NotificationHandlerModule : AbstractModule() {
        fun getLocalPostNotification(loaders: Loaders) =
                        ListNotification<Post>("local-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
 
+       @Provides
+       @Singleton
+       @Named("newVersion")
+       fun getNewVersionNotification(loaders: Loaders) =
+                       TemplateNotification("new-version-notification", loaders.loadTemplate("/templates/notify/newVersionNotification.html"))
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index d4000b5..6bb02f4 100644 (file)
@@ -265,4 +265,36 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<LocalPostHandler>()
        }
 
+       @Test
+       fun `new-version notification is created as singleton`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("newVersion"))
+       }
+
+       @Test
+       fun `new-version notification has correct ID`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).id, equalTo("new-version-notification"))
+       }
+
+       @Test
+       fun `new-version notification is dismissable`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("newVersion")).isDismissable, equalTo(true))
+       }
+
+       @Test
+       fun `new-version notification loads correct template`() {
+               loaders.templates += "/templates/notify/newVersionNotification.html" to "1".asTemplate()
+               val notification = injector.getInstance<TemplateNotification>(named("newVersion"))
+               assertThat(notification.render(), equalTo("1"))
+       }
+
+       @Test
+       fun `new-version handler can be created`() {
+               assertThat(injector.getInstance<NewVersionHandler>(), notNullValue())
+       }
+
+       @Test
+       fun `new-version handler is created as singleton`() {
+               injector.verifySingletonInstance<NewVersionHandler>()
+       }
+
 }