From: David ‘Bombe’ Roden Date: Wed, 11 Dec 2019 10:47:25 +0000 (+0100) Subject: ♻️ Extract new-version notification handler X-Git-Tag: v81^2~5^2~59 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=05ebea72f846cf147a51938b15e6f0cd93773f4e ♻️ Extract new-version notification handler --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 1170809..96d9d9a 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -179,9 +179,6 @@ public class WebInterface implements SessionProvider { /** Sone locked notification ticker objects. */ private final Map> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap>()); - /** The “new version” notification. */ - private final TemplateNotification newVersionNotification; - /** The “inserting images” notification. */ private final ListNotification 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 diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/NewVersionHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/NewVersionHandler.kt index da6375e..95ef0ef 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NewVersionHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NewVersionHandler.kt @@ -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) { 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 04449e7..631b026 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -29,5 +29,6 @@ class NotificationHandler @Inject constructor( newSoneHandler: NewSoneHandler, newRemotePostHandler: NewRemotePostHandler, soneLockedOnStartupHandler: SoneLockedOnStartupHandler, - soneLockedHandler: SoneLockedHandler + soneLockedHandler: SoneLockedHandler, + newVersionHandler: NewVersionHandler ) 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 8161cfd..09cf0cb 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -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().asSingleton() bind().asSingleton() bind().asSingleton() + bind().asSingleton() } @Provides @@ -79,6 +81,12 @@ class NotificationHandlerModule : AbstractModule() { fun getLocalPostNotification(loaders: Loaders) = ListNotification("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 bind(): AnnotatedBindingBuilder = bind(T::class.java) private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java) 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 d4000b5..6bb02f4 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt @@ -265,4 +265,36 @@ class NotificationHandlerModuleTest { injector.verifySingletonInstance() } + @Test + fun `new-version notification is created as singleton`() { + injector.verifySingletonInstance(named("newVersion")) + } + + @Test + fun `new-version notification has correct ID`() { + assertThat(injector.getInstance(named("newVersion")).id, equalTo("new-version-notification")) + } + + @Test + fun `new-version notification is dismissable`() { + assertThat(injector.getInstance(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(named("newVersion")) + assertThat(notification.render(), equalTo("1")) + } + + @Test + fun `new-version handler can be created`() { + assertThat(injector.getInstance(), notNullValue()) + } + + @Test + fun `new-version handler is created as singleton`() { + injector.verifySingletonInstance() + } + }