Convert “Sone locked” into EventBus-based event.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
index 045cc11..9e5e0eb 100644 (file)
@@ -37,6 +37,16 @@ import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.CoreListener;
+import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
+import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
+import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
+import net.pterodactylus.sone.core.event.NewPostFoundEvent;
+import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
+import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
+import net.pterodactylus.sone.core.event.PostRemovedEvent;
+import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
+import net.pterodactylus.sone.core.event.SoneLockedEvent;
+import net.pterodactylus.sone.core.event.SoneRemovedEvent;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
@@ -103,8 +113,6 @@ import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.sone.web.page.PageToadlet;
 import net.pterodactylus.sone.web.page.PageToadletFactory;
-import net.pterodactylus.util.collection.SetBuilder;
-import net.pterodactylus.util.collection.filter.Filters;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.notify.Notification;
 import net.pterodactylus.util.notify.NotificationManager;
@@ -131,6 +139,12 @@ import net.pterodactylus.util.version.Version;
 import net.pterodactylus.util.web.RedirectPage;
 import net.pterodactylus.util.web.StaticPage;
 import net.pterodactylus.util.web.TemplatePage;
+
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.eventbus.Subscribe;
+import com.google.inject.Inject;
+
 import freenet.clients.http.SessionManager;
 import freenet.clients.http.SessionManager.Session;
 import freenet.clients.http.ToadletContainer;
@@ -215,6 +229,7 @@ public class WebInterface implements CoreListener {
         * @param sonePlugin
         *            The Sone plugin
         */
+       @Inject
        public WebInterface(SonePlugin sonePlugin) {
                this.sonePlugin = sonePlugin;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
@@ -458,7 +473,7 @@ public class WebInterface implements CoreListener {
         * @return The new posts
         */
        public Set<Post> getNewPosts() {
-               return new SetBuilder<Post>().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get();
+               return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
        }
 
        /**
@@ -468,7 +483,7 @@ public class WebInterface implements CoreListener {
         * @return The new replies
         */
        public Set<PostReply> getNewReplies() {
-               return new SetBuilder<PostReply>().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get();
+               return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
        }
 
        /**
@@ -725,15 +740,15 @@ public class WebInterface implements CoreListener {
        }
 
        /**
-        * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are
-        * referenced by {@link SonePart}s in the given text (after parsing it using
+        * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
+        * {@link SonePart}s in the given text (after parsing it using
         * {@link SoneTextParser}).
         *
         * @param text
         *            The text to parse
         * @return All mentioned local Sones
         */
-       private Set<Sone> getMentionedSones(String text) {
+       private Collection<Sone> getMentionedSones(String text) {
                /* we need no context to find mentioned Sones. */
                Set<Sone> mentionedSones = new HashSet<Sone>();
                try {
@@ -745,7 +760,7 @@ public class WebInterface implements CoreListener {
                } catch (IOException ioe1) {
                        logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
                }
-               return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
+               return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
        }
 
        /**
@@ -770,25 +785,32 @@ public class WebInterface implements CoreListener {
        }
 
        //
-       // CORELISTENER METHODS
+       // EVENT HANDLERS
        //
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a new {@link Sone} was found.
+        *
+        * @param newSoneFoundEvent
+        *            The event
         */
-       @Override
-       public void newSoneFound(Sone sone) {
-               newSoneNotification.add(sone);
+       @Subscribe
+       public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
+               newSoneNotification.add(newSoneFoundEvent.sone());
                if (!hasFirstStartNotification()) {
                        notificationManager.addNotification(newSoneNotification);
                }
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a new {@link Post} was found.
+        *
+        * @param newPostFoundEvent
+        *            The event
         */
-       @Override
-       public void newPostFound(Post post) {
+       @Subscribe
+       public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
+               Post post = newPostFoundEvent.post();
                boolean isLocal = post.getSone().isLocal();
                if (isLocal) {
                        localPostNotification.add(post);
@@ -807,10 +829,14 @@ public class WebInterface implements CoreListener {
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a new {@link PostReply} was found.
+        *
+        * @param newPostReplyFoundEvent
+        *            The event
         */
-       @Override
-       public void newReplyFound(PostReply reply) {
+       @Subscribe
+       public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
+               PostReply reply = newPostReplyFoundEvent.postReply();
                boolean isLocal = reply.getSone().isLocal();
                if (isLocal) {
                        localReplyNotification.add(reply);
@@ -829,56 +855,75 @@ public class WebInterface implements CoreListener {
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link Sone} was marked as known.
+        *
+        * @param markSoneKnownEvent
+        *            The event
         */
-       @Override
-       public void markSoneKnown(Sone sone) {
-               newSoneNotification.remove(sone);
+       @Subscribe
+       public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
+               newSoneNotification.remove(markSoneKnownEvent.sone());
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link Post} was marked as known.
+        *
+        * @param markPostKnownEvent
+        *            The event
         */
-       @Override
-       public void markPostKnown(Post post) {
-               newPostNotification.remove(post);
-               localPostNotification.remove(post);
-               mentionNotification.remove(post);
+       @Subscribe
+       public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
+               newPostNotification.remove(markPostKnownEvent.post());
+               localPostNotification.remove(markPostKnownEvent.post());
+               mentionNotification.remove(markPostKnownEvent.post());
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link PostReply} was marked as known.
+        *
+        * @param markPostReplyKnownEvent
+        *            The event
         */
-       @Override
-       public void markReplyKnown(PostReply reply) {
-               newReplyNotification.remove(reply);
-               localReplyNotification.remove(reply);
-               mentionNotification.remove(reply.getPost());
+       @Subscribe
+       public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
+               newReplyNotification.remove(markPostReplyKnownEvent.postReply());
+               localReplyNotification.remove(markPostReplyKnownEvent.postReply());
+               mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link Sone} was removed.
+        *
+        * @param soneRemovedEvent
+        *            The event
         */
-       @Override
-       public void soneRemoved(Sone sone) {
-               newSoneNotification.remove(sone);
+       @Subscribe
+       public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
+               newSoneNotification.remove(soneRemovedEvent.sone());
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link Post} was removed.
+        *
+        * @param postRemovedEvent
+        *            The event
         */
-       @Override
-       public void postRemoved(Post post) {
-               newPostNotification.remove(post);
-               localPostNotification.remove(post);
-               mentionNotification.remove(post);
+       @Subscribe
+       public void postRemoved(PostRemovedEvent postRemovedEvent) {
+               newPostNotification.remove(postRemovedEvent.post());
+               localPostNotification.remove(postRemovedEvent.post());
+               mentionNotification.remove(postRemovedEvent.post());
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a {@link PostReply} was removed.
+        *
+        * @param postReplyRemovedEvent
+        *            The event
         */
-       @Override
-       public void replyRemoved(PostReply reply) {
+       @Subscribe
+       public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
+               PostReply reply = postReplyRemovedEvent.postReply();
                newReplyNotification.remove(reply);
                localReplyNotification.remove(reply);
                if (!getMentionedSones(reply.getText()).isEmpty()) {
@@ -893,10 +938,14 @@ public class WebInterface implements CoreListener {
        }
 
        /**
-        * {@inheritDoc}
+        * Notifies the web interface that a Sone was locked.
+        *
+        * @param soneLockedEvent
+        *            The event
         */
-       @Override
-       public void soneLocked(final Sone sone) {
+       @Subscribe
+       public void soneLocked(SoneLockedEvent soneLockedEvent) {
+               final Sone sone = soneLockedEvent.sone();
                Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
 
                        @Override
@@ -910,6 +959,10 @@ public class WebInterface implements CoreListener {
                lockedSonesTickerObjects.put(sone, tickerObject);
        }
 
+       //
+       // CORELISTENER METHODS
+       //
+
        /**
         * {@inheritDoc}
         */