From: David ‘Bombe’ Roden Date: Thu, 12 Dec 2019 19:03:58 +0000 (+0100) Subject: ♻️ Move web of trust notification into handler X-Git-Tag: v81^2~5^2~31 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=2ec60e8d6d2efadd7b4d35de3c3d257a0bc5f190;p=Sone.git ♻️ Move web of trust notification into handler --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 8e21ea5..dc25dd4 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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); } /** 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 41e67bd..21a5b37 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -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 ) 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 0d7bb25..74c9f43 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -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().asSingleton() bind().asSingleton() bind().asSingleton() + bind().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 { ticker.schedule(it, 15, SECONDS) } + private inline fun bind(): AnnotatedBindingBuilder = bind(T::class.java) private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/WebOfTrustHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/WebOfTrustHandler.kt index 2f644a6..924d395 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/WebOfTrustHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/WebOfTrustHandler.kt @@ -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) { 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 b84b3ee..ca52c1f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt @@ -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() + private val webOfTrustConnector = mock() private val ticker = mock() 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() } + @Test + fun `web-of-trust notification is created as singleton`() { + injector.verifySingletonInstance(named("webOfTrust")) + } + + @Test + fun `web-of-trust notification has correct ID`() { + assertThat(injector.getInstance(named("webOfTrust")).id, equalTo("wot-missing-notification")) + } + + @Test + fun `web-of-trust notification is dismissable`() { + assertThat(injector.getInstance(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(named("webOfTrust")) + assertThat(notification.render(), equalTo("1")) + } + + @Test + fun `web-of-trust handler is created as singleton`() { + injector.verifySingletonInstance(named("webOfTrust")) + } + + @Test + fun `web-of-trust reacher is created as singleton`() { + injector.verifySingletonInstance(named("webOfTrustReacher")) + } + + @Test + fun `web-of-trust reacher access the wot connector`() { + injector.getInstance(named("webOfTrustReacher")).run() + verify(webOfTrustConnector).ping() + } + + @Test + fun `web-of-trust reschedule is created as singleton`() { + injector.verifySingletonInstance>(named("webOfTrustReschedule")) + } + + @Test + fun `web-of-trust reschedule schedules at the correct delay`() { + val webOfTrustPinger = injector.getInstance() + injector.getInstance>(named("webOfTrustReschedule"))(webOfTrustPinger) + verify(ticker).schedule(ArgumentMatchers.eq(webOfTrustPinger), ArgumentMatchers.eq(15L), ArgumentMatchers.eq(SECONDS)) + } + }