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;
import java.util.logging.Logger;
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.PageToadletFactory;
import net.pterodactylus.sone.web.page.StaticPage;
import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.notify.Notification;
import net.pterodactylus.util.notify.NotificationManager;
import net.pterodactylus.util.notify.TemplateNotification;
import net.pterodactylus.util.template.DateFilter;
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;
/**
/** 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;
+
/**
* Creates a new web interface.
*
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());
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);
}
//
}
/**
+ * 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(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));
+ }
+
+ /**
+ * 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
return new HashSet<Reply>(newReplyNotification.getElements());
}
+ /**
+ * Sets whether the current start of the plugin is the first start. It is
+ * considered a first start if the configuration file does not exist.
+ *
+ * @param firstStart
+ * {@code true} if no configuration file existed when Sone was
+ * loaded, {@code false} otherwise
+ */
+ public void setFirstStart(boolean firstStart) {
+ if (firstStart) {
+ Template firstStartNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/firstStartNotification.html"));
+ Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
+ notificationManager.addNotification(firstStartNotification);
+ }
+ }
+
+ /**
+ * Sets whether Sone was started with a fresh configuration file.
+ *
+ * @param newConfig
+ * {@code true} if Sone was started with a fresh configuration,
+ * {@code false} if the existing configuration could be read
+ */
+ public void setNewConfig(boolean newConfig) {
+ if (newConfig && !hasFirstStartNotification()) {
+ Template configNotReadNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/configNotReadNotification.html"));
+ Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
+ notificationManager.addNotification(configNotReadNotification);
+ }
+ }
+
+ //
+ // PRIVATE ACCESSORS
+ //
+
+ /**
+ * Returns whether the first start notification is currently displayed.
+ *
+ * @return {@code true} if the first-start notification is currently
+ * displayed, {@code false} otherwise
+ */
+ private boolean hasFirstStartNotification() {
+ return notificationManager.getNotification("first-start-notification") != null;
+ }
+
//
// ACTIONS
//
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)));
@Override
public void newSoneFound(Sone sone) {
newSoneNotification.add(sone);
- notificationManager.addNotification(newSoneNotification);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newSoneNotification);
+ }
}
/**
@Override
public void newPostFound(Post post) {
newPostNotification.add(post);
- notificationManager.addNotification(newPostNotification);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newPostNotification);
+ } else {
+ getCore().markPostKnown(post);
+ }
}
/**
return;
}
newReplyNotification.add(reply);
- notificationManager.addNotification(newReplyNotification);
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newReplyNotification);
+ } else {
+ getCore().markReplyKnown(reply);
+ }
}
/**
}
/**
+ * {@inheritDoc}
+ */
+ @Override
+ public void postRemoved(Post post) {
+ newPostNotification.remove(post);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void replyRemoved(Reply reply) {
+ newReplyNotification.remove(reply);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ 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);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void soneUnlocked(Sone sone) {
+ lockedSonesNotification.remove(sone);
+ Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
+ }
+
+ /**
* Template provider implementation that uses
* {@link WebInterface#createReader(String)} to load templates for
* inclusion.