LinkedElementRenderFilter linkedElementRenderFilter,
PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter,
NotificationManager notificationManager, @Named("newRemotePost") ListNotification<Post> newPostNotification,
+ @Named("newRemotePostReply") ListNotification<PostReply> newReplyNotification,
@Named("localPost") ListNotification<Post> localPostNotification) {
this.sonePlugin = sonePlugin;
this.loaders = loaders;
this.translation = translation;
this.notificationManager = notificationManager;
this.newPostNotification = newPostNotification;
+ this.newReplyNotification = newReplyNotification;
this.localPostNotification = localPostNotification;
formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
templateContextFactory.addTemplateObject("formPassword", formPassword);
/* create notifications. */
- Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
- newReplyNotification = new ListNotification<>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
-
Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
localReplyNotification = new ListNotification<>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
}
boolean isLocal = reply.getSone().isLocal();
if (isLocal) {
localReplyNotification.add(reply);
- } else {
- newReplyNotification.add(reply);
- }
- if (!hasFirstStartNotification()) {
- notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(localReplyNotification);
+ }
}
}
}
private void removeReply(PostReply reply) {
- newReplyNotification.remove(reply);
localReplyNotification.remove(reply);
}
markPostReplyKnownDuringFirstStartHandler: MarkPostReplyKnownDuringFirstStartHandler,
newSoneHandler: NewSoneHandler,
newRemotePostHandler: NewRemotePostHandler,
+ remotePostReplyHandler: RemotePostReplyHandler,
soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
soneLockedHandler: SoneLockedHandler,
newVersionHandler: NewVersionHandler,
bind<SoneLockedOnStartupHandler>().asSingleton()
bind<NewSoneHandler>().asSingleton()
bind<NewRemotePostHandler>().asSingleton()
+ bind<RemotePostReplyHandler>().asSingleton()
bind<SoneLockedHandler>().asSingleton()
bind<LocalPostHandler>().asSingleton()
bind<NewVersionHandler>().asSingleton()
@Provides
@Singleton
+ @Named("newRemotePostReply")
+ fun getNewRemotePostReplyNotification(loaders: Loaders) =
+ ListNotification<PostReply>("new-reply-notification", "replies", loaders.loadTemplate("/templates/notify/newReplyNotification.html"), dismissable = false)
+
+ @Provides
+ @Singleton
@Named("soneLocked")
fun getSoneLockedNotification(loaders: Loaders) =
ListNotification<Sone>("sones-locked-notification", "sones", loaders.loadTemplate("/templates/notify/lockedSonesNotification.html"), dismissable = true)
--- /dev/null
+/**
+ * Sone - RemotePostReplyHandler.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 <http://www.gnu.org/licenses/>.
+ */
+
+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 javax.inject.*
+
+/**
+ * Handler for remote replies.
+ */
+class RemotePostReplyHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("newRemotePostReply") private val notification: ListNotification<PostReply>) {
+
+ @Subscribe
+ fun newPostReplyFound(event: NewPostReplyFoundEvent) {
+ event.postReply.let { postReply ->
+ postReply.sone.isLocal.onFalse {
+ if (!notificationManager.hasFirstStartNotification()) {
+ notification.add(event.postReply)
+ notificationManager.addNotification(notification)
+ }
+ }
+ }
+ }
+
+ @Subscribe
+ fun postReplyRemoved(event: PostReplyRemovedEvent) {
+ notification.remove(event.postReply)
+ }
+
+ @Subscribe
+ fun postReplyMarkedAsKnown(event: MarkPostReplyKnownEvent) {
+ notification.remove(event.postReply)
+ }
+
+}
}
@Test
+ fun `remote-post handler is created as singleton`() {
+ injector.verifySingletonInstance<RemotePostReplyHandler>()
+ }
+
+ @Test
+ fun `new-remote-post-reply notification is created as singleton`() {
+ injector.verifySingletonInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
+ }
+
+ @Test
+ fun `new-remote-post-reply notification has correct ID`() {
+ assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).id, equalTo("new-reply-notification"))
+ }
+
+ @Test
+ fun `new-remote-post-reply notification is not dismissable`() {
+ assertThat(injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply")).isDismissable, equalTo(false))
+ }
+
+ @Test
+ fun `new-remote-post-reply notification has correct key and template`() {
+ loaders.templates += "/templates/notify/newReplyNotification.html" to "<% replies>".asTemplate()
+ val notification = injector.getInstance<ListNotification<PostReply>>(named("newRemotePostReply"))
+ val postReplies = listOf(emptyPostReply(), emptyPostReply())
+ postReplies.forEach(notification::add)
+ assertThat(notification.render(), equalTo(postReplies.toString()))
+ }
+
+ @Test
fun `sone-locked notification is created as singleton`() {
injector.verifySingletonInstance<ListNotification<Sone>>(named("soneLocked"))
}
--- /dev/null
+/**
+ * Sone - RemotePostReplyHandler.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 <http://www.gnu.org/licenses/>.
+ */
+
+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.notify.*
+import net.pterodactylus.util.template.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import kotlin.test.*
+
+/**
+ * Unit test for [RemotePostReplyHandler].
+ */
+class RemotePostReplyHandlerTest {
+
+ private val notification = ListNotification<PostReply>("", "", Template())
+ private val notificationHandlerTester = NotificationHandlerTester { RemotePostReplyHandler(it, notification) }
+ private val postReply = emptyPostReply()
+
+ @Test
+ fun `reply is added to notification on new reply`() {
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notification.elements, hasItem<PostReply>(postReply))
+ }
+
+ @Test
+ fun `notification is added to manager on new reply`() {
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notificationHandlerTester.notifications, hasItem<Notification>(notification))
+ }
+
+ @Test
+ fun `reply is not added to notification on new reply during first start`() {
+ notificationHandlerTester.firstStart()
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notification.elements, not(hasItem<PostReply>(postReply)))
+ }
+
+ @Test
+ fun `notification is not added to manager on new reply during first start`() {
+ notificationHandlerTester.firstStart()
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notificationHandlerTester.notifications, not(hasItem<Notification>(notification)))
+ }
+
+ @Test
+ fun `reply is not added to notification on new local reply`() {
+ val postReply = emptyPostReply(sone = localSone1)
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notification.elements, not(hasItem<PostReply>(postReply)))
+ }
+
+ @Test
+ fun `notification is not added to manager on new local reply`() {
+ val postReply = emptyPostReply(sone = localSone1)
+ notificationHandlerTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(notificationHandlerTester.notifications, not(hasItem<Notification>(notification)))
+ }
+
+ @Test
+ fun `reply is removed from notification when removed`() {
+ notification.add(postReply)
+ notificationHandlerTester.sendEvent(PostReplyRemovedEvent(postReply))
+ assertThat(notification.elements, not(hasItem<PostReply>(postReply)))
+ }
+
+ @Test
+ fun `reply is removed from notification when marked as known`() {
+ notification.add(postReply)
+ notificationHandlerTester.sendEvent(MarkPostReplyKnownEvent(postReply))
+ assertThat(notification.elements, not(hasItem<PostReply>(postReply)))
+ }
+
+}