Merge branch 'mark-elements-as-known' into next
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 09:27:58 +0000 (11:27 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 09:27:58 +0000 (11:27 +0200)
This fixes #119.

src/main/java/net/pterodactylus/sone/web/IndexPage.java
src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java
src/main/java/net/pterodactylus/sone/web/SearchPage.java
src/main/java/net/pterodactylus/sone/web/ViewPostPage.java
src/main/java/net/pterodactylus/sone/web/ViewSonePage.java
src/main/resources/static/css/sone.css
src/main/resources/static/javascript/sone.js
src/main/resources/templates/notify/newPostNotification.html
src/main/resources/templates/notify/newReplyNotification.html
src/main/resources/templates/notify/newSoneNotification.html

index 2d85e02..db48314 100644 (file)
@@ -77,21 +77,15 @@ public class IndexPage extends SoneTemplatePage {
                Pagination<Post> pagination = new Pagination<Post>(allPosts, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
                templateContext.set("pagination", pagination);
                templateContext.set("posts", pagination.getItems());
-       }
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void postProcess(Request request, TemplateContext templateContext) {
-               @SuppressWarnings("unchecked")
-               List<Post> posts = (List<Post>) templateContext.get("posts");
-               for (Post post : posts) {
+               /* mark it all as known. */
+               for (Post post : pagination.getItems()) {
                        webInterface.getCore().markPostKnown(post);
                        for (Reply reply : webInterface.getCore().getReplies(post)) {
                                webInterface.getCore().markReplyKnown(reply);
                        }
                }
+
        }
 
 }
index c0e6364..6b1b4e3 100644 (file)
@@ -62,17 +62,9 @@ public class KnownSonesPage extends SoneTemplatePage {
                Pagination<Sone> sonePagination = new Pagination<Sone>(knownSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
                templateContext.set("pagination", sonePagination);
                templateContext.set("knownSones", sonePagination.getItems());
-       }
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void postProcess(Request request, TemplateContext templateContext) {
-               super.postProcess(request, templateContext);
-               @SuppressWarnings("unchecked")
-               List<Sone> sones = (List<Sone>) templateContext.get("knownSones");
-               for (Sone sone : sones) {
+               /* mark Sones as known. */
+               for (Sone sone : sonePagination.getItems()) {
                        webInterface.getCore().markSoneKnown(sone);
                }
        }
index 2b854f3..73673ab 100644 (file)
@@ -110,6 +110,17 @@ public class SearchPage extends SoneTemplatePage {
                templateContext.set("soneHits", sonePagination.getItems());
                templateContext.set("postPagination", postPagination);
                templateContext.set("postHits", postPagination.getItems());
+
+               /* mark found posts and Sones as knew. */
+               for (Sone sone : sonePagination.getItems()) {
+                       webInterface.getCore().markSoneKnown(sone);
+               }
+               for (Post post : postPagination.getItems()) {
+                       webInterface.getCore().markPostKnown(post);
+                       for (Reply reply : webInterface.getCore().getReplies(post)) {
+                               webInterface.getCore().markReplyKnown(reply);
+                       }
+               }
        }
 
        //
index e528709..fe63b34 100644 (file)
@@ -73,17 +73,6 @@ public class ViewPostPage extends SoneTemplatePage {
                Post post = webInterface.getCore().getPost(postId);
                templateContext.set("post", post);
                templateContext.set("raw", raw);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected void postProcess(Request request, TemplateContext templateContext) {
-               Post post = (Post) templateContext.get("post");
-               if (post == null) {
-                       return;
-               }
                webInterface.getCore().markPostKnown(post);
                for (Reply reply : webInterface.getCore().getReplies(post)) {
                        webInterface.getCore().markReplyKnown(reply);
index c7edc32..82284d5 100644 (file)
@@ -29,6 +29,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.template.SoneAccessor;
+import net.pterodactylus.util.collection.ListBuilder;
 import net.pterodactylus.util.collection.Pagination;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.template.Template;
@@ -108,22 +109,10 @@ public class ViewSonePage extends SoneTemplatePage {
                Pagination<Post> repliedPostPagination = new Pagination<Post>(posts, 10).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0));
                templateContext.set("repliedPostPagination", repliedPostPagination);
                templateContext.set("repliedPosts", repliedPostPagination.getItems());
-       }
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       @SuppressWarnings("unchecked")
-       protected void postProcess(Request request, TemplateContext templateContext) {
-               Sone sone = (Sone) templateContext.get("sone");
-               if (sone == null) {
-                       return;
-               }
+               /* mark Sone and posts and replies as known. */
                webInterface.getCore().markSoneKnown(sone);
-               List<Post> posts = (List<Post>) templateContext.get("posts");
-               posts.addAll((List<Post>) templateContext.get("repliedPosts"));
-               for (Post post : posts) {
+               for (Post post : new ListBuilder<Post>().addAll(postPagination.getItems()).addAll(repliedPostPagination.getItems()).get()) {
                        if (post.getSone() != null) {
                                webInterface.getCore().markPostKnown(post);
                        }
index 38fcd19..f6bb62d 100644 (file)
@@ -121,6 +121,10 @@ textarea {
        float: right;
 }
 
+#sone #notification-area .notification .hidden {
+       display: none;
+}
+
 #sone #plugin-warning {
        border: solid 0.5em red;
        padding: 0.5em;
index 86475e5..21f6803 100644 (file)
@@ -265,6 +265,19 @@ function getFormPassword() {
        return $("#sone #formPassword").text();
 }
 
+/**
+ * Returns the element of the Sone with the given ID.
+ *
+ * @param soneId
+ *            The ID of the Sone
+ * @returns All Sone elements with the given ID
+ */
+function getSone(soneId) {
+       return $("#sone .sone").filter(function(index) {
+               return $(".id").text() == soneId;
+       });
+}
+
 function getSoneElement(element) {
        return $(element).closest(".sone");
 }
@@ -367,6 +380,39 @@ function getReplyAuthor(element) {
        return getReplyElement(element).find(".reply-author").text();
 }
 
+/**
+ * Returns the notification with the given ID.
+ *
+ * @param notificationId
+ *            The ID of the notification
+ * @returns The notification element
+ */
+function getNotification(notificationId) {
+       return $("#sone #notification-area .notification#" + notificationId);
+}
+
+/**
+ * Returns the notification element closest to the given element.
+ *
+ * @param element
+ *            The element to get the closest notification of
+ * @return The closest notification element
+ */
+function getNotificationElement(element) {
+       return $(element).closest(".notification");
+}
+
+/**
+ * Returns the ID of the notification element.
+ *
+ * @param notificationElement
+ *            The notification element
+ * @returns The ID of the notification
+ */
+function getNotificationId(notificationElement) {
+       return $(notificationElement).attr("id");
+}
+
 function likePost(postId) {
        $.getJSON("like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
                if ((data == null) || !data.success) {
@@ -833,6 +879,90 @@ function ajaxifyNotification(notification) {
        return notification;
 }
 
+/**
+ * Retrieves element IDs from notification elements.
+ *
+ * @param notification
+ *            The notification element
+ * @param selector
+ *            The selector of the element containing the ID as text
+ * @returns All extracted IDs
+ */
+function getElementIds(notification, selector) {
+       elementIds = [];
+       $(selector, notification).each(function() {
+               elementIds.push($(this).text());
+       });
+       return elementIds;
+}
+
+/**
+ * Compares the given notification elements and calls {@link #markSoneAsKnown()}
+ * for every ID that is contained in the old notification but not in the new.
+ *
+ * @param oldNotification
+ *            The old notification element
+ * @param newNotification
+ *            The new notification element
+ */
+function checkForRemovedSones(oldNotification, newNotification) {
+       if (getNotificationId(oldNotification) != "new-sone-notification") {
+               return;
+       }
+       oldIds = getElementIds(oldNotification, ".sone-id");
+       newIds = getElementIds(newNotification, ".sone-id");
+       $.each(oldIds, function(index, value) {
+               if ($.inArray(value, newIds) == -1) {
+                       markSoneAsKnown(getSone(value), true);
+               }
+       });
+}
+
+/**
+ * Compares the given notification elements and calls {@link #markPostAsKnown()}
+ * for every ID that is contained in the old notification but not in the new.
+ *
+ * @param oldNotification
+ *            The old notification element
+ * @param newNotification
+ *            The new notification element
+ */
+function checkForRemovedPosts(oldNotification, newNotification) {
+       if (getNotificationId(oldNotification) != "new-post-notification") {
+               return;
+       }
+       oldIds = getElementIds(oldNotification, ".post-id");
+       newIds = getElementIds(newNotification, ".post-id");
+       $.each(oldIds, function(index, value) {
+               if ($.inArray(value, newIds) == -1) {
+                       markPostAsKnown(getPost(value), true);
+               }
+       });
+}
+
+/**
+ * Compares the given notification elements and calls
+ * {@link #markReplyAsKnown()} for every ID that is contained in the old
+ * notification but not in the new.
+ *
+ * @param oldNotification
+ *            The old notification element
+ * @param newNotification
+ *            The new notification element
+ */
+function checkForRemovedReplies(oldNotification, newNotification) {
+       if (getNotificationId(oldNotification) != "new-replies-notification") {
+               return;
+       }
+       oldIds = getElementIds(oldNotification, ".reply-id");
+       newIds = getElementIds(newNotification, ".reply-id");
+       $.each(oldIds, function(index, value) {
+               if ($.inArray(value, newIds) == -1) {
+                       markReplyAsKnown(getReply(value), true);
+               }
+       });
+}
+
 function getStatus() {
        $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
                if ((data != null) && data.success) {
@@ -851,6 +981,22 @@ function getStatus() {
                                        }
                                });
                                if (!foundNotification) {
+                                       if (notificationId == "new-sone-notification") {
+                                               $(".sone-id", this).each(function(index, element) {
+                                                       soneId = $(this).text();
+                                                       markSoneAsKnown(getSone(soneId), true);
+                                               });
+                                       } else if (notificationId == "new-post-notification") {
+                                               $(".post-id", this).each(function(index, element) {
+                                                       postId = $(this).text();
+                                                       markPostAsKnown(getPost(postId), true);
+                                               });
+                                       } else if (notificationId == "new-replies-notification") {
+                                               $(".reply-id", this).each(function(index, element) {
+                                                       replyId = $(this).text();
+                                                       markReplyAsKnown(getReply(replyId), true);
+                                               });
+                                       }
                                        $(this).slideUp("normal", function() {
                                                $(this).remove();
                                        });
@@ -858,7 +1004,7 @@ function getStatus() {
                        });
                        /* process notifications. */
                        $.each(data.notifications, function(index, value) {
-                               oldNotification = $("#sone #notification-area .notification#" + value.id);
+                               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)) {
@@ -866,6 +1012,9 @@ function getStatus() {
                                                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);
@@ -1083,36 +1232,45 @@ function loadNewReply(replyId, soneId, postId, postSoneId) {
  *
  * @param soneElement
  *            The Sone to mark as known
+ * @param skipRequest
+ *            true to skip the JSON request, false or omit to perform the JSON
+ *            request
  */
-function markSoneAsKnown(soneElement) {
+function markSoneAsKnown(soneElement, skipRequest) {
        if ($(".new", soneElement).length > 0) {
-               $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
-                       $(soneElement).removeClass("new");
-               });
+               if ((typeof skipRequest != "undefined") && !skipRequest) {
+                       $.getJSON("maskAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)}, function(data, textStatus) {
+                               $(soneElement).removeClass("new");
+                       });
+               }
        }
 }
 
-function markPostAsKnown(postElements) {
+function markPostAsKnown(postElements, skipRequest) {
        $(postElements).each(function() {
                postElement = this;
                if ($(postElement).hasClass("new")) {
                        (function(postElement) {
                                $(postElement).removeClass("new");
                                $(".click-to-show", postElement).removeClass("new");
-                               $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
+                               if ((typeof skipRequest == "undefined") || !skipRequest) {
+                                       $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
+                               }
                        })(postElement);
                }
        });
        markReplyAsKnown($(postElements).find(".reply"));
 }
 
-function markReplyAsKnown(replyElements) {
+function markReplyAsKnown(replyElements, skipRequest) {
        $(replyElements).each(function() {
                replyElement = this;
                if ($(replyElement).hasClass("new")) {
                        (function(replyElement) {
                                $(replyElement).removeClass("new");
-                               $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
+                               if ((typeof skipRequest == "undefined") || !skipRequest) {
+                                       $.getJSON("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
+                               }
                        })(replyElement);
                }
        });
index 177b470..54008dc 100644 (file)
@@ -12,6 +12,7 @@
        </form>
        <%= Notification.NewPost.Text|l10n|html>
        <%foreach posts post>
+               <div class="hidden post-id"><%post.id|html></div>
                <a class="link-<% post.id|html>" href="viewPost.html?post=<% post.id|html>"><% post.sone.niceName|html></a><%notlast>,<%/notlast><%last>.<%/last>
        <%/foreach>
 </div>
index 21a5ca0..1253da5 100644 (file)
@@ -12,6 +12,7 @@
        </form>
        <%= Notification.NewReply.Text|l10n|html>
        <%foreach replies reply>
+               <div class="hidden reply-id"><%reply.id|html></div>
                <a class="link-<% reply.post.id|html>" href="viewPost.html?post=<% reply.post.id|html>"><% reply.sone.niceName|html></a><%notlast>,<%/notlast><%last>.<%/last>
        <%/foreach>
 </div>
index 45357b1..aaa0d14 100644 (file)
@@ -12,6 +12,7 @@
        </form>
        <%= Notification.NewSone.Text|l10n|html>
        <%foreach sones sone>
+               <div class="hidden sone-id"><% sone.id|html></div>
                <a href="viewSone.html?sone=<% sone.id|html>" title="<% sone.requestUri|html>"><% sone.niceName|html></a><%notlast>,<%/notlast><%last>.<%/last>
        <%/foreach>
 </div>