Replace create post ajax page with Kotlin version
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / CreateReplyAjaxPage.java
index aa8849a..fe2ae85 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - CreateReplyAjaxPage.java - Copyright © 2010 David Roden
+ * Sone - CreateReplyAjaxPage.java - Copyright © 2010–2016 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
 
 package net.pterodactylus.sone.web.ajax;
 
+import com.google.common.base.Optional;
+
 import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Reply;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.WebInterface;
-import net.pterodactylus.util.json.JsonObject;
+import net.pterodactylus.sone.web.page.FreenetRequest;
 
 /**
  * This AJAX page create a reply.
@@ -37,7 +40,7 @@ public class CreateReplyAjaxPage extends JsonPage {
         *            The Sone web interface
         */
        public CreateReplyAjaxPage(WebInterface webInterface) {
-               super("ajax/createReply.ajax", webInterface);
+               super("createReply.ajax", webInterface);
        }
 
        //
@@ -48,19 +51,21 @@ public class CreateReplyAjaxPage extends JsonPage {
         * {@inheritDoc}
         */
        @Override
-       protected JsonObject createJsonObject(Request request) {
+       protected JsonReturnObject createJsonObject(FreenetRequest request) {
                String postId = request.getHttpRequest().getParam("post");
                String text = request.getHttpRequest().getParam("text").trim();
-               Sone currentSone = getCurrentSone(request.getToadletContext());
-               if (currentSone == null) {
-                       return createErrorJsonObject("auth-required");
+               String senderId = request.getHttpRequest().getParam("sender");
+               Sone sender = webInterface.getCore().getLocalSone(senderId);
+               if (sender == null) {
+                       sender = getCurrentSone(request.getToadletContext());
                }
-               Post post = webInterface.getCore().getPost(postId);
-               if ((post == null) || (post.getSone() == null)) {
+               Optional<Post> post = webInterface.getCore().getPost(postId);
+               if (!post.isPresent()) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               Reply reply = webInterface.getCore().createReply(currentSone, post, text);
-               return new JsonObject().put("success", true).put("reply", reply.getId());
+               text = TextFilter.filter(request.getHttpRequest().getHeader("host"), text);
+               PostReply reply = webInterface.getCore().createReply(sender, post.get(), text);
+               return createSuccessJsonObject().put("reply", reply.getId()).put("sone", sender.getId());
        }
 
 }