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;
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;
+
/**
* AJAX handler to return all current notifications.
*
* {@inheritDoc}
*/
@Override
- protected JsonObject createJsonObject(FreenetRequest request) {
+ protected JsonReturnObject createJsonObject(FreenetRequest request) {
Sone currentSone = getCurrentSone(request.getToadletContext(), false);
Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
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));
}
//
* 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 {
* The current Sone (may be {@code null})
* @return The current options
*/
- private static JsonObject createJsonOptions(Sone currentSone) {
- JsonObject options = new JsonObject();
+ 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());
+ options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
+ options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
+ options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());
}
return options;
}