Merge branch 'master' into next
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
index a74c125..ee8b9bf 100644 (file)
@@ -21,11 +21,12 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
+import java.util.UUID;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -37,9 +38,7 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.sone.notify.NewPostNotification;
-import net.pterodactylus.sone.notify.NewReplyNotification;
-import net.pterodactylus.sone.notify.NewSoneNotification;
+import net.pterodactylus.sone.notify.ListNotification;
 import net.pterodactylus.sone.template.CollectionAccessor;
 import net.pterodactylus.sone.template.CssClassNameFilter;
 import net.pterodactylus.sone.template.GetPagePlugin;
@@ -50,19 +49,24 @@ import net.pterodactylus.sone.template.ReplyAccessor;
 import net.pterodactylus.sone.template.RequestChangeFilter;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.sone.template.SubstringFilter;
+import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
-import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
+import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
-import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
+import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
+import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
+import net.pterodactylus.sone.web.ajax.MarkPostAsKnownPage;
+import net.pterodactylus.sone.web.ajax.MarkReplyAsKnownPage;
 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
+import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
 import net.pterodactylus.sone.web.page.PageToadlet;
 import net.pterodactylus.sone.web.page.PageToadletFactory;
 import net.pterodactylus.sone.web.page.StaticPage;
@@ -81,7 +85,9 @@ import net.pterodactylus.util.template.TemplateProvider;
 import net.pterodactylus.util.template.XmlFilter;
 import net.pterodactylus.util.thread.Ticker;
 import freenet.clients.http.SessionManager;
+import freenet.clients.http.SessionManager.Session;
 import freenet.clients.http.ToadletContainer;
+import freenet.clients.http.ToadletContext;
 import freenet.l10n.BaseL10n;
 
 /**
@@ -111,13 +117,19 @@ public class WebInterface implements CoreListener {
        private DefaultTemplateFactory templateFactory;
 
        /** The “new Sone” notification. */
-       private final NewSoneNotification newSoneNotification;
+       private final ListNotification<Sone> newSoneNotification;
 
        /** The “new post” notification. */
-       private final NewPostNotification newPostNotification;
+       private final ListNotification<Post> newPostNotification;
 
        /** The “new reply” notification. */
-       private final NewReplyNotification newReplyNotification;
+       private final ListNotification<Reply> newReplyNotification;
+
+       /** The “rescuing Sone” notification. */
+       private final ListNotification<Sone> rescuingSonesNotification;
+
+       /** The “Sone rescued” notification. */
+       private final ListNotification<Sone> sonesRescuedNotification;
 
        /**
         * Creates a new web interface.
@@ -133,8 +145,8 @@ public class WebInterface implements CoreListener {
                templateFactory.addAccessor(Object.class, new ReflectionAccessor());
                templateFactory.addAccessor(Collection.class, new CollectionAccessor());
                templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
-               templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
-               templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
+               templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
+               templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
                templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
                templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
                templateFactory.addFilter("date", new DateFilter());
@@ -151,13 +163,19 @@ public class WebInterface implements CoreListener {
 
                /* create notifications. */
                Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
-               newSoneNotification = new NewSoneNotification(newSoneNotificationTemplate);
+               newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
 
                Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
-               newPostNotification = new NewPostNotification(newPostNotificationTemplate);
+               newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
 
                Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
-               newReplyNotification = new NewReplyNotification(newReplyNotificationTemplate);
+               newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
+
+               Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
+               rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
+
+               Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
+               sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
        }
 
        //
@@ -174,6 +192,87 @@ public class WebInterface implements CoreListener {
        }
 
        /**
+        * Returns the current session, creating a new session if there is no
+        * current session.
+        *
+        * @param toadletContenxt
+        *            The toadlet context
+        * @return The current session, or {@code null} if there is no current
+        *         session
+        */
+       public Session getCurrentSession(ToadletContext toadletContenxt) {
+               return getCurrentSession(toadletContenxt, true);
+       }
+
+       /**
+        * Returns the current session, creating a new session if there is no
+        * current session and {@code create} is {@code true}.
+        *
+        * @param toadletContenxt
+        *            The toadlet context
+        * @param create
+        *            {@code true} to create a new session if there is no current
+        *            session, {@code false} otherwise
+        * @return The current session, or {@code null} if there is no current
+        *         session
+        */
+       public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
+               Session session = getSessionManager().useSession(toadletContenxt);
+               if (create && (session == null)) {
+                       session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
+               }
+               return session;
+       }
+
+       /**
+        * Returns the currently logged in Sone.
+        *
+        * @param toadletContext
+        *            The toadlet context
+        * @return The currently logged in Sone, or {@code null} if no Sone is
+        *         currently logged in
+        */
+       public Sone getCurrentSone(ToadletContext toadletContext) {
+               return getCurrentSone(getCurrentSession(toadletContext));
+       }
+
+       /**
+        * Returns the currently logged in Sone.
+        *
+        * @param session
+        *            The session
+        * @return The currently logged in Sone, or {@code null} if no Sone is
+        *         currently logged in
+        */
+       public Sone getCurrentSone(Session session) {
+               if (session == null) {
+                       return null;
+               }
+               String soneId = (String) session.getAttribute("Sone.CurrentSone");
+               if (soneId == null) {
+                       return null;
+               }
+               return getCore().getLocalSone(soneId, false);
+       }
+
+       /**
+        * Sets the currently logged in Sone.
+        *
+        * @param toadletContext
+        *            The toadlet context
+        * @param sone
+        *            The Sone to set as currently logged in
+        */
+       public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
+               Session session = getCurrentSession(toadletContext);
+               if (sone == null) {
+                       session.removeAttribute("Sone.CurrentSone");
+               } else {
+                       session.setAttribute("Sone.CurrentSone", sone.getId());
+               }
+       }
+
+       /**
         * Returns the notification manager.
         *
         * @return The notification manager
@@ -197,12 +296,7 @@ public class WebInterface implements CoreListener {
         * @return The node’s session manager
         */
        public SessionManager getSessionManager() {
-               try {
-                       return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
-               } catch (URISyntaxException use1) {
-                       logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
-                       return null;
-               }
+               return sonePlugin.pluginRespirator().getSessionManager("Sone");
        }
 
        /**
@@ -214,6 +308,26 @@ public class WebInterface implements CoreListener {
                return formPassword;
        }
 
+       /**
+        * Returns the posts that have been announced as new in the
+        * {@link #newPostNotification}.
+        *
+        * @return The new posts
+        */
+       public Set<Post> getNewPosts() {
+               return new HashSet<Post>(newPostNotification.getElements());
+       }
+
+       /**
+        * Returns the replies that have been announced as new in the
+        * {@link #newReplyNotification}.
+        *
+        * @return The new replies
+        */
+       public Set<Reply> getNewReplies() {
+               return new HashSet<Reply>(newReplyNotification.getElements());
+       }
+
        //
        // ACTIONS
        //
@@ -237,6 +351,23 @@ public class WebInterface implements CoreListener {
                                startupNotification.dismiss();
                        }
                }, "Sone Startup Notification Remover");
+
+               Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
+               final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
+               Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               if (getCore().getIdentityManager().isConnected()) {
+                                       wotMissingNotification.dismiss();
+                               } else {
+                                       notificationManager.addNotification(wotMissingNotification);
+                               }
+                               Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
+                       }
+
+               }, "Sone WoT Connector Checker");
        }
 
        /**
@@ -268,6 +399,8 @@ public class WebInterface implements CoreListener {
                Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
                Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
                Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
+               Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
+               Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
                Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
                Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
                Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
@@ -276,6 +409,7 @@ public class WebInterface implements CoreListener {
                Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
                Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
                Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
+               Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
                Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
 
                PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
@@ -291,6 +425,8 @@ public class WebInterface implements CoreListener {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
@@ -304,13 +440,18 @@ public class WebInterface implements CoreListener {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
-               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
-               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkPostAsKnownPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkReplyAsKnownPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
@@ -364,8 +505,27 @@ public class WebInterface implements CoreListener {
         * {@inheritDoc}
         */
        @Override
+       public void rescuingSone(Sone sone) {
+               rescuingSonesNotification.add(sone);
+               notificationManager.addNotification(rescuingSonesNotification);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void rescuedSone(Sone sone) {
+               rescuingSonesNotification.remove(sone);
+               sonesRescuedNotification.add(sone);
+               notificationManager.addNotification(sonesRescuedNotification);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
        public void newSoneFound(Sone sone) {
-               newSoneNotification.addSone(sone);
+               newSoneNotification.add(sone);
                notificationManager.addNotification(newSoneNotification);
        }
 
@@ -374,7 +534,7 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void newPostFound(Post post) {
-               newPostNotification.addPost(post);
+               newPostNotification.add(post);
                notificationManager.addNotification(newPostNotification);
        }
 
@@ -386,7 +546,7 @@ public class WebInterface implements CoreListener {
                if (reply.getPost().getSone() == null) {
                        return;
                }
-               newReplyNotification.addReply(reply);
+               newReplyNotification.add(reply);
                notificationManager.addNotification(newReplyNotification);
        }
 
@@ -395,10 +555,7 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void markSoneKnown(Sone sone) {
-               newSoneNotification.removeSone(sone);
-               if (newSoneNotification.isEmpty()) {
-                       newSoneNotification.dismiss();
-               }
+               newSoneNotification.remove(sone);
        }
 
        /**
@@ -406,10 +563,7 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void markPostKnown(Post post) {
-               newPostNotification.removePost(post);
-               if (newPostNotification.isEmpty()) {
-                       newPostNotification.dismiss();
-               }
+               newPostNotification.remove(post);
        }
 
        /**
@@ -417,10 +571,23 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void markReplyKnown(Reply reply) {
-               newReplyNotification.removeReply(reply);
-               if (newReplyNotification.isEmpty()) {
-                       newReplyNotification.dismiss();
-               }
+               newReplyNotification.remove(reply);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void postRemoved(Post post) {
+               /* TODO */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void replyRemoved(Reply reply) {
+               /* TODO */
        }
 
        /**