String text = request.getHttpRequest().getParam("text").trim();
Sone currentSone = getCurrentSone(request.getToadletContext());
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
Post post = webInterface.getCore().getPost(postId);
if ((post == null) || (post.getSone() == null)) {
- return new JsonObject().put("success", false).put("error", "invalid-post-id");
+ return createErrorJsonObject("invalid-post-id");
}
Reply reply = webInterface.getCore().createReply(currentSone, post, text);
return new JsonObject().put("success", true).put("reply", reply.getId());
Post post = webInterface.getCore().getPost(postId);
Sone currentSone = getCurrentSone(request.getToadletContext());
if (post == null) {
- return new JsonObject().put("success", false).put("error", "invalid-post-id");
+ return createErrorJsonObject("invalid-post-id");
}
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
if (!post.getSone().equals(currentSone)) {
- return new JsonObject().put("success", false).put("error", "not-authorized");
+ return createErrorJsonObject("not-authorized");
}
currentSone.removePost(post);
return new JsonObject().put("success", true);
Reply reply = webInterface.getCore().getReply(replyId);
Sone currentSone = getCurrentSone(request.getToadletContext());
if (reply == null) {
- return new JsonObject().put("success", false).put("error", "invalid-reply-id");
+ return createErrorJsonObject("invalid-reply-id");
}
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
if (!reply.getSone().equals(currentSone)) {
- return new JsonObject().put("success", false).put("error", "not-authorized");
+ return createErrorJsonObject("not-authorized");
}
webInterface.getCore().deleteReply(reply);
return new JsonObject().put("success", true);
protected JsonObject createJsonObject(Request request) {
String soneId = request.getHttpRequest().getParam("sone");
if (!webInterface.getCore().hasSone(soneId)) {
- return new JsonObject().put("success", false).put("error", "invalid-sone-id");
+ return createErrorJsonObject("invalid-sone-id");
}
Sone currentSone = getCurrentSone(request.getToadletContext());
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
currentSone.addFriend(soneId);
webInterface.getCore().saveSone(currentSone);
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
- return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+ return createErrorJsonObject("invalid-" + type + "-id");
}
if ("post".equals(type)) {
Post post = webInterface.getCore().getPost(id);
Set<Sone> sones = webInterface.getCore().getLikes(reply);
return new JsonObject().put("success", true).put("likes", sones.size()).put("sones", getSones(sones));
}
- return new JsonObject().put("success", false).put("error", "invalid-type");
+ return createErrorJsonObject("invalid-type");
}
/**
}
//
+ // PROTECTED METHODS
+ //
+
+ /**
+ * Creates an error reply.
+ *
+ * @param error
+ * The error that has occured
+ * @return The JSON object, signalling failure and the error code
+ */
+ protected JsonObject createErrorJsonObject(String error) {
+ return new JsonObject().put("success", false).put("error", error);
+ }
+
+ //
// PAGE METHODS
//
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
- return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+ return createErrorJsonObject("invalid-" + type + "-id");
}
Sone currentSone = getCurrentSone(request.getToadletContext());
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
if ("post".equals(type)) {
currentSone.addLikedPostId(id);
} else if ("reply".equals(type)) {
currentSone.addLikedReplyId(id);
} else {
- return new JsonObject().put("success", false).put("error", "invalid-type");
+ return createErrorJsonObject("invalid-type");
}
return new JsonObject().put("success", true);
}
protected JsonObject createJsonObject(Request request) {
String soneId = request.getHttpRequest().getParam("sone");
if (!webInterface.getCore().hasSone(soneId)) {
- return new JsonObject().put("success", false).put("error", "invalid-sone-id");
+ return createErrorJsonObject("invalid-sone-id");
}
Sone currentSone = getCurrentSone(request.getToadletContext());
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
currentSone.removeFriend(soneId);
webInterface.getCore().saveSone(currentSone);
String type = request.getHttpRequest().getParam("type", null);
String id = request.getHttpRequest().getParam(type, null);
if ((id == null) || (id.length() == 0)) {
- return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id");
+ return createErrorJsonObject("invalid-" + type + "-id");
}
Sone currentSone = getCurrentSone(request.getToadletContext());
if (currentSone == null) {
- return new JsonObject().put("success", false).put("error", "auth-required");
+ return createErrorJsonObject("auth-required");
}
if ("post".equals(type)) {
currentSone.removeLikedPostId(id);
} else if ("reply".equals(type)) {
currentSone.removeLikedReplyId(id);
} else {
- return new JsonObject().put("success", false).put("error", "invalid-type");
+ return createErrorJsonObject("invalid-type");
}
return new JsonObject().put("success", true);
}