Move web pages to their own package
[Sone.git] / src / main / java / net / pterodactylus / sone / web / pages / MarkAsKnownPage.java
1 /*
2  * Sone - MarkAsKnownPage.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.pages;
19
20 import java.util.StringTokenizer;
21
22 import net.pterodactylus.sone.data.Post;
23 import net.pterodactylus.sone.data.PostReply;
24 import net.pterodactylus.sone.data.Reply;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.web.WebInterface;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30
31 import com.google.common.base.Optional;
32
33 /**
34  * Page that lets the user mark a number of {@link Sone}s, {@link Post}s, or
35  * {@link Reply Replie}s as known.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class MarkAsKnownPage extends SoneTemplatePage {
40
41         /**
42          * Creates a new “mark as known” page.
43          *
44          * @param template
45          *            The template to render
46          * @param webInterface
47          *            The Sone web interface
48          */
49         public MarkAsKnownPage(Template template, WebInterface webInterface) {
50                 super("markAsKnown.html", template, "Page.MarkAsKnown.Title", webInterface);
51         }
52
53         //
54         // SONETEMPLATEPAGE METHODS
55         //
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
62                 String type = request.getHttpRequest().getPartAsStringFailsafe("type", 5);
63                 if (!type.equals("sone") && !type.equals("post") && !type.equals("reply")) {
64                         throw new RedirectException("invalid.html");
65                 }
66                 String ids = request.getHttpRequest().getPartAsStringFailsafe("id", 65536);
67                 for (StringTokenizer idTokenizer = new StringTokenizer(ids); idTokenizer.hasMoreTokens();) {
68                         String id = idTokenizer.nextToken();
69                         if (type.equals("post")) {
70                                 Optional<Post> post = webInterface.getCore().getPost(id);
71                                 if (!post.isPresent()) {
72                                         continue;
73                                 }
74                                 webInterface.getCore().markPostKnown(post.get());
75                         } else if (type.equals("reply")) {
76                                 Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
77                                 if (!reply.isPresent()) {
78                                         continue;
79                                 }
80                                 webInterface.getCore().markReplyKnown(reply.get());
81                         } else {
82                                 Optional<Sone> sone = webInterface.getCore().getSone(id);
83                                 if (!sone.isPresent()) {
84                                         continue;
85                                 }
86                                 webInterface.getCore().markSoneKnown(sone.get());
87                         }
88                 }
89                 String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
90                 throw new RedirectException(returnPage);
91         }
92
93 }