From: David ‘Bombe’ Roden Date: Thu, 7 Apr 2011 09:27:58 +0000 (+0200) Subject: Merge branch 'mark-elements-as-known' into next X-Git-Tag: 0.6.1^2~26 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=9d341071d7fc10118038e238d29ce7bd38ff08c4;hp=e42a8f16b2ed6c3650cb288abbf48932ef63d97b Merge branch 'mark-elements-as-known' into next This fixes #119. --- diff --git a/src/main/java/net/pterodactylus/sone/web/IndexPage.java b/src/main/java/net/pterodactylus/sone/web/IndexPage.java index 2d85e02..db48314 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -77,21 +77,15 @@ public class IndexPage extends SoneTemplatePage { Pagination pagination = new Pagination(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 posts = (List) 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); } } + } } diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index c0e6364..6b1b4e3 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -62,17 +62,9 @@ public class KnownSonesPage extends SoneTemplatePage { Pagination sonePagination = new Pagination(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 sones = (List) templateContext.get("knownSones"); - for (Sone sone : sones) { + /* mark Sones as known. */ + for (Sone sone : sonePagination.getItems()) { webInterface.getCore().markSoneKnown(sone); } } diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 2b854f3..73673ab 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -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); + } + } } // diff --git a/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java b/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java index e528709..fe63b34 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewPostPage.java @@ -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); diff --git a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index c7edc32..82284d5 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -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 repliedPostPagination = new Pagination(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 posts = (List) templateContext.get("posts"); - posts.addAll((List) templateContext.get("repliedPosts")); - for (Post post : posts) { + for (Post post : new ListBuilder().addAll(postPagination.getItems()).addAll(repliedPostPagination.getItems()).get()) { if (post.getSone() != null) { webInterface.getCore().markPostKnown(post); } diff --git a/src/main/resources/static/css/sone.css b/src/main/resources/static/css/sone.css index 38fcd19..f6bb62d 100644 --- a/src/main/resources/static/css/sone.css +++ b/src/main/resources/static/css/sone.css @@ -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; diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index 86475e5..21f6803 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -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); } }); diff --git a/src/main/resources/templates/notify/newPostNotification.html b/src/main/resources/templates/notify/newPostNotification.html index 177b470..54008dc 100644 --- a/src/main/resources/templates/notify/newPostNotification.html +++ b/src/main/resources/templates/notify/newPostNotification.html @@ -12,6 +12,7 @@ <%= Notification.NewPost.Text|l10n|html> <%foreach posts post> + <% post.sone.niceName|html><%notlast>,<%/notlast><%last>.<%/last> <%/foreach> diff --git a/src/main/resources/templates/notify/newReplyNotification.html b/src/main/resources/templates/notify/newReplyNotification.html index 21a5ca0..1253da5 100644 --- a/src/main/resources/templates/notify/newReplyNotification.html +++ b/src/main/resources/templates/notify/newReplyNotification.html @@ -12,6 +12,7 @@ <%= Notification.NewReply.Text|l10n|html> <%foreach replies reply> + <% reply.sone.niceName|html><%notlast>,<%/notlast><%last>.<%/last> <%/foreach> diff --git a/src/main/resources/templates/notify/newSoneNotification.html b/src/main/resources/templates/notify/newSoneNotification.html index 45357b1..aaa0d14 100644 --- a/src/main/resources/templates/notify/newSoneNotification.html +++ b/src/main/resources/templates/notify/newSoneNotification.html @@ -12,6 +12,7 @@ <%= Notification.NewSone.Text|l10n|html> <%foreach sones sone> + <% sone.niceName|html><%notlast>,<%/notlast><%last>.<%/last> <%/foreach>