🐛 Fix mark-post-known handler not being registered
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 10 Dec 2019 15:44:21 +0000 (16:44 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 15:59:13 +0000 (16:59 +0100)
Turns out if you do not use constructor or member injection to create
a type, Guice will not notify you about it.

src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostKnownDuringFirstStartHandlerTest.kt

diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Functions.kt
new file mode 100644 (file)
index 0000000..d7fb595
--- /dev/null
@@ -0,0 +1,6 @@
+package net.pterodactylus.sone.utils
+
+import java.util.function.*
+
+/** Allows easy invocation of Java Consumers. */
+operator fun <T> Consumer<T>.invoke(t: T) = accept(t)
index 69144e3..caca76e 100644 (file)
@@ -21,7 +21,9 @@ import com.google.common.eventbus.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.notify.*
+import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.notify.*
+import java.util.function.*
 import javax.inject.*
 
 /**
@@ -29,7 +31,7 @@ import javax.inject.*
  * the [notification manager][NotificationManager] shows a [first start notification]
  * [NotificationManager.hasFirstStartNotification].
  */
-class MarkPostKnownDuringFirstStartHandler(private val notificationManager: NotificationManager, private val markPostAsKnown: (Post) -> Unit) {
+class MarkPostKnownDuringFirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, private val markPostAsKnown: Consumer<Post>) {
 
        @Subscribe
        fun newPostFound(newPostFoundEvent: NewPostFoundEvent) {
index 7571f67..4737267 100644 (file)
 package net.pterodactylus.sone.web.notification
 
 import com.google.inject.*
+import com.google.inject.binder.*
 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.*
 import javax.inject.Singleton
 
@@ -34,12 +36,11 @@ class NotificationHandlerModule : AbstractModule() {
 
        override fun configure() {
                bind(NotificationHandler::class.java).`in`(Singleton::class.java)
+               bind<MarkPostKnownDuringFirstStartHandler>().asSingleton()
        }
 
        @Provides
-       @Singleton
-       fun getMarkPostKnownDuringFirstStartHandler(core: Core, notificationManager: NotificationManager) =
-                       MarkPostKnownDuringFirstStartHandler(notificationManager, core::markPostKnown)
+       fun getMarkPostKnownHandler(core: Core): Consumer<Post> = Consumer { core.markPostKnown(it) }
 
        @Provides
        @Singleton
@@ -84,4 +85,7 @@ class NotificationHandlerModule : AbstractModule() {
        fun getSoneLockedHandler(notificationManager: NotificationManager, @Named("soneLocked") soneLockedNotification: ListNotification<Sone>) =
                        SoneLockedHandler(notificationManager, soneLockedNotification, newScheduledThreadPool(1))
 
+       private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
+       private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
+
 }
index 286648a..28a1719 100644 (file)
@@ -24,6 +24,7 @@ import net.pterodactylus.util.notify.*
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
 import java.io.*
+import java.util.function.Consumer
 import kotlin.test.*
 
 /**
@@ -35,7 +36,7 @@ class MarkPostKnownDuringFirstStartHandlerTest {
        private val eventBus = EventBus()
        private val notificationManager = NotificationManager()
        private val markedPosts = mutableListOf<Post>()
-       private val handler = MarkPostKnownDuringFirstStartHandler(notificationManager) { markedPosts += it }
+       private val handler = MarkPostKnownDuringFirstStartHandler(notificationManager, Consumer { markedPosts += it })
 
        init {
                eventBus.register(handler)