From 79ef6e4644845290d9feb45a06f8588864dd0a83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 8 Feb 2020 13:09:17 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Use=20handler=20for=20local?= =?utf8?q?-reply=20notifications?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 40 +--------- .../sone/web/notification/LocalReplyHandler.kt | 54 ++++++++++++++ .../sone/web/notification/NotificationHandler.kt | 1 + .../web/notification/NotificationHandlerModule.kt | 7 ++ .../sone/web/notification/LocalReplyHandlerTest.kt | 85 ++++++++++++++++++++++ .../notification/NotificationHandlerModuleTest.kt | 29 ++++++++ 6 files changed, 179 insertions(+), 37 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandler.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandlerTest.kt diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index c757599..027fb7f 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -170,7 +170,8 @@ public class WebInterface implements SessionProvider { PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter, NotificationManager notificationManager, @Named("newRemotePost") ListNotification newPostNotification, @Named("newRemotePostReply") ListNotification newReplyNotification, - @Named("localPost") ListNotification localPostNotification) { + @Named("localPost") ListNotification localPostNotification, + @Named("localReply") ListNotification localReplyNotification) { this.sonePlugin = sonePlugin; this.loaders = loaders; this.listNotificationFilter = listNotificationFilter; @@ -190,15 +191,12 @@ public class WebInterface implements SessionProvider { this.newPostNotification = newPostNotification; this.newReplyNotification = newReplyNotification; this.localPostNotification = localPostNotification; + this.localReplyNotification = localReplyNotification; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); this.templateContextFactory = templateContextFactory; templateContextFactory.addTemplateObject("webInterface", this); templateContextFactory.addTemplateObject("formPassword", formPassword); - - /* create notifications. */ - Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html"); - localReplyNotification = new ListNotification<>("local-reply-notification", "replies", localReplyNotificationTemplate, false); } // @@ -514,35 +512,12 @@ public class WebInterface implements SessionProvider { // EVENT HANDLERS // - /** - * Notifies the web interface that a new {@link PostReply} was found. - * - * @param newPostReplyFoundEvent - * The event - */ - @Subscribe - public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) { - PostReply reply = newPostReplyFoundEvent.getPostReply(); - boolean isLocal = reply.getSone().isLocal(); - if (isLocal) { - localReplyNotification.add(reply); - if (!hasFirstStartNotification()) { - notificationManager.addNotification(localReplyNotification); - } - } - } - @Subscribe public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) { removePost(markPostKnownEvent.getPost()); } @Subscribe - public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) { - removeReply(markPostReplyKnownEvent.getPostReply()); - } - - @Subscribe public void postRemoved(PostRemovedEvent postRemovedEvent) { removePost(postRemovedEvent.getPost()); } @@ -551,15 +526,6 @@ public class WebInterface implements SessionProvider { newPostNotification.remove(post); } - @Subscribe - public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) { - removeReply(postReplyRemovedEvent.getPostReply()); - } - - private void removeReply(PostReply reply) { - localReplyNotification.remove(reply); - } - /** * Notifies the web interface that a {@link Sone} is being inserted. * diff --git a/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandler.kt b/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandler.kt new file mode 100644 index 0000000..424295e --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandler.kt @@ -0,0 +1,54 @@ +/** + * Sone - LocalReplyHandler.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.sone.core.event.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.notify.* +import net.pterodactylus.util.notify.* +import javax.inject.* + +/** + * Handler for local replies. + */ +class LocalReplyHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("localReply") private val notification: ListNotification) { + + @Subscribe + fun newReplyFound(event: NewPostReplyFoundEvent) = + event.postReply.onLocal { + notification.add(it) + if (!notificationManager.hasFirstStartNotification()) { + notificationManager.addNotification(notification) + } + } + + @Subscribe + fun replyRemoved(event: PostReplyRemovedEvent) { + notification.remove(event.postReply) + } + + @Subscribe + fun replyMarkedAsKnown(event: MarkPostReplyKnownEvent) { + notification.remove(event.postReply) + } + +} + +private fun PostReply.onLocal(action: (PostReply) -> Unit) = + if (sone.isLocal) action(this) else Unit 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 6e1d216..c55308e 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandler.kt @@ -35,6 +35,7 @@ class NotificationHandler @Inject constructor( soneLockedOnStartupHandler: SoneLockedOnStartupHandler, soneLockedHandler: SoneLockedHandler, localPostHandler: LocalPostHandler, + localReplyHandler: LocalReplyHandler, newVersionHandler: NewVersionHandler, imageInsertHandler: ImageInsertHandler, firstStartHandler: FirstStartHandler, 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 4a91fc7..bb37e09 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt @@ -47,6 +47,7 @@ class NotificationHandlerModule : AbstractModule() { bind().asSingleton() bind().asSingleton() bind().asSingleton() + bind().asSingleton() bind().asSingleton() bind().asSingleton() bind().asSingleton() @@ -100,6 +101,12 @@ class NotificationHandlerModule : AbstractModule() { @Provides @Singleton + @Named("localReply") + fun getLocalReplyNotification(loaders: Loaders) = + ListNotification("local-reply-notification", "replies", loaders.loadTemplate("/templates/notify/newReplyNotification.html"), dismissable = false) + + @Provides + @Singleton @Named("newVersion") fun getNewVersionNotification(loaders: Loaders) = TemplateNotification("new-version-notification", loaders.loadTemplate("/templates/notify/newVersionNotification.html")) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandlerTest.kt new file mode 100644 index 0000000..750d083 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/LocalReplyHandlerTest.kt @@ -0,0 +1,85 @@ +/** + * Sone - LocalReplyHandlerTest.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 net.pterodactylus.sone.core.event.* +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.notify.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.template.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import kotlin.test.* + +/** + * Unit test for [LocalReplyHandler]. + */ +class LocalReplyHandlerTest { + + private val notification = ListNotification("", "", Template()) + private val localReplyHandlerTester = NotificationHandlerTester { LocalReplyHandler(it, notification) } + + @Test + fun `handler does not add reply to notification`() { + localReplyHandlerTester.sendEvent(NewPostReplyFoundEvent(remoteReply)) + assertThat(notification.elements, emptyIterable()) + } + + @Test + fun `handler does add local reply to notification`() { + localReplyHandlerTester.sendEvent(NewPostReplyFoundEvent(localReply)) + assertThat(notification.elements, contains(localReply)) + } + + @Test + fun `handler adds notification to manager`() { + localReplyHandlerTester.sendEvent(NewPostReplyFoundEvent(localReply)) + assertThat(localReplyHandlerTester.notifications, hasItem(notification)) + } + + @Test + fun `handler does not add notification to manager for remote reply`() { + localReplyHandlerTester.sendEvent(NewPostReplyFoundEvent(remoteReply)) + assertThat(localReplyHandlerTester.notifications, not(hasItem(notification))) + } + + @Test + fun `handler does not add notification to manager during first start`() { + localReplyHandlerTester.firstStart() + localReplyHandlerTester.sendEvent(NewPostReplyFoundEvent(localReply)) + assertThat(localReplyHandlerTester.notifications, not(hasItem(notification))) + } + + @Test + fun `handler removes reply from notification if reply is removed`() { + notification.add(localReply) + localReplyHandlerTester.sendEvent(PostReplyRemovedEvent(localReply)) + assertThat(notification.elements, not(hasItem(localReply))) + } + + @Test + fun `handler removes reply from notification if reply is marked as known`() { + notification.add(localReply) + localReplyHandlerTester.sendEvent(MarkPostReplyKnownEvent(localReply)) + assertThat(notification.elements, not(hasItem(localReply))) + } + +} + +private val localReply = emptyPostReply(sone = localSone1) +private val remoteReply = emptyPostReply() 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 138a41e..27f396f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt @@ -269,6 +269,35 @@ class NotificationHandlerModuleTest { } @Test + fun `local-reply notification is not dismissable`() { + assertThat(injector.getInstance>(named("localReply")).isDismissable, equalTo(false)) + } + + @Test + fun `local-reply notification has correct ID`() { + assertThat(injector.getInstance>(named("localReply")).id, equalTo("local-reply-notification")) + } + + @Test + fun `local-reply notification has correct key and template`() { + loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate() + val notification = injector.getInstance>(named("localReply")) + val replies = listOf(emptyPostReply("reply1"), emptyPostReply("reply2")) + replies.forEach(notification::add) + assertThat(notification.render(), equalTo(replies.toString())) + } + + @Test + fun `local-reply notification is created as singleton`() { + injector.verifySingletonInstance>(named("localReply")) + } + + @Test + fun `local-reply handler is created as singleton`() { + injector.verifySingletonInstance() + } + + @Test fun `new-version notification is created as singleton`() { injector.verifySingletonInstance(named("newVersion")) } -- 2.7.4