From 341d8308a20e234dbec084be1b370367fa63ff94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 14 Nov 2010 15:07:22 +0100 Subject: [PATCH] =?utf8?q?Add=20method=20to=20create=20a=20=E2=80=9Csucces?= =?utf8?q?s=E2=80=9D=20JSON=20object.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java | 2 +- .../net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java | 4 ++-- .../java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/GetTranslationPage.java | 2 +- src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java | 9 +++++++++ src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java | 2 +- .../net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java | 2 +- .../java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java | 2 +- 12 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java index aa8849a..486fb75 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/CreateReplyAjaxPage.java @@ -60,7 +60,7 @@ public class CreateReplyAjaxPage extends JsonPage { return createErrorJsonObject("invalid-post-id"); } Reply reply = webInterface.getCore().createReply(currentSone, post, text); - return new JsonObject().put("success", true).put("reply", reply.getId()); + return createSuccessJsonObject().put("reply", reply.getId()); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java index 16c047d..603c013 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeletePostAjaxPage.java @@ -61,7 +61,7 @@ public class DeletePostAjaxPage extends JsonPage { return createErrorJsonObject("not-authorized"); } currentSone.removePost(post); - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java index b34ca5a..6563a7c 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/DeleteReplyAjaxPage.java @@ -61,7 +61,7 @@ public class DeleteReplyAjaxPage extends JsonPage { return createErrorJsonObject("not-authorized"); } webInterface.getCore().deleteReply(reply); - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java index e90fa5f..5cb03e1 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/FollowSoneAjaxPage.java @@ -53,7 +53,7 @@ public class FollowSoneAjaxPage extends JsonPage { } currentSone.addFriend(soneId); webInterface.getCore().saveSone(currentSone); - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index 7b55faa..58b1265 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -64,11 +64,11 @@ public class GetLikesAjaxPage extends JsonPage { if ("post".equals(type)) { Post post = webInterface.getCore().getPost(id); Set sones = webInterface.getCore().getLikes(post); - return new JsonObject().put("success", true).put("likes", sones.size()).put("sones", getSones(sones)); + return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } else if ("reply".equals(type)) { Reply reply = webInterface.getCore().getReply(id); Set sones = webInterface.getCore().getLikes(reply); - return new JsonObject().put("success", true).put("likes", sones.size()).put("sones", getSones(sones)); + return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } return createErrorJsonObject("invalid-type"); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java index 292f3bf..6b94874 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -81,7 +81,7 @@ public class GetReplyAjaxPage extends JsonPage { Closer.close(templateWriter); } synchronized (dateFormat) { - return new JsonObject().put("success", true).put("soneId", reply.getSone().getId()).put("soneName", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("displayTime", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()).put("html", templateWriter.toString()); + return createSuccessJsonObject().put("soneId", reply.getSone().getId()).put("soneName", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("displayTime", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()).put("html", templateWriter.toString()); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java index ff8e181..13ed15e 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetSoneStatusPage.java @@ -57,7 +57,7 @@ public class GetSoneStatusPage extends JsonPage { String soneId = request.getHttpRequest().getParam("sone"); Sone sone = webInterface.getCore().getSone(soneId); SoneStatus soneStatus = webInterface.getCore().getSoneStatus(sone); - return new JsonObject().put("status", soneStatus.name()).put("name", SoneAccessor.getNiceName(sone)).put("modified", webInterface.getCore().isModifiedSone(sone)).put("lastUpdated", new SimpleDateFormat("MMM d, yyyy, HH:mm:ss").format(new Date(sone.getTime()))).put("age", (System.currentTimeMillis() - sone.getTime()) / 1000); + return createSuccessJsonObject().put("status", soneStatus.name()).put("name", SoneAccessor.getNiceName(sone)).put("modified", webInterface.getCore().isModifiedSone(sone)).put("lastUpdated", new SimpleDateFormat("MMM d, yyyy, HH:mm:ss").format(new Date(sone.getTime()))).put("age", (System.currentTimeMillis() - sone.getTime()) / 1000); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTranslationPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTranslationPage.java index 20f1636..014bc88 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTranslationPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetTranslationPage.java @@ -48,7 +48,7 @@ public class GetTranslationPage extends JsonPage { protected JsonObject createJsonObject(Request request) { String key = request.getHttpRequest().getParam("key"); String translation = webInterface.getL10n().getString(key); - return new JsonObject().put("value", translation); + return createSuccessJsonObject().put("value", translation); } /** diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java index 8e7d27e..b9bdc77 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonPage.java @@ -145,6 +145,15 @@ public abstract class JsonPage implements Page { // /** + * Creates a success reply. + * + * @return A reply signaling success + */ + protected JsonObject createSuccessJsonObject() { + return new JsonObject().put("success", true); + } + + /** * Creates an error reply. * * @param error diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java index 258eb7f..1a49ccd 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java @@ -60,7 +60,7 @@ public class LikeAjaxPage extends JsonPage { } else { return createErrorJsonObject("invalid-type"); } - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java index 7e620ef..586ab55 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnfollowSoneAjaxPage.java @@ -53,7 +53,7 @@ public class UnfollowSoneAjaxPage extends JsonPage { } currentSone.removeFriend(soneId); webInterface.getCore().saveSone(currentSone); - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java index ecacf98..6a4dde7 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -60,7 +60,7 @@ public class UnlikeAjaxPage extends JsonPage { } else { return createErrorJsonObject("invalid-type"); } - return new JsonObject().put("success", true); + return createSuccessJsonObject(); } } -- 2.7.4