♻️ Move web of trust notification into handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 12 Dec 2019 19:03:58 +0000 (20:03 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 12 Dec 2019 19:11:59 +0000 (20:11 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/WebOfTrustHandler.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt

index 8e21ea5..dc25dd4 100644 (file)
@@ -29,7 +29,6 @@ import java.util.TimeZone;
 import java.util.UUID;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
 import java.util.logging.Logger;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -409,23 +408,6 @@ public class WebInterface implements SessionProvider {
         */
        public void start() {
                registerToadlets();
-
-               /* notification templates. */
-               Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
-               final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
-               ticker.scheduleAtFixedRate(new Runnable() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void run() {
-                               if (getCore().getIdentityManager().isConnected()) {
-                                       wotMissingNotification.dismiss();
-                               } else {
-                                       notificationManager.addNotification(wotMissingNotification);
-                               }
-                       }
-
-               }, 15, 15, TimeUnit.SECONDS);
        }
 
        /**
index 41e67bd..21a5b37 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web.notification
 
+import net.pterodactylus.sone.freenet.wot.*
 import javax.inject.*
 
 /**
@@ -34,5 +35,7 @@ class NotificationHandler @Inject constructor(
                imageInsertHandler: ImageInsertHandler,
                firstStartHandler: FirstStartHandler,
                configNotReadHandler: ConfigNotReadHandler,
-               startupHandler: StartupHandler
+               startupHandler: StartupHandler,
+               webOfTrustPinger: WebOfTrustPinger,
+               webOfTrustHandler: WebOfTrustHandler
 )
index 0d7bb25..74c9f43 100644 (file)
@@ -21,11 +21,12 @@ import com.google.inject.*
 import com.google.inject.binder.*
 import net.pterodactylus.sone.core.*
 import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.freenet.wot.*
 import net.pterodactylus.sone.main.*
 import net.pterodactylus.sone.notify.*
 import net.pterodactylus.util.notify.*
 import java.util.concurrent.*
-import java.util.concurrent.Executors.*
+import java.util.concurrent.TimeUnit.*
 import java.util.function.*
 import javax.inject.*
 import javax.inject.Singleton
@@ -48,6 +49,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<FirstStartHandler>().asSingleton()
                bind<ConfigNotReadHandler>().asSingleton()
                bind<StartupHandler>().asSingleton()
+               bind<WebOfTrustHandler>().asSingleton()
        }
 
        @Provides
@@ -124,6 +126,24 @@ class NotificationHandlerModule : AbstractModule() {
        fun getStartupNotification(loaders: Loaders) =
                        TemplateNotification("startup-notification", loaders.loadTemplate("/templates/notify/startupNotification.html"))
 
+       @Provides
+       @Singleton
+       @Named("webOfTrust")
+       fun getWebOfTrustNotification(loaders: Loaders) =
+                       TemplateNotification("wot-missing-notification", loaders.loadTemplate("/templates/notify/wotMissingNotification.html"))
+
+       @Provides
+       @Singleton
+       @Named("webOfTrustReacher")
+       fun getWebOfTrustReacher(webOfTrustConnector: WebOfTrustConnector): Runnable =
+                       Runnable { webOfTrustConnector.ping() }
+
+       @Provides
+       @Singleton
+       @Named("webOfTrustReschedule")
+       fun getWebOfTrustReschedule(@Named("notification") ticker: ScheduledExecutorService) =
+                       Consumer<Runnable> { ticker.schedule(it, 15, SECONDS) }
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index 2f644a6..924d395 100644 (file)
@@ -20,12 +20,13 @@ 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 web of trust-related notifications and the [WebOfTrustAppeared]
  * and [WebOfTrustDisappeared] events.
  */
-class WebOfTrustHandler(private val notificationManager: NotificationManager, private val notification: TemplateNotification) {
+class WebOfTrustHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("webOfTrust") private val notification: TemplateNotification) {
 
        @Subscribe
        fun webOfTrustAppeared(webOfTrustAppeared: WebOfTrustAppeared) {
index b84b3ee..ca52c1f 100644 (file)
@@ -25,16 +25,21 @@ import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.data.Post.*
 import net.pterodactylus.sone.data.impl.*
+import net.pterodactylus.sone.freenet.wot.*
 import net.pterodactylus.sone.main.*
 import net.pterodactylus.sone.notify.*
 import net.pterodactylus.sone.test.*
 import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.notify.*
 import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers
 import org.hamcrest.Matchers.*
+import org.mockito.*
 import org.mockito.Mockito.*
 import java.io.*
 import java.util.concurrent.*
+import java.util.concurrent.TimeUnit.*
+import java.util.function.*
 import kotlin.test.*
 
 /**
@@ -43,6 +48,7 @@ import kotlin.test.*
 class NotificationHandlerModuleTest {
 
        private val core = mock<Core>()
+       private val webOfTrustConnector = mock<WebOfTrustConnector>()
        private val ticker = mock<ScheduledExecutorService>()
        private val notificationManager = NotificationManager()
        private val loaders = TestLoaders()
@@ -50,6 +56,7 @@ class NotificationHandlerModuleTest {
                        Core::class.isProvidedBy(core),
                        NotificationManager::class.isProvidedBy(notificationManager),
                        Loaders::class.isProvidedBy(loaders),
+                       WebOfTrustConnector::class.isProvidedBy(webOfTrustConnector),
                        ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
                        NotificationHandlerModule()
        )
@@ -400,4 +407,54 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<StartupHandler>()
        }
 
+       @Test
+       fun `web-of-trust notification is created as singleton`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
+       }
+
+       @Test
+       fun `web-of-trust notification has correct ID`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).id, equalTo("wot-missing-notification"))
+       }
+
+       @Test
+       fun `web-of-trust notification is dismissable`() {
+               assertThat(injector.getInstance<TemplateNotification>(named("webOfTrust")).isDismissable, equalTo(true))
+       }
+
+       @Test
+       fun `web-of-trust notification loads correct template`() {
+               loaders.templates += "/templates/notify/wotMissingNotification.html" to "1".asTemplate()
+               val notification = injector.getInstance<TemplateNotification>(named("webOfTrust"))
+               assertThat(notification.render(), equalTo("1"))
+       }
+
+       @Test
+       fun `web-of-trust handler is created as singleton`() {
+               injector.verifySingletonInstance<TemplateNotification>(named("webOfTrust"))
+       }
+
+       @Test
+       fun `web-of-trust reacher is created as singleton`() {
+               injector.verifySingletonInstance<Runnable>(named("webOfTrustReacher"))
+       }
+
+       @Test
+       fun `web-of-trust reacher access the wot connector`() {
+               injector.getInstance<Runnable>(named("webOfTrustReacher")).run()
+               verify(webOfTrustConnector).ping()
+       }
+
+       @Test
+       fun `web-of-trust reschedule is created as singleton`() {
+               injector.verifySingletonInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))
+       }
+
+       @Test
+       fun `web-of-trust reschedule schedules at the correct delay`() {
+               val webOfTrustPinger = injector.getInstance<WebOfTrustPinger>()
+               injector.getInstance<Consumer<Runnable>>(named("webOfTrustReschedule"))(webOfTrustPinger)
+               verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS))
+       }
+
 }