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;
*/
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);
}
/**
package net.pterodactylus.sone.web.notification
+import net.pterodactylus.sone.freenet.wot.*
import javax.inject.*
/**
imageInsertHandler: ImageInsertHandler,
firstStartHandler: FirstStartHandler,
configNotReadHandler: ConfigNotReadHandler,
- startupHandler: StartupHandler
+ startupHandler: StartupHandler,
+ webOfTrustPinger: WebOfTrustPinger,
+ webOfTrustHandler: WebOfTrustHandler
)
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
bind<FirstStartHandler>().asSingleton()
bind<ConfigNotReadHandler>().asSingleton()
bind<StartupHandler>().asSingleton()
+ bind<WebOfTrustHandler>().asSingleton()
}
@Provides
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)
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) {
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.*
/**
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()
Core::class.isProvidedBy(core),
NotificationManager::class.isProvidedBy(notificationManager),
Loaders::class.isProvidedBy(loaders),
+ WebOfTrustConnector::class.isProvidedBy(webOfTrustConnector),
ScheduledExecutorService::class.withNameIsProvidedBy(ticker, "notification"),
NotificationHandlerModule()
)
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))
+ }
+
}