From 85fd1173da553ee8c1afbe0c4638cc9bbce8d44f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Apr 2011 20:01:16 +0200 Subject: [PATCH] Only render notifications if they have changed. --- .../sone/web/ajax/GetStatusAjaxPage.java | 37 ++++---------- src/main/resources/static/javascript/sone.js | 59 +++++++++++++++------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java index 426c08f..09a9d2b 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -17,8 +17,6 @@ package net.pterodactylus.sone.web.ajax; -import java.io.IOException; -import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -39,8 +37,6 @@ import net.pterodactylus.util.filter.Filters; 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.template.TemplateContext; /** * The “get status” AJAX handler returns all information that is necessary to @@ -86,9 +82,9 @@ public class GetStatusAjaxPage extends JsonPage { /* load notifications. */ List notifications = ListNotificationFilters.filterNotifications(new ArrayList(webInterface.getNotifications().getNotifications()), currentSone); Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER); - JsonArray jsonNotifications = new JsonArray(); + JsonArray jsonNotificationInformations = new JsonArray(); for (Notification notification : notifications) { - jsonNotifications.add(createJsonNotification(notification)); + jsonNotificationInformations.add(createJsonNotificationInformation(notification)); } /* load new posts. */ Set newPosts = webInterface.getNewPosts(); @@ -132,7 +128,7 @@ public class GetStatusAjaxPage extends JsonPage { jsonReply.put("postSone", reply.getPost().getSone().getId()); jsonReplies.add(jsonReply); } - return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies); + return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies); } /** @@ -179,31 +175,20 @@ public class GetStatusAjaxPage extends JsonPage { } /** - * Creates a JSON object from the given notification. + * Creates a JSON object that only contains the ID and the last-updated time + * of the given notification. * + * @see Notification#getId() + * @see Notification#getLastUpdatedTime() * @param notification - * The notification to create a JSON object - * @return The JSON object + * The notification + * @return A JSON object containing the notification ID and last-updated + * time */ - private JsonObject createJsonNotification(Notification notification) { + private JsonObject createJsonNotificationInformation(Notification notification) { JsonObject jsonNotification = new JsonObject(); 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("notification", notification); - ((TemplateNotification) notification).render(templateContext, notificationWriter); - } else { - notification.render(notificationWriter); - } - } catch (IOException ioe1) { - /* StringWriter never throws, ignore. */ - } - jsonNotification.put("text", notificationWriter.toString()); - jsonNotification.put("createdTime", notification.getCreatedTime()); jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime()); - jsonNotification.put("dismissable", notification.isDismissable()); return jsonNotification; } diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 5232431..5aaa6a5 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -1021,25 +1021,16 @@ function getStatus() { } }); /* process notifications. */ + notificationIds = []; $.each(data.notifications, function(index, value) { oldNotification = getNotification(value.id); - notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide(); - if (oldNotification.length != 0) { - if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) { - opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden"); - notification.find(".short-text").toggleClass("hidden", opened); - notification.find(".text").toggleClass("hidden", !opened); - } - checkForRemovedSones(oldNotification, notification); - checkForRemovedPosts(oldNotification, notification); - checkForRemovedReplies(oldNotification, notification); - oldNotification.replaceWith(notification.show()); - } else { - $("#sone #notification-area").append(notification); - notification.slideDown(); - setActivity(); + if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) { + notificationIds.push(value.id); } }); + if (notificationIds.length > 0) { + loadNotifications(notificationIds); + } /* process new posts. */ $.each(data.newPosts, function(index, value) { loadNewPost(value.id, value.sone, value.recipient, value.time); @@ -1061,6 +1052,40 @@ function getStatus() { } /** + * Requests multiple notifications from Sone and displays them. + * + * @param notificationIds + * Array of IDs of the notifications to load + */ +function loadNotifications(notificationIds) { + $.getJSON("getNotification.ajax", {"notifications": notificationIds.join(",")}, function(data, textStatus) { + if (!data || !data.success) { + // TODO - show error + return; + } + $.each(data.notifications, function(index, value) { + oldNotification = getNotification(value.id); + notification = ajaxifyNotification(createNotification(value.id, value.lastUpdatedTime, value.text, value.dismissable)).hide(); + if (oldNotification.length != 0) { + if ((oldNotification.find(".short-text").length > 0) && (notification.find(".short-text").length > 0)) { + opened = oldNotification.is(":visible") && oldNotification.find(".short-text").hasClass("hidden"); + notification.find(".short-text").toggleClass("hidden", opened); + notification.find(".text").toggleClass("hidden", !opened); + } + checkForRemovedSones(oldNotification, notification); + checkForRemovedPosts(oldNotification, notification); + checkForRemovedReplies(oldNotification, notification); + oldNotification.replaceWith(notification.show()); + } else { + $("#sone #notification-area").append(notification); + notification.slideDown(); + setActivity(); + } + }) + }); +} + +/** * Returns the ID of the currently logged in Sone. * * @return The ID of the current Sone, or an empty string if no Sone is logged @@ -1451,8 +1476,8 @@ function changeIcon(iconUrl) { * true if the notification can be dismissed by the * user */ -function createNotification(id, text, dismissable) { - notification = $("
").addClass("notification").attr("id", id); +function createNotification(id, lastUpdatedTime, text, dismissable) { + notification = $("
").addClass("notification").attr("id", id).attr("lastUpdatedTime", lastUpdatedTime); if (dismissable) { dismissForm = $("#sone #notification-area #notification-dismiss-template").clone().removeClass("hidden").removeAttr("id") dismissForm.find("input[name=notification]").val(id); -- 2.7.4