Add accessor for profile fields.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
index a5376af..e18521c 100644 (file)
@@ -23,8 +23,11 @@ import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.logging.Level;
@@ -33,6 +36,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.CoreListener;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.L10nFilter;
@@ -45,6 +49,7 @@ import net.pterodactylus.sone.template.GetPagePlugin;
 import net.pterodactylus.sone.template.IdentityAccessor;
 import net.pterodactylus.sone.template.NotificationManagerAccessor;
 import net.pterodactylus.sone.template.PostAccessor;
+import net.pterodactylus.sone.template.ProfileAccessor;
 import net.pterodactylus.sone.template.ReplyAccessor;
 import net.pterodactylus.sone.template.RequestChangeFilter;
 import net.pterodactylus.sone.template.SoneAccessor;
@@ -85,6 +90,7 @@ import net.pterodactylus.util.template.TemplateFactory;
 import net.pterodactylus.util.template.TemplateProvider;
 import net.pterodactylus.util.template.XmlFilter;
 import net.pterodactylus.util.thread.Ticker;
+import net.pterodactylus.util.version.Version;
 import freenet.clients.http.SessionManager;
 import freenet.clients.http.SessionManager.Session;
 import freenet.clients.http.ToadletContainer;
@@ -132,6 +138,15 @@ public class WebInterface implements CoreListener {
        /** The “Sone rescued” notification. */
        private final ListNotification<Sone> sonesRescuedNotification;
 
+       /** Sone locked notification ticker objects. */
+       private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
+
+       /** The “Sone locked” notification. */
+       private final ListNotification<Sone> lockedSonesNotification;
+
+       /** The “new version” notification. */
+       private final TemplateNotification newVersionNotification;
+
        /**
         * Creates a new web interface.
         *
@@ -146,6 +161,7 @@ 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(Profile.class, new ProfileAccessor());
                templateFactory.addAccessor(Post.class, new PostAccessor(getCore(), templateFactory));
                templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore(), templateFactory));
                templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
@@ -177,6 +193,12 @@ public class WebInterface implements CoreListener {
 
                Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
                sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
+
+               Template lockedSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/lockedSonesNotification.html"));
+               lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
+
+               Template newVersionTemplate = templateFactory.createTemplate(createReader("/templates/notify/newVersionNotification.html"));
+               newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
        }
 
        //
@@ -234,7 +256,22 @@ public class WebInterface implements CoreListener {
         *         currently logged in
         */
        public Sone getCurrentSone(ToadletContext toadletContext) {
-               return getCurrentSone(getCurrentSession(toadletContext));
+               return getCurrentSone(toadletContext, true);
+       }
+
+       /**
+        * Returns the currently logged in Sone.
+        *
+        * @param toadletContext
+        *            The toadlet context
+        * @param create
+        *            {@code true} to create a new session if no session exists,
+        *            {@code false} to not create a new session
+        * @return The currently logged in Sone, or {@code null} if no Sone is
+        *         currently logged in
+        */
+       public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
+               return getCurrentSone(getCurrentSession(toadletContext, create));
        }
 
        /**
@@ -635,7 +672,7 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void postRemoved(Post post) {
-               /* TODO */
+               newPostNotification.remove(post);
        }
 
        /**
@@ -643,15 +680,25 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void replyRemoved(Reply reply) {
-               /* TODO */
+               newReplyNotification.remove(reply);
        }
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public void soneLocked(Sone sone) {
-               /* TODO */
+       public void soneLocked(final Sone sone) {
+               Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               lockedSonesNotification.add(sone);
+                               lockedSonesTickerObjects.remove(sone);
+                               notificationManager.addNotification(lockedSonesNotification);
+                       }
+               }, "Sone Locked Notification");
+               lockedSonesTickerObjects.put(sone, tickerObject);
        }
 
        /**
@@ -659,7 +706,18 @@ public class WebInterface implements CoreListener {
         */
        @Override
        public void soneUnlocked(Sone sone) {
-               /* TODO */
+               lockedSonesNotification.remove(sone);
+               Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public void updateFound(Version version, long releaseTime) {
+               newVersionNotification.set("version", version);
+               newVersionNotification.set("releaseTime", releaseTime);
+               notificationManager.addNotification(newVersionNotification);
        }
 
        /**