♻️ Move local-post handling from web interface to handler
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 10 Dec 2019 20:53:56 +0000 (21:53 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 11 Dec 2019 15:59:21 +0000 (16:59 +0100)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/kotlin/net/pterodactylus/sone/web/notification/LocalPostHandler.kt
src/main/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModule.kt
src/test/kotlin/net/pterodactylus/sone/web/notification/NotificationHandlerModuleTest.kt

index b4c5942..1170809 100644 (file)
@@ -203,7 +203,8 @@ public class WebInterface implements SessionProvider {
                        RenderFilter renderFilter,
                        LinkedElementRenderFilter linkedElementRenderFilter,
                        PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter,
-                       NotificationManager notificationManager, @Named("newRemotePost") ListNotification<Post> newPostNotification) {
+                       NotificationManager notificationManager, @Named("newRemotePost") ListNotification<Post> newPostNotification,
+                       @Named("localPost") ListNotification<Post> localPostNotification) {
                this.sonePlugin = sonePlugin;
                this.loaders = loaders;
                this.listNotificationFilter = listNotificationFilter;
@@ -221,6 +222,7 @@ public class WebInterface implements SessionProvider {
                this.translation = translation;
                this.notificationManager = notificationManager;
                this.newPostNotification = newPostNotification;
+               this.localPostNotification = localPostNotification;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
                soneTextParser = new SoneTextParser(getCore(), getCore());
 
@@ -229,9 +231,6 @@ public class WebInterface implements SessionProvider {
                templateContextFactory.addTemplateObject("formPassword", formPassword);
 
                /* create notifications. */
-               Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
-               localPostNotification = new ListNotification<>("local-post-notification", "posts", localPostNotificationTemplate, false);
-
                Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
                newReplyNotification = new ListNotification<>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
 
@@ -676,13 +675,7 @@ public class WebInterface implements SessionProvider {
        public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
                Post post = newPostFoundEvent.getPost();
                boolean isLocal = post.getSone().isLocal();
-               if (isLocal) {
-                       localPostNotification.add(post);
-               }
                if (!hasFirstStartNotification()) {
-                       if (isLocal) {
-                               notificationManager.addNotification(localPostNotification);
-                       }
                        if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
                                mentionNotification.add(post);
                                notificationManager.addNotification(mentionNotification);
@@ -733,7 +726,6 @@ public class WebInterface implements SessionProvider {
 
        private void removePost(Post post) {
                newPostNotification.remove(post);
-               localPostNotification.remove(post);
                if (!localSoneMentionedInNewPostOrReply(post)) {
                        mentionNotification.remove(post);
                }
index 1ff4ea2..a5153ce 100644 (file)
@@ -22,11 +22,12 @@ 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 posts.
  */
-class LocalPostHandler(private val notificationManager: NotificationManager, private val notification: ListNotification<Post>) {
+class LocalPostHandler @Inject constructor(private val notificationManager: NotificationManager, @Named("localPost") private val notification: ListNotification<Post>) {
 
        @Subscribe
        fun newPostFound(newPostFoundEvent: NewPostFoundEvent) {
index 7918758..8161cfd 100644 (file)
@@ -40,6 +40,7 @@ class NotificationHandlerModule : AbstractModule() {
                bind<NewSoneHandler>().asSingleton()
                bind<NewRemotePostHandler>().asSingleton()
                bind<SoneLockedHandler>().asSingleton()
+               bind<LocalPostHandler>().asSingleton()
        }
 
        @Provides
@@ -72,6 +73,12 @@ class NotificationHandlerModule : AbstractModule() {
        fun getScheduledExecutorService() =
                        newScheduledThreadPool(1)
 
+       @Provides
+       @Singleton
+       @Named("localPost")
+       fun getLocalPostNotification(loaders: Loaders) =
+                       ListNotification<Post>("local-post-notification", "posts", loaders.loadTemplate("/templates/notify/newPostNotification.html"), dismissable = false)
+
        private inline fun <reified T> bind(): AnnotatedBindingBuilder<T> = bind(T::class.java)
        private fun ScopedBindingBuilder.asSingleton() = `in`(Singleton::class.java)
 
index c66531b..d4000b5 100644 (file)
@@ -226,4 +226,43 @@ class NotificationHandlerModuleTest {
                injector.verifySingletonInstance<SoneLockedHandler>()
        }
 
+       @Test
+       fun `local-post notification can be created`() {
+               assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")), notNullValue())
+       }
+
+       @Test
+       fun `local-post notification is not dismissable`() {
+               assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).isDismissable, equalTo(false))
+       }
+
+       @Test
+       fun `local-post notification has correct ID`() {
+               assertThat(injector.getInstance<ListNotification<Post>>(named("localPost")).id, equalTo("local-post-notification"))
+       }
+
+       @Test
+       fun `local-post notification has correct key and template`() {
+               loaders.templates += "/templates/notify/newPostNotification.html" to "<% posts>".asTemplate()
+               val notification = injector.getInstance<ListNotification<Post>>(named("localPost"))
+               val posts = listOf(EmptyPost("post1"), EmptyPost("post2"))
+               posts.forEach(notification::add)
+               assertThat(notification.render(), equalTo(posts.toString()))
+       }
+
+       @Test
+       fun `local-post notification is created as singleton`() {
+               injector.verifySingletonInstance<ListNotification<Post>>(named("localPost"))
+       }
+
+       @Test
+       fun `local-post handler can be created`() {
+               assertThat(injector.getInstance<LocalPostHandler>(), notNullValue())
+       }
+
+       @Test
+       fun `local-post handler is created as singleton`() {
+               injector.verifySingletonInstance<LocalPostHandler>()
+       }
+
 }