Rename session-related Methode in web interface
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / EditImageAjaxPage.java
1 /*
2  * Sone - EditImageAjaxPage.java - Copyright © 2011–2016 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 com.google.common.collect.ImmutableMap;
21 import net.pterodactylus.sone.data.Image;
22 import net.pterodactylus.sone.template.ParserFilter;
23 import net.pterodactylus.sone.template.RenderFilter;
24 import net.pterodactylus.sone.template.ShortenFilter;
25 import net.pterodactylus.sone.text.TextFilter;
26 import net.pterodactylus.sone.web.WebInterface;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.template.TemplateContext;
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         private final ParserFilter parserFilter;
38         private final ShortenFilter shortenFilter;
39         private final RenderFilter renderFilter;
40
41         /**
42          * Creates a new edit image AJAX page.
43          *
44          * @param webInterface
45          *            The Sone web interface
46          * @param parserFilter
47          *            The parser filter for image descriptions
48          */
49         public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, ShortenFilter shortenFilter, RenderFilter renderFilter) {
50                 super("editImage.ajax", webInterface);
51                 this.parserFilter = parserFilter;
52                 this.shortenFilter = shortenFilter;
53                 this.renderFilter = renderFilter;
54         }
55
56         //
57         // JSONPAGE METHODS
58         //
59
60         /**
61          * {@inheritDoc}
62          */
63         @Override
64         protected JsonReturnObject createJsonObject(FreenetRequest request) {
65                 String imageId = request.getHttpRequest().getParam("image");
66                 Image image = webInterface.getCore().getImage(imageId, false);
67                 if (image == null) {
68                         return createErrorJsonObject("invalid-image-id");
69                 }
70                 if (!image.getSone().isLocal()) {
71                         return createErrorJsonObject("not-authorized");
72                 }
73                 if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
74                         Image swappedImage = image.getAlbum().moveImageUp(image);
75                         webInterface.getCore().touchConfiguration();
76                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
77                 }
78                 if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
79                         Image swappedImage = image.getAlbum().moveImageDown(image);
80                         webInterface.getCore().touchConfiguration();
81                         return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId());
82                 }
83                 String title = request.getHttpRequest().getParam("title").trim();
84                 if (title.isEmpty()) {
85                         return createErrorJsonObject("invalid-image-title");
86                 }
87                 String description = request.getHttpRequest().getParam("description").trim();
88                 image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
89                 webInterface.getCore().touchConfiguration();
90                 return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", renderImageDescription(image));
91         }
92
93         private String renderImageDescription(Image image) {
94                 TemplateContext templateContext = new TemplateContext();
95                 ImmutableMap<String, Object> parameters = ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build();
96                 Object parts = parserFilter.format(templateContext, image.getDescription(), parameters);
97                 Object shortenedParts = shortenFilter.format(templateContext, parts, parameters);
98                 return (String) renderFilter.format(templateContext, shortenedParts, parameters);
99         }
100
101 }