Change the way notifications are handled.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Apr 2011 08:05:15 +0000 (10:05 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Apr 2011 08:06:14 +0000 (10:06 +0200)
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
src/main/java/net/pterodactylus/sone/template/NotificationManagerAccessor.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java
src/main/resources/static/javascript/sone.js

diff --git a/pom.xml b/pom.xml
index 690bb17..21f47ee 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
                <dependency>
                        <groupId>net.pterodactylus</groupId>
                        <artifactId>utils</artifactId>
-                       <version>0.9.2</version>
+                       <version>0.9.3-SNAPSHOT</version>
                </dependency>
                <dependency>
                        <groupId>junit</groupId>
index a8f34a8..c4468ea 100644 (file)
@@ -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<Notification> notifications = new ArrayList<Notification>(notificationManager.getNotifications());
+                       List<Notification> notifications = ListNotificationFilters.filterNotifications(new ArrayList<Notification>(notificationManager.getNotifications()), (Sone) templateContext.get("currentSone"));
                        Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
                        return notifications;
-               } else if ("new".equals(member)) {
-                       List<Notification> notifications = new ArrayList<Notification>(notificationManager.getChangedNotifications());
-                       Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
-                       return notifications;
                }
                return super.get(templateContext, object, member);
        }
index fd9ae76..b109169 100644 (file)
@@ -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<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
@@ -80,19 +86,24 @@ public class GetStatusAjaxPage extends JsonPage {
                        jsonSones.add(jsonSone);
                }
                /* load notifications. */
-               List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
-               Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
+               List<Notification> notifications = ListNotificationFilters.filterNotifications(new ArrayList<Notification>(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<Post> newPosts = webInterface.getNewPosts();
+               if (currentSone != null) {
+                       newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
+
+                               @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<Reply> newReplies = webInterface.getNewReplies();
+               if (currentSone != null) {
+                       newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
+
+                               @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);
        }
 
        /**
index aeebfb1..a3761b8 100644 (file)
@@ -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) {