X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetNotificationsAjaxPage.java;h=d86e5a85229b02d38a174025f459042703286717;hb=53a71d1d6b91e4d56af49a06f2e06bc4d11bf3eb;hp=d4be6bef641082a03f776d01a75be037a67cf07b;hpb=99888ce13cc17d49f5e217ab6f2c9ad5ef168792;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 d4be6be..d86e5a8 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web.ajax; +import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; + import java.io.IOException; import java.io.StringWriter; import java.util.Collection; @@ -28,13 +30,15 @@ import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.json.JsonArray; -import net.pterodactylus.util.json.JsonObject; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.TemplateNotification; -import net.pterodactylus.util.object.HashCode; 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; +import com.google.common.base.Optional; + /** * AJAX handler to return all current notifications. * @@ -76,17 +80,16 @@ public class GetNotificationsAjaxPage extends JsonPage { * {@inheritDoc} */ @Override - protected JsonObject createJsonObject(FreenetRequest request) { - Sone currentSone = getCurrentSone(request.getToadletContext(), false); + protected JsonReturnObject createJsonObject(FreenetRequest request) { + Optional currentSone = getCurrentSone(request.getToadletContext(), false); Collection notifications = webInterface.getNotifications().getNotifications(); - List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone); + List filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone.orNull()); Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER); - int notificationHash = HashCode.hashCode(filteredNotifications); - JsonArray jsonNotifications = new JsonArray(); + ArrayNode jsonNotifications = new ArrayNode(instance); for (Notification notification : filteredNotifications) { jsonNotifications.add(createJsonNotification(request, notification)); } - return createSuccessJsonObject().put("notificationHash", notificationHash).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone)); + return createSuccessJsonObject().put("notificationHash", filteredNotifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone)); } // @@ -102,8 +105,8 @@ public class GetNotificationsAjaxPage extends JsonPage { * The notification to create a JSON object * @return The JSON object */ - private JsonObject createJsonNotification(FreenetRequest request, Notification notification) { - JsonObject jsonNotification = new JsonObject(); + private JsonNode createJsonNotification(FreenetRequest request, Notification notification) { + ObjectNode jsonNotification = new ObjectNode(instance); jsonNotification.put("id", notification.getId()); StringWriter notificationWriter = new StringWriter(); try { @@ -142,12 +145,12 @@ public class GetNotificationsAjaxPage extends JsonPage { * The current Sone (may be {@code null}) * @return The current options */ - private static JsonObject createJsonOptions(Sone currentSone) { - JsonObject options = new JsonObject(); - 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()); + private static JsonNode createJsonOptions(Optional currentSone) { + ObjectNode options = new ObjectNode(instance); + if (currentSone.isPresent()) { + options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications()); + options.put("ShowNotification/NewPosts", currentSone.get().getOptions().isShowNewPostNotifications()); + options.put("ShowNotification/NewReplies", currentSone.get().getOptions().isShowNewReplyNotifications()); } return options; }