From: David ‘Bombe’ Roden Date: Thu, 9 Jan 2020 21:18:13 +0000 (+0100) Subject: 🚧 Add handler for marking replies as known during first start X-Git-Tag: v81^2~5^2~11 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=7a4ce0402bb7146ad791fbc52c0cf9b4d6871c91;ds=sidebyside 🚧 Add handler for marking replies as known during first start --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 5bb4e3a..ea357d2 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -532,8 +532,6 @@ public class WebInterface implements SessionProvider { } if (!hasFirstStartNotification()) { notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification); - } else { - getCore().markReplyKnown(reply); } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt new file mode 100644 index 0000000..6a7f083 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandler.kt @@ -0,0 +1,43 @@ +/** + * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +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.* + +/** + * Handler that marks post replies [as known][net.pterodactylus.sone.core.Core.markReplyKnown] + * while the [first start notification][net.pterodactylus.util.notify.NotificationManager.hasFirstStartNotification] + * is shown. + */ +class MarkPostReplyKnownDuringFirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, private val markAsKnown: Consumer) { + + @Subscribe + fun newPostReply(event: NewPostReplyFoundEvent) { + if (notificationManager.hasFirstStartNotification()) { + markAsKnown(event.postReply) + } + } + +} 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 aba74ae..d0e8bf2 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -28,6 +28,7 @@ import javax.inject.* @Suppress("UNUSED_PARAMETER") class NotificationHandler @Inject constructor( markPostKnownDuringFirstStartHandler: MarkPostKnownDuringFirstStartHandler, + markPostReplyKnownDuringFirstStartHandler: MarkPostReplyKnownDuringFirstStartHandler, newSoneHandler: NewSoneHandler, newRemotePostHandler: NewRemotePostHandler, soneLockedOnStartupHandler: SoneLockedOnStartupHandler, 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 cfe5db3..2b147fc 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -40,6 +40,7 @@ class NotificationHandlerModule : AbstractModule() { override fun configure() { bind(NotificationHandler::class.java).`in`(Singleton::class.java) bind().asSingleton() + bind().asSingleton() bind().asSingleton() bind().asSingleton() bind().asSingleton() @@ -59,6 +60,9 @@ class NotificationHandlerModule : AbstractModule() { fun getMarkPostKnownHandler(core: Core): Consumer = Consumer { core.markPostKnown(it) } @Provides + fun getMarkPostReplyKnownHandler(core: Core): Consumer = Consumer { core.markReplyKnown(it) } + + @Provides @Singleton @Named("soneLockedOnStartup") fun getSoneLockedOnStartupNotification(loaders: Loaders) = diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt new file mode 100644 index 0000000..3cb463f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/MarkPostReplyKnownDuringFirstStartHandlerTest.kt @@ -0,0 +1,50 @@ +/** + * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 2020 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +import net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.test.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import java.util.function.* +import kotlin.test.* + +/** + * Unit test for [MarkPostReplyKnownDuringFirstStartHandler]. + */ +class MarkPostReplyKnownDuringFirstStartHandlerTest { + + private val markedAsKnown = mutableListOf() + private val notificationTester = NotificationHandlerTester { MarkPostReplyKnownDuringFirstStartHandler(it, Consumer { markedAsKnown += it }) } + private val postReply = emptyPostReply() + + @Test + fun `post reply is marked as known on new reply during first start`() { + notificationTester.firstStart() + notificationTester.sendEvent(NewPostReplyFoundEvent(postReply)) + assertThat(markedAsKnown, contains(postReply)) + } + + @Test + fun `post reply is not marked as known on new reply if not during first start`() { + notificationTester.sendEvent(NewPostReplyFoundEvent(postReply)) + assertThat(markedAsKnown, not(hasItem(postReply))) + } + +} 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 00b0a19..a4ed7af 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt @@ -83,6 +83,20 @@ class NotificationHandlerModuleTest { } @Test + fun `mark-post-reply-known-during-first-start handler is created as singleton`() { + injector.verifySingletonInstance() + } + + @Test + fun `mark-post-reply-known-during-first-start handler is created with correct action`() { + notificationManager.firstStart() + val handler = injector.getInstance() + val postReply = mock() + handler.newPostReply(NewPostReplyFoundEvent(postReply)) + verify(core).markReplyKnown(postReply) + } + + @Test fun `sone-locked-on-startup handler is created as singleton`() { injector.verifySingletonInstance() } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt new file mode 100644 index 0000000..4d5627f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerTester.kt @@ -0,0 +1,58 @@ +/** + * Sone - NotificationHandlerTester.kt - Copyright © 2019 David ‘Bombe’ Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +import com.google.common.eventbus.* +import net.pterodactylus.util.notify.* + +/** + * Helper for testing event handlers that deal with notifications. It contains + * a notification manager and an [event bus][EventBus] and automatically + * registers the created handler on the event bus. + * + * ``` + * val notification = SomeNotification() + * val notificationTester = NotificationTester { SomeHandler(it, notification) } + * + * fun test() { + * notificationTester.sendEvent(SomeEvent()) + * assertThat(notificationTester.elements, hasItem(notification)) + * } + * ``` + */ +@Suppress("UnstableApiUsage") +class NotificationHandlerTester(createHandler: (NotificationManager) -> Any) { + + private val eventBus = EventBus() + private val notificationManager = NotificationManager() + + /** Returns all notifications of the notification manager. */ + val notifications: Set + get() = notificationManager.notifications + + init { + eventBus.register(createHandler(notificationManager)) + } + + /** Sends an event to the event bus. */ + fun sendEvent(event: Any) = eventBus.post(event) + + /** Sets the first-start notification on the notification manager. */ + fun firstStart() = notificationManager.firstStart() + +}