Update copyright headers.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / EditImageAjaxPage.java
1 /*
2  * Sone - EditImageAjaxPage.java - Copyright © 2011–2013 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.json.JsonObject;
26 import net.pterodactylus.util.template.TemplateContext;
27
28 import com.google.common.collect.ImmutableMap;
29
30 /**
31  * Page that stores a user’s image modifications.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class EditImageAjaxPage extends JsonPage {
36
37         /** Parser for image descriptions. */
38         private final ParserFilter parserFilter;
39
40         /**
41          * Creates a new edit image AJAX page.
42          *
43          * @param webInterface
44          *            The Sone web interface
45          * @param parserFilter
46          *            The parser filter for image descriptions
47          */
48         public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter) {
49                 super("editImage.ajax", webInterface);
50                 this.parserFilter = parserFilter;
51         }
52
53         //
54         // JSONPAGE METHODS
55         //
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         protected JsonObject createJsonObject(FreenetRequest request) {
62                 String imageId = request.getHttpRequest().getParam("image");
63                 Image image = webInterface.getCore().getImage(imageId, false);
64                 if (image == null) {
65                         return createErrorJsonObject("invalid-image-id");
66                 }
67                 if (!image.getSone().isLocal()) {
68                         return createErrorJsonObject("not-authorized");
69                 }
70                 if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
71                         Image swappedImage = image.getAlbum().moveImageUp(image);
72                         webInterface.getCore().touchConfiguration();
73                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
74                 }
75                 if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
76                         Image swappedImage = image.getAlbum().moveImageDown(image);
77                         webInterface.getCore().touchConfiguration();
78                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
79                 }
80                 String title = request.getHttpRequest().getParam("title").trim();
81                 String description = request.getHttpRequest().getParam("description").trim();
82                 image.setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description));
83                 webInterface.getCore().touchConfiguration();
84                 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()));
85         }
86
87 }