From: David ‘Bombe’ Roden Date: Fri, 19 Nov 2010 22:18:26 +0000 (+0100) Subject: Replace getSoneStatus and getNotifications with getStatus. X-Git-Tag: 0.3.1-RC1~55 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=80822a0b544648746b293b562eed1da88e439991;p=Sone.git Replace getSoneStatus and getNotifications with getStatus. getStatus returns information about everything that needs to be updated in the web interface. --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 441e323..31f9f0d 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -54,9 +54,8 @@ import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage; import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage; import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage; import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage; -import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage; import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage; -import net.pterodactylus.sone.web.ajax.GetSoneStatusPage; +import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage; import net.pterodactylus.sone.web.ajax.GetTranslationPage; import net.pterodactylus.sone.web.ajax.LikeAjaxPage; import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage; @@ -337,9 +336,8 @@ public class WebInterface implements CoreListener { pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png"))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this))); - pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this))); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java deleted file mode 100644 index 2264b2e..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetNotificationsAjaxPage.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Sone - GetNotificationsAjaxPage.java - Copyright © 2010 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.ajax; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.util.json.JsonArray; -import net.pterodactylus.util.json.JsonObject; -import net.pterodactylus.util.notify.Notification; - -/** - * Returns all changed notifications. - * - * @author David ‘Bombe’ Roden - */ -public class GetNotificationsAjaxPage extends JsonPage { - - /** - * Creates a new “get notifications” AJAX handler. - * - * @param webInterface - * The Sone web interface - */ - public GetNotificationsAjaxPage(WebInterface webInterface) { - super("ajax/getNotifications.ajax", webInterface); - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonObject createJsonObject(Request request) { - List notifications = new ArrayList(webInterface.getNotifications().getChangedNotifications()); - Set removedNotifications = webInterface.getNotifications().getRemovedNotifications(); - Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER); - JsonObject result = createSuccessJsonObject(); - JsonArray jsonNotifications = new JsonArray(); - for (Notification notification : notifications) { - jsonNotifications.add(createJsonNotification(notification)); - } - JsonArray jsonRemovedNotifications = new JsonArray(); - for (Notification notification : removedNotifications) { - jsonRemovedNotifications.add(createJsonNotification(notification)); - } - return result.put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean needsFormPassword() { - return false; - } - - // - // PRIVATE METHODS - // - - /** - * Creates a JSON object from the given notification. - * - * @param notification - * The notification to create a JSON object - * @return The JSON object - */ - private static JsonObject createJsonNotification(Notification notification) { - JsonObject jsonNotification = new JsonObject(); - jsonNotification.put("id", notification.getId()); - jsonNotification.put("text", notification.toString()); - jsonNotification.put("createdTime", notification.getCreatedTime()); - jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime()); - jsonNotification.put("dismissable", notification.isDismissable()); - return jsonNotification; - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java deleted file mode 100644 index 47d377a..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Sone - GetSoneStatusPage.java - Copyright © 2010 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.ajax; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import net.pterodactylus.sone.core.Core; -import net.pterodactylus.sone.core.Core.SoneStatus; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.template.SoneAccessor; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.util.json.JsonObject; - -/** - * AJAX page that reeturns the status of a sone, as a) a {@link SoneStatus} name - * and b) a “modified” boolean (as per {@link Core#isModifiedSone(Sone)}). - * - * @author David ‘Bombe’ Roden - */ -public class GetSoneStatusPage extends JsonPage { - - /** - * Creates a new AJAX sone status handler. - * - * @param webInterface - * The Sone web interface - */ - public GetSoneStatusPage(WebInterface webInterface) { - super("ajax/getSoneStatus.ajax", webInterface); - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonObject createJsonObject(Request request) { - String soneId = request.getHttpRequest().getParam("sone"); - Sone sone = webInterface.getCore().getSone(soneId); - SoneStatus soneStatus = webInterface.getCore().getSoneStatus(sone); - return createSuccessJsonObject().put("status", soneStatus.name()).put("name", SoneAccessor.getNiceName(sone)).put("modified", webInterface.getCore().isModifiedSone(sone)).put("locked", webInterface.getCore().isLocked(sone)).put("lastUpdated", new SimpleDateFormat("MMM d, yyyy, HH:mm:ss").format(new Date(sone.getTime()))).put("age", (System.currentTimeMillis() - sone.getTime()) / 1000); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean needsFormPassword() { - return false; - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java new file mode 100644 index 0000000..831bb6f --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -0,0 +1,135 @@ +/* + * Sone - GetStatusAjaxPage.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.ajax; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Set; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.template.SoneAccessor; +import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.util.json.JsonArray; +import net.pterodactylus.util.json.JsonObject; +import net.pterodactylus.util.notify.Notification; + +/** + * The “get status” AJAX handler returns all information that is necessary to + * update the web interface in real-time. + * + * @author David ‘Bombe’ Roden + */ +public class GetStatusAjaxPage extends JsonPage { + + /** Date formatter. */ + private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss"); + + /** + * Creates a new “get status” AJAX handler. + * + * @param webInterface + * The Sone web interface + */ + public GetStatusAjaxPage(WebInterface webInterface) { + super("ajax/getStatus.ajax", webInterface); + } + + /** + * {@inheritDoc} + */ + @Override + protected JsonObject createJsonObject(Request request) { + /* load Sones. */ + boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true")); + Set sones = loadAllSones ? webInterface.getCore().getSones() : Collections.singleton(getCurrentSone(request.getToadletContext())); + JsonArray jsonSones = new JsonArray(); + for (Sone sone : sones) { + JsonObject jsonSone = createJsonSone(sone); + jsonSones.add(jsonSone); + } + /* load notifications. */ + List notifications = new ArrayList(webInterface.getNotifications().getChangedNotifications()); + Set removedNotifications = webInterface.getNotifications().getRemovedNotifications(); + 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)); + } + return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications); + } + + /** + * {@inheritDoc} + */ + @Override + protected boolean needsFormPassword() { + return false; + } + + // + // PRIVATE METHODS + // + + /** + * Creates a JSON object from the given Sone. + * + * @param sone + * The Sone to convert to a JSON object + * @return The JSON representation of the given Sone + */ + private JsonObject createJsonSone(Sone sone) { + JsonObject jsonSone = new JsonObject(); + jsonSone.put("id", sone.getId()); + jsonSone.put("name", SoneAccessor.getNiceName(sone)); + jsonSone.put("local", sone.getInsertUri() != null); + jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name()); + jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone)); + jsonSone.put("locked", webInterface.getCore().isLocked(sone)); + synchronized (dateFormat) { + jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime()))); + } + jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000); + return jsonSone; + } + + /** + * Creates a JSON object from the given notification. + * + * @param notification + * The notification to create a JSON object + * @return The JSON object + */ + private static JsonObject createJsonNotification(Notification notification) { + JsonObject jsonNotification = new JsonObject(); + jsonNotification.put("id", notification.getId()); + jsonNotification.put("text", notification.toString()); + jsonNotification.put("createdTime", notification.getCreatedTime()); + jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime()); + jsonNotification.put("dismissable", notification.isDismissable()); + return jsonNotification; + } + +} diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js index e29c136..a346894 100644 --- a/src/main/resources/static/javascript/sone.js +++ b/src/main/resources/static/javascript/sone.js @@ -119,33 +119,6 @@ function getTranslation(key, callback) { } /** - * Fires off an AJAX request to retrieve the current status of a Sone. - * - * @param soneId - * The ID of the Sone - * @param local - * true if the Sone is local, false - * otherwise - */ -function getSoneStatus(soneId, local) { - $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) { - if ((data != null) && data.success) { - updateSoneStatus(soneId, data.name, data.status, data.modified, data.locked, data.lastUpdated); - } - /* seconds! */ - updateInterval = 60; - if (local || (data!= null) && (data.modified || (data.status == "downloading") || (data.status == "inserting"))) { - updateInterval = 5; - } - setTimeout(function() { - getSoneStatus(soneId, local); - }, updateInterval * 1000); - }, function(xmlHttpRequest, textStatus, error) { - /* ignore error. */ - }); -} - -/** * Filters the given Sone ID, replacing all “~” characters by an underscore. * * @param soneId @@ -184,29 +157,6 @@ function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) { $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name); } -var watchedSones = {}; - -/** - * Watches this Sone for updates to its status. - * - * @param soneId - * The ID of the Sone to watch - * @param local - * true if the Sone is local, false - * otherwise - */ -function watchSone(soneId, local) { - if (watchedSones[soneId]) { - return; - } - watchedSones[soneId] = true; - (function(soneId) { - setTimeout(function() { - getSoneStatus(soneId, local); - }, 5000); - })(soneId); -} - /** * Enhances a “delete” button so that the confirmation is done on the same page. * @@ -502,12 +452,14 @@ function ajaxifyNotification(notification) { return notification; } -/** - * Retrieves all changed notifications. - */ -function getNotifications() { - $.getJSON("ajax/getNotifications.ajax", {}, function(data, textStatus) { +function getStatus() { + $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) { if ((data != null) && data.success) { + /* process Sone information. */ + $.each(data.sones, function(index, value) { + updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated); + }); + /* process notifications. */ $.each(data.notifications, function(index, value) { oldNotification = $("#sone #notification-area .notification#" + value.id); notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide(); @@ -521,13 +473,16 @@ function getNotifications() { $.each(data.removedNotifications, function(index, value) { $("#sone #notification-area .notification#" + value.id).slideUp(); }); - setTimeout(getNotifications, 5000); + /* do it again in 5 seconds. */ + setTimeout(getStatus, 5000); } else { - setTimeout(getNotifications, 30000); + /* data.success was false, wait 30 seconds. */ + setTimeout(getStatus, 30000); } }, function(xmlHttpRequest, textStatus, error) { - /* ignore error. */ - }); + /* something really bad happend, wait a minute. */ + setTimeout(getStatus, 60000); + }) } /** @@ -563,11 +518,6 @@ $(document).ready(function() { registerInputTextareaSwap("#sone #update-status .status-input", text, "text", false, false); }) - /* these functions are necessary for updating Sone statuses. */ - $("#sone .sone").each(function() { - watchSone($(this).find(".id").text(), $(this).hasClass("local")); - }); - /* this initializes all reply input fields. */ getTranslation("WebInterface.DefaultText.Reply", function(text) { registerInputTextareaSwap("#sone input.reply-input", text, "text", false, false); @@ -716,6 +666,6 @@ $(document).ready(function() { ajaxifyNotification($(this)); }); - /* activate notification polling. */ - setTimeout(getNotifications, 5000); + /* activate status polling. */ + setTimeout(getStatus, 5000); });