Fix some Kotlin compiler warnings
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / EditImageAjaxPage.java
index 0c45225..d6256c1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - EditImageAjaxPage.java - Copyright © 2011–2013 David Roden
+ * Sone - EditImageAjaxPage.java - Copyright © 2011–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.collect.ImmutableMap;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.template.ParserFilter;
+import net.pterodactylus.sone.template.RenderFilter;
+import net.pterodactylus.sone.template.ShortenFilter;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.TemplateContext;
 
-import com.google.common.collect.ImmutableMap;
-
 /**
  * Page that stores a user’s image modifications.
  *
@@ -33,8 +34,9 @@ import com.google.common.collect.ImmutableMap;
  */
 public class EditImageAjaxPage extends JsonPage {
 
-       /** Parser for image descriptions. */
        private final ParserFilter parserFilter;
+       private final ShortenFilter shortenFilter;
+       private final RenderFilter renderFilter;
 
        /**
         * Creates a new edit image AJAX page.
@@ -44,9 +46,11 @@ public class EditImageAjaxPage extends JsonPage {
         * @param parserFilter
         *            The parser filter for image descriptions
         */
-       public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter) {
+       public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, ShortenFilter shortenFilter, RenderFilter renderFilter) {
                super("editImage.ajax", webInterface);
                this.parserFilter = parserFilter;
+               this.shortenFilter = shortenFilter;
+               this.renderFilter = renderFilter;
        }
 
        //
@@ -77,10 +81,21 @@ public class EditImageAjaxPage extends JsonPage {
                        return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
                }
                String title = request.getHttpRequest().getParam("title").trim();
+               if (title.isEmpty()) {
+                       return createErrorJsonObject("invalid-image-title");
+               }
                String description = request.getHttpRequest().getParam("description").trim();
                image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
                webInterface.getCore().touchConfiguration();
-               return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build()));
+               return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", renderImageDescription(image));
+       }
+
+       private String renderImageDescription(Image image) {
+               TemplateContext templateContext = new TemplateContext();
+               ImmutableMap<String, Object> parameters = ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build();
+               Object parts = parserFilter.format(templateContext, image.getDescription(), parameters);
+               Object shortenedParts = shortenFilter.format(templateContext, parts, parameters);
+               return (String) renderFilter.format(templateContext, shortenedParts, parameters);
        }
 
 }