X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetNotificationsAjaxPage.java;h=34eabc1fb522a626917791e4e052f697c3caa927;hb=541f49177de5f63a97d94fe74ffb60badbf5d4c9;hp=de2f8f89612f02d56a1e9b19a6a6c1190612f472;hpb=1c8d22496d1b1c26502d0adf2daab1605fa6e7fc;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
index de2f8f8..34eabc1 100644
--- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
+++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java
@@ -1,5 +1,5 @@
/*
- * Sone - GetNotificationsAjaxPage.java - Copyright © 2010 David Roden
+ * Sone - GetNotificationsAjaxPage.java - Copyright © 2011â2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -17,62 +17,132 @@
package net.pterodactylus.sone.web.ajax;
-import java.util.ArrayList;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.sone.notify.ListNotificationFilters;
import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.util.json.JsonArray;
-import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.sone.web.page.FreenetRequest;
import net.pterodactylus.util.notify.Notification;
+import net.pterodactylus.util.notify.TemplateNotification;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
/**
- * Returns all changed notifications.
+ * AJAX handler to return all current notifications.
*
* @author David âBombeâ Roden
*/
public class GetNotificationsAjaxPage extends JsonPage {
/**
- * TODO
+ * Creates a new âget notificationsâ AJAX handler.
*
* @param webInterface
+ * The Sone web interface
*/
public GetNotificationsAjaxPage(WebInterface webInterface) {
- super("ajax/getNotifications.ajax", webInterface);
+ super("getNotifications.ajax", webInterface);
}
//
// JSONPAGE METHODS
//
+ @Override
+ protected boolean needsFormPassword() {
+ return false;
+ }
+
+ @Override
+ protected boolean requiresLogin() {
+ return false;
+ }
+
+ @Override
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
+ Sone currentSone = getCurrentSone(request.getToadletContext(), false);
+ Collection notifications = webInterface.getNotifications().getNotifications();
+ List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
+ Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER);
+ ArrayNode jsonNotifications = new ArrayNode(instance);
+ for (Notification notification : filteredNotifications) {
+ jsonNotifications.add(createJsonNotification(request, notification));
+ }
+ return createSuccessJsonObject().put("notificationHash", filteredNotifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
/**
- * {@inheritDoc}
+ * Creates a JSON object from the given notification.
+ *
+ * @param request
+ * The request to load the session from
+ * @param notification
+ * The notification to create a JSON object
+ * @return The JSON object
*/
- @Override
- protected JsonObject createJsonObject(Request request) {
- List notifications = new ArrayList(webInterface.getNotifications().getChangedNotifications());
- Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
- JsonObject result = createSuccessJsonObject();
- JsonArray jsonNotifications = new JsonArray();
- for (Notification notification : notifications) {
- JsonObject jsonNotification = new JsonObject();
- jsonNotification.put("id", notification.getId());
- jsonNotification.put("text", notification.toString());
- jsonNotification.put("createdTime", notification.getCreatedTime());
- jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
- jsonNotification.put("dismissable", notification.isDismissable());
- jsonNotifications.add(jsonNotification);
+ private JsonNode createJsonNotification(FreenetRequest request, Notification notification) {
+ ObjectNode jsonNotification = new ObjectNode(instance);
+ jsonNotification.put("id", notification.getId());
+ StringWriter notificationWriter = new StringWriter();
+ try {
+ if (notification instanceof TemplateNotification) {
+ TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
+ templateContext.set("core", webInterface.getCore());
+ templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
+ templateContext.set("localSones", webInterface.getCore().getLocalSones());
+ templateContext.set("request", request);
+ templateContext.set("currentVersion", SonePlugin.VERSION);
+ templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
+ templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
+ templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
+ templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
+ templateContext.set("notification", notification);
+ ((TemplateNotification) notification).render(templateContext, notificationWriter);
+ } else {
+ notification.render(notificationWriter);
+ }
+ } catch (IOException ioe1) {
+ /* StringWriter never throws, ignore. */
}
- return result.put("notifications", jsonNotifications);
+ jsonNotification.put("text", notificationWriter.toString());
+ jsonNotification.put("createdTime", notification.getCreatedTime());
+ jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
+ jsonNotification.put("dismissable", notification.isDismissable());
+ return jsonNotification;
}
/**
- * {@inheritDoc}
+ * Creates a JSON object that contains all options that are currently in
+ * effect for the given Sone (or overall, if the given Sone is {@code null}
+ * ).
+ *
+ * @param currentSone
+ * The current Sone (may be {@code null})
+ * @return The current options
*/
- @Override
- protected boolean needsFormPassword() {
- return false;
+ private static JsonNode createJsonOptions(Sone currentSone) {
+ ObjectNode options = new ObjectNode(instance);
+ if (currentSone != null) {
+ options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
+ options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
+ options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
+ }
+ return options;
}
}