From d615555392317949e47f6367209ee7194f650768 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 Apr 2011 10:05:15 +0200 Subject: [PATCH] Change the way notifications are handled. The notification manager only keeps a list of current notifications. Removed and changed notifications are now detected by the web interface itself. Also, the notifications are filtered to show only posts and replies that the logged in Sone is interested in. --- pom.xml | 2 +- .../sone/template/NotificationManagerAccessor.java | 8 ++--- .../sone/web/ajax/GetStatusAjaxPage.java | 35 +++++++++++++++++----- src/main/resources/static/javascript/sone.js | 21 ++++++++++--- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 690bb17..21f47ee 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ net.pterodactylus utils - 0.9.2 + 0.9.3-SNAPSHOT junit diff --git a/src/main/java/net/pterodactylus/sone/template/NotificationManagerAccessor.java b/src/main/java/net/pterodactylus/sone/template/NotificationManagerAccessor.java index a8f34a8..c4468ea 100644 --- a/src/main/java/net/pterodactylus/sone/template/NotificationManagerAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/NotificationManagerAccessor.java @@ -21,6 +21,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.NotificationManager; import net.pterodactylus.util.template.ReflectionAccessor; @@ -47,13 +49,9 @@ public class NotificationManagerAccessor extends ReflectionAccessor { public Object get(TemplateContext templateContext, Object object, String member) { NotificationManager notificationManager = (NotificationManager) object; if ("all".equals(member)) { - List notifications = new ArrayList(notificationManager.getNotifications()); + List notifications = ListNotificationFilters.filterNotifications(new ArrayList(notificationManager.getNotifications()), (Sone) templateContext.get("currentSone")); Collections.sort(notifications, Notification.CREATED_TIME_SORTER); return notifications; - } else if ("new".equals(member)) { - List notifications = new ArrayList(notificationManager.getChangedNotifications()); - Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER); - return notifications; } return super.get(templateContext, object, member); } 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 fd9ae76..b109169 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -22,6 +22,7 @@ import java.io.StringWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashSet; @@ -31,8 +32,12 @@ import java.util.Set; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.notify.ListNotification; +import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.util.filter.Filter; +import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.json.JsonArray; import net.pterodactylus.util.json.JsonObject; import net.pterodactylus.util.notify.Notification; @@ -65,6 +70,7 @@ public class GetStatusAjaxPage extends JsonPage { */ @Override protected JsonObject createJsonObject(Request request) { + final Sone currentSone = getCurrentSone(request.getToadletContext(), false); /* load Sones. */ boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true")); Set sones = new HashSet(Collections.singleton(getCurrentSone(request.getToadletContext(), false))); @@ -80,19 +86,24 @@ public class GetStatusAjaxPage extends JsonPage { jsonSones.add(jsonSone); } /* load notifications. */ - List notifications = new ArrayList(webInterface.getNotifications().getChangedNotifications()); - Set removedNotifications = webInterface.getNotifications().getRemovedNotifications(); + List notifications = ListNotificationFilters.filterNotifications(new ArrayList(webInterface.getNotifications().getNotifications()), currentSone); Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER); JsonArray jsonNotifications = new JsonArray(); for (Notification notification : notifications) { jsonNotifications.add(createJsonNotification(notification)); } - JsonArray jsonRemovedNotifications = new JsonArray(); - for (Notification notification : removedNotifications) { - jsonRemovedNotifications.add(createJsonNotification(notification)); - } /* load new posts. */ Set newPosts = webInterface.getNewPosts(); + if (currentSone != null) { + newPosts = Filters.filteredSet(newPosts, new Filter() { + + @Override + public boolean filterObject(Post post) { + return currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone()); + } + + }); + } JsonArray jsonPosts = new JsonArray(); for (Post post : newPosts) { JsonObject jsonPost = new JsonObject(); @@ -104,6 +115,16 @@ public class GetStatusAjaxPage extends JsonPage { } /* load new replies. */ Set newReplies = webInterface.getNewReplies(); + if (currentSone != null) { + newReplies = Filters.filteredSet(newReplies, new Filter() { + + @Override + public boolean filterObject(Reply reply) { + return currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone()); + } + + }); + } JsonArray jsonReplies = new JsonArray(); for (Reply reply : newReplies) { JsonObject jsonReply = new JsonObject(); @@ -113,7 +134,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("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies); + return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies); } /** diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index aeebfb1..a3761b8 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -845,6 +845,22 @@ function getStatus() { $.each(data.sones, function(index, value) { updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdatedUnknown ? null : value.lastUpdated); }); + /* search for removed notifications. */ + $("#sone #notification-area .notification").each(function() { + notificationId = $(this).attr("id"); + foundNotification = false; + $.each(data.notifications, function(index, value) { + if (value.id == notificationId) { + foundNotification = true; + return false; + } + }); + if (!foundNotification) { + $(this).slideUp("normal", function() { + $(this).remove(); + }); + } + }); /* process notifications. */ $.each(data.notifications, function(index, value) { oldNotification = $("#sone #notification-area .notification#" + value.id); @@ -859,11 +875,8 @@ function getStatus() { } else { $("#sone #notification-area").append(notification); notification.slideDown(); + setActivity(); } - setActivity(); - }); - $.each(data.removedNotifications, function(index, value) { - $("#sone #notification-area .notification#" + value.id).slideUp(); }); /* process new posts. */ $.each(data.newPosts, function(index, value) { -- 2.7.4