Change code to use new way to specify filter parameters.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / EditImageAjaxPage.java
1 /*
2  * Sone - EditImageAjaxPage.java - Copyright © 2011–2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
20 import net.pterodactylus.sone.data.Image;
21 import net.pterodactylus.sone.template.ParserFilter;
22 import net.pterodactylus.sone.text.TextFilter;
23 import net.pterodactylus.sone.web.WebInterface;
24 import net.pterodactylus.sone.web.page.FreenetRequest;
25 import net.pterodactylus.util.collection.MapBuilder;
26 import net.pterodactylus.util.json.JsonObject;
27 import net.pterodactylus.util.template.TemplateContext;
28
29 /**
30  * Page that stores a user’s image modifications.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class EditImageAjaxPage extends JsonPage {
35
36         /** Parser for image descriptions. */
37         private final ParserFilter parserFilter;
38
39         /**
40          * Creates a new edit image AJAX page.
41          *
42          * @param webInterface
43          *            The Sone web interface
44          * @param parserFilter
45          *            The parser filter for image descriptions
46          */
47         public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter) {
48                 super("editImage.ajax", webInterface);
49                 this.parserFilter = parserFilter;
50         }
51
52         //
53         // JSONPAGE METHODS
54         //
55
56         /**
57          * {@inheritDoc}
58          */
59         @Override
60         protected JsonObject createJsonObject(FreenetRequest request) {
61                 String imageId = request.getHttpRequest().getParam("image");
62                 Image image = webInterface.getCore().getImage(imageId, false);
63                 if (image == null) {
64                         return createErrorJsonObject("invalid-image-id");
65                 }
66                 if (!webInterface.getCore().isLocalSone(image.getSone())) {
67                         return createErrorJsonObject("not-authorized");
68                 }
69                 if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
70                         Image swappedImage = image.getAlbum().moveImageUp(image);
71                         webInterface.getCore().touchConfiguration();
72                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
73                 }
74                 if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
75                         Image swappedImage = image.getAlbum().moveImageDown(image);
76                         webInterface.getCore().touchConfiguration();
77                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
78                 }
79                 String title = request.getHttpRequest().getParam("title").trim();
80                 String description = request.getHttpRequest().getParam("description").trim();
81                 image.setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
82                 webInterface.getCore().touchConfiguration();
83                 return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), new MapBuilder<String, Object>().put("sone", image.getSone()).get()));
84         }
85
86 }