Only use the first ID.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 78d68f1..6afb232 100644 (file)
@@ -874,7 +874,7 @@ function ajaxifyPost(postElement) {
                }
        });
        (function(postElement) {
-               var soneId = $(".sone-menu-id", postElement).text();
+               var soneId = $(".sone-menu-id:first", postElement).text();
                $(".sone-post-menu .follow", postElement).click(function() {
                        var followElement = this;
                        ajaxGet("followSone.ajax", { "sone": soneId, "formPassword": getFormPassword() }, function() {
@@ -1091,7 +1091,7 @@ function ajaxifyNotification(notification) {
  * Returns the notification hash. This hash is used in {@link #getStatus()} to
  * determine whether the notifications changed and need to be reloaded.
  */
-public getNotificationHash() {
+function getNotificationHash() {
        return $("#sone #notification-area #notification-hash").text();
 }
 
@@ -1101,7 +1101,7 @@ public getNotificationHash() {
  * @param notificationHash
  *            The new notification hash
  */
-public setNotificationHash(notificationHash) {
+function setNotificationHash(notificationHash) {
        $("#sone #notification-area #notification-hash").text(notificationHash);
 }
 
@@ -1202,8 +1202,31 @@ function getStatus() {
                        }
                        if (data.notificationHash != getNotificationHash()) {
                                console.log("Old hash: ", getNotificationHash(), ", new hash: ", data.notificationHash);
-                               setNotificationHash(data.notificationHash);
+                               requestNotifications();
                        }
+                       /* process new posts. */
+                       $.each(data.newPosts, function(index, value) {
+                               loadNewPost(value.id, value.sone, value.recipient, value.time);
+                       });
+                       /* process new replies. */
+                       $.each(data.newReplies, function(index, value) {
+                               loadNewReply(value.id, value.sone, value.post, value.postSone);
+                       });
+                       /* do it again in 5 seconds. */
+                       setTimeout(getStatus, 5000);
+               } else {
+                       /* data.success was false, wait 30 seconds. */
+                       setTimeout(getStatus, 30000);
+               }
+       }, function() {
+               statusRequestQueued = false;
+               ajaxError();
+       });
+}
+
+function requestNotifications() {
+       ajaxGet("getNotifications.ajax", {}, function(data, textStatus) {
+               if (data && data.success) {
                        /* search for removed notifications. */
                        $("#sone #notification-area .notification").each(function() {
                                notificationId = $(this).attr("id");
@@ -1241,69 +1264,29 @@ function getStatus() {
                                }
                        });
                        /* process notifications. */
-                       notificationIds = [];
                        $.each(data.notifications, function(index, value) {
                                oldNotification = getNotification(value.id);
-                               if ((oldNotification.length == 0) || (value.lastUpdatedTime > getNotificationLastUpdatedTime(oldNotification))) {
-                                       notificationIds.push(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);
+                                       if (value.id.substring(0, 5) != "local") {
+                                               notification.slideDown();
+                                               setActivity();
+                                       }
                                }
                        });
-                       if (notificationIds.length > 0) {
-                               loadNotifications(notificationIds);
-                       }
-                       /* process new posts. */
-                       $.each(data.newPosts, function(index, value) {
-                               loadNewPost(value.id, value.sone, value.recipient, value.time);
-                       });
-                       /* process new replies. */
-                       $.each(data.newReplies, function(index, value) {
-                               loadNewReply(value.id, value.sone, value.post, value.postSone);
-                       });
-                       /* do it again in 5 seconds. */
-                       setTimeout(getStatus, 5000);
-               } else {
-                       /* data.success was false, wait 30 seconds. */
-                       setTimeout(getStatus, 30000);
+                       setNotificationHash(data.notificationHash);
                }
-       }, function() {
-               statusRequestQueued = false;
-               ajaxError();
-       });
-}
-
-/**
- * Requests multiple notifications from Sone and displays them.
- *
- * @param notificationIds
- *            Array of IDs of the notifications to load
- */
-function loadNotifications(notificationIds) {
-       ajaxGet("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);
-                               if (value.id.substring(0, 5) != "local") {
-                                       notification.slideDown();
-                                       setActivity();
-                               }
-                       }
-               });
        });
 }
 
@@ -1533,6 +1516,7 @@ function markSoneAsKnown(soneElement, skipRequest) {
                $(soneElement).removeClass("new");
                if ((typeof skipRequest == "undefined") || !skipRequest) {
                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "sone", "id": getSoneId(soneElement)});
+                       requestNotifications();
                }
        }
 }
@@ -1545,6 +1529,7 @@ function markPostAsKnown(postElements, skipRequest) {
                                $(postElement).removeClass("new");
                                if ((typeof skipRequest == "undefined") || !skipRequest) {
                                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "post", "id": getPostId(postElement)});
+                                       requestNotifications();
                                }
                        })(postElement);
                }
@@ -1561,6 +1546,7 @@ function markReplyAsKnown(replyElements, skipRequest) {
                                $(replyElement).removeClass("new");
                                if ((typeof skipRequest == "undefined") || !skipRequest) {
                                        ajaxGet("markAsKnown.ajax", {"formPassword": getFormPassword(), "type": "reply", "id": getReplyId(replyElement)});
+                                       requestNotifications();
                                }
                        })(replyElement);
                }