♻️ Extract handler for sone-locked notification
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
index e31d9a8..b4c5942 100644 (file)
@@ -35,6 +35,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.logging.Logger;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import javax.inject.Named;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.ElementLoader;
@@ -124,7 +125,7 @@ public class WebInterface implements SessionProvider {
        private final Loaders loaders;
 
        /** The notification manager. */
-       private final NotificationManager notificationManager = new NotificationManager();
+       private final NotificationManager notificationManager;
 
        /** The Sone plugin. */
        private final SonePlugin sonePlugin;
@@ -157,9 +158,6 @@ public class WebInterface implements SessionProvider {
        private final MetricRegistry metricRegistry;
        private final Translation translation;
 
-       /** The “new Sone” notification. */
-       private final ListNotification<Sone> newSoneNotification;
-
        /** The “new post” notification. */
        private final ListNotification<Post> newPostNotification;
 
@@ -181,9 +179,6 @@ public class WebInterface implements SessionProvider {
        /** Sone locked notification ticker objects. */
        private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
 
-       /** The “Sone locked” notification. */
-       private final ListNotification<Sone> lockedSonesNotification;
-
        /** The “new version” notification. */
        private final TemplateNotification newVersionNotification;
 
@@ -207,7 +202,8 @@ public class WebInterface implements SessionProvider {
                        ParserFilter parserFilter, ShortenFilter shortenFilter,
                        RenderFilter renderFilter,
                        LinkedElementRenderFilter linkedElementRenderFilter,
-                       PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter) {
+                       PageToadletRegistry pageToadletRegistry, MetricRegistry metricRegistry, Translation translation, L10nFilter l10nFilter,
+                       NotificationManager notificationManager, @Named("newRemotePost") ListNotification<Post> newPostNotification) {
                this.sonePlugin = sonePlugin;
                this.loaders = loaders;
                this.listNotificationFilter = listNotificationFilter;
@@ -223,6 +219,8 @@ public class WebInterface implements SessionProvider {
                this.metricRegistry = metricRegistry;
                this.l10nFilter = l10nFilter;
                this.translation = translation;
+               this.notificationManager = notificationManager;
+               this.newPostNotification = newPostNotification;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
                soneTextParser = new SoneTextParser(getCore(), getCore());
 
@@ -231,12 +229,6 @@ public class WebInterface implements SessionProvider {
                templateContextFactory.addTemplateObject("formPassword", formPassword);
 
                /* create notifications. */
-               Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
-               newSoneNotification = new ListNotification<>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
-
-               Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
-               newPostNotification = new ListNotification<>("new-post-notification", "posts", newPostNotificationTemplate, false);
-
                Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
                localPostNotification = new ListNotification<>("local-post-notification", "posts", localPostNotificationTemplate, false);
 
@@ -249,9 +241,6 @@ public class WebInterface implements SessionProvider {
                Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
                mentionNotification = new ListNotification<>("mention-notification", "posts", mentionNotificationTemplate, false);
 
-               Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
-               lockedSonesNotification = new ListNotification<>("sones-locked-notification", "sones", lockedSonesTemplate);
-
                Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
                newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
 
@@ -400,16 +389,6 @@ public class WebInterface implements SessionProvider {
                return formPassword;
        }
 
-       /**
-        * Returns the posts that have been announced as new in the
-        * {@link #newPostNotification}.
-        *
-        * @return The new posts
-        */
-       public Set<Post> getNewPosts() {
-               return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
-       }
-
        @Nonnull
        public Collection<Post> getNewPosts(@Nullable Sone currentSone) {
                Set<Post> allNewPosts = ImmutableSet.<Post> builder()
@@ -688,20 +667,6 @@ public class WebInterface implements SessionProvider {
        //
 
        /**
-        * Notifies the web interface that a new {@link Sone} was found.
-        *
-        * @param newSoneFoundEvent
-        *            The event
-        */
-       @Subscribe
-       public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
-               newSoneNotification.add(newSoneFoundEvent.getSone());
-               if (!hasFirstStartNotification()) {
-                       notificationManager.addNotification(newSoneNotification);
-               }
-       }
-
-       /**
         * Notifies the web interface that a new {@link Post} was found.
         *
         * @param newPostFoundEvent
@@ -713,17 +678,15 @@ public class WebInterface implements SessionProvider {
                boolean isLocal = post.getSone().isLocal();
                if (isLocal) {
                        localPostNotification.add(post);
-               } else {
-                       newPostNotification.add(post);
                }
                if (!hasFirstStartNotification()) {
-                       notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
+                       if (isLocal) {
+                               notificationManager.addNotification(localPostNotification);
+                       }
                        if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
                                mentionNotification.add(post);
                                notificationManager.addNotification(mentionNotification);
                        }
-               } else {
-                       getCore().markPostKnown(post);
                }
        }
 
@@ -753,17 +716,6 @@ public class WebInterface implements SessionProvider {
                }
        }
 
-       /**
-        * Notifies the web interface that a {@link Sone} was marked as known.
-        *
-        * @param markSoneKnownEvent
-        *            The event
-        */
-       @Subscribe
-       public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
-               newSoneNotification.remove(markSoneKnownEvent.getSone());
-       }
-
        @Subscribe
        public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
                removePost(markPostKnownEvent.getPost());
@@ -775,11 +727,6 @@ public class WebInterface implements SessionProvider {
        }
 
        @Subscribe
-       public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
-               newSoneNotification.remove(soneRemovedEvent.getSone());
-       }
-
-       @Subscribe
        public void postRemoved(PostRemovedEvent postRemovedEvent) {
                removePost(postRemovedEvent.getPost());
        }
@@ -806,39 +753,6 @@ public class WebInterface implements SessionProvider {
        }
 
        /**
-        * Notifies the web interface that a Sone was locked.
-        *
-        * @param soneLockedEvent
-        *            The event
-        */
-       @Subscribe
-       public void soneLocked(SoneLockedEvent soneLockedEvent) {
-               final Sone sone = soneLockedEvent.getSone();
-               ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void run() {
-                               lockedSonesNotification.add(sone);
-                               notificationManager.addNotification(lockedSonesNotification);
-                       }
-               }, 5, TimeUnit.MINUTES);
-               lockedSonesTickerObjects.put(sone, tickerObject);
-       }
-
-       /**
-        * Notifies the web interface that a Sone was unlocked.
-        *
-        * @param soneUnlockedEvent
-        *            The event
-        */
-       @Subscribe
-       public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
-               lockedSonesNotification.remove(soneUnlockedEvent.getSone());
-               lockedSonesTickerObjects.remove(soneUnlockedEvent.getSone()).cancel(false);
-       }
-
-       /**
         * Notifies the web interface that a {@link Sone} is being inserted.
         *
         * @param soneInsertingEvent