2 * Sone - WebInterface.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.UUID;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.ScheduledExecutorService;
37 import java.util.concurrent.ScheduledFuture;
38 import java.util.concurrent.TimeUnit;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
42 import net.pterodactylus.sone.core.Core;
43 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
46 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
47 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
48 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
49 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
50 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
51 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
52 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
53 import net.pterodactylus.sone.core.event.PostRemovedEvent;
54 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
55 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
57 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
58 import net.pterodactylus.sone.core.event.SoneLockedEvent;
59 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
60 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
61 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
62 import net.pterodactylus.sone.data.Album;
63 import net.pterodactylus.sone.data.Image;
64 import net.pterodactylus.sone.data.Post;
65 import net.pterodactylus.sone.data.PostReply;
66 import net.pterodactylus.sone.data.Profile;
67 import net.pterodactylus.sone.data.Reply;
68 import net.pterodactylus.sone.data.Sone;
69 import net.pterodactylus.sone.freenet.L10nFilter;
70 import net.pterodactylus.sone.freenet.wot.Identity;
71 import net.pterodactylus.sone.freenet.wot.Trust;
72 import net.pterodactylus.sone.main.SonePlugin;
73 import net.pterodactylus.sone.notify.ListNotification;
74 import net.pterodactylus.sone.template.AlbumAccessor;
75 import net.pterodactylus.sone.template.CollectionAccessor;
76 import net.pterodactylus.sone.template.CssClassNameFilter;
77 import net.pterodactylus.sone.template.HttpRequestAccessor;
78 import net.pterodactylus.sone.template.IdentityAccessor;
79 import net.pterodactylus.sone.template.ImageAccessor;
80 import net.pterodactylus.sone.template.ImageLinkFilter;
81 import net.pterodactylus.sone.template.JavascriptFilter;
82 import net.pterodactylus.sone.template.ParserFilter;
83 import net.pterodactylus.sone.template.PostAccessor;
84 import net.pterodactylus.sone.template.ProfileAccessor;
85 import net.pterodactylus.sone.template.ReplyAccessor;
86 import net.pterodactylus.sone.template.ReplyGroupFilter;
87 import net.pterodactylus.sone.template.RequestChangeFilter;
88 import net.pterodactylus.sone.template.SoneAccessor;
89 import net.pterodactylus.sone.template.SubstringFilter;
90 import net.pterodactylus.sone.template.TrustAccessor;
91 import net.pterodactylus.sone.template.UniqueElementFilter;
92 import net.pterodactylus.sone.template.UnknownDateFilter;
93 import net.pterodactylus.sone.text.Part;
94 import net.pterodactylus.sone.text.SonePart;
95 import net.pterodactylus.sone.text.SoneTextParser;
96 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
97 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
98 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
99 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
100 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
101 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
102 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
104 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
105 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
106 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
107 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
108 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
109 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
111 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
115 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
116 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
117 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
118 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
119 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
120 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
121 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
122 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
123 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
125 import net.pterodactylus.sone.web.page.FreenetRequest;
126 import net.pterodactylus.sone.web.page.PageToadlet;
127 import net.pterodactylus.sone.web.page.PageToadletFactory;
128 import net.pterodactylus.util.logging.Logging;
129 import net.pterodactylus.util.notify.Notification;
130 import net.pterodactylus.util.notify.NotificationManager;
131 import net.pterodactylus.util.notify.TemplateNotification;
132 import net.pterodactylus.util.template.ClassPathTemplateProvider;
133 import net.pterodactylus.util.template.CollectionSortFilter;
134 import net.pterodactylus.util.template.ContainsFilter;
135 import net.pterodactylus.util.template.DateFilter;
136 import net.pterodactylus.util.template.FormatFilter;
137 import net.pterodactylus.util.template.HtmlFilter;
138 import net.pterodactylus.util.template.MatchFilter;
139 import net.pterodactylus.util.template.ModFilter;
140 import net.pterodactylus.util.template.PaginationFilter;
141 import net.pterodactylus.util.template.ReflectionAccessor;
142 import net.pterodactylus.util.template.ReplaceFilter;
143 import net.pterodactylus.util.template.StoreFilter;
144 import net.pterodactylus.util.template.Template;
145 import net.pterodactylus.util.template.TemplateContextFactory;
146 import net.pterodactylus.util.template.TemplateParser;
147 import net.pterodactylus.util.template.TemplateProvider;
148 import net.pterodactylus.util.template.XmlFilter;
149 import net.pterodactylus.util.web.RedirectPage;
150 import net.pterodactylus.util.web.StaticPage;
151 import net.pterodactylus.util.web.TemplatePage;
153 import freenet.clients.http.SessionManager;
154 import freenet.clients.http.SessionManager.Session;
155 import freenet.clients.http.ToadletContainer;
156 import freenet.clients.http.ToadletContext;
157 import freenet.l10n.BaseL10n;
158 import freenet.support.api.HTTPRequest;
160 import com.google.common.collect.Collections2;
161 import com.google.common.collect.ImmutableSet;
162 import com.google.common.eventbus.Subscribe;
163 import com.google.inject.Inject;
166 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
167 * references to l10n helpers.
169 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
171 public class WebInterface {
174 private static final Logger logger = Logging.getLogger(WebInterface.class);
176 /** The notification manager. */
177 private final NotificationManager notificationManager = new NotificationManager();
179 /** The Sone plugin. */
180 private final SonePlugin sonePlugin;
182 /** The registered toadlets. */
183 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
185 /** The form password. */
186 private final String formPassword;
188 /** The template context factory. */
189 private final TemplateContextFactory templateContextFactory;
191 /** The Sone text parser. */
192 private final SoneTextParser soneTextParser;
194 /** The parser filter. */
195 private final ParserFilter parserFilter;
197 /** The “new Sone” notification. */
198 private final ListNotification<Sone> newSoneNotification;
200 /** The “new post” notification. */
201 private final ListNotification<Post> newPostNotification;
203 /** The “new reply” notification. */
204 private final ListNotification<PostReply> newReplyNotification;
206 /** The invisible “local post” notification. */
207 private final ListNotification<Post> localPostNotification;
209 /** The invisible “local reply” notification. */
210 private final ListNotification<PostReply> localReplyNotification;
212 /** The “you have been mentioned” notification. */
213 private final ListNotification<Post> mentionNotification;
215 /** Notifications for sone inserts. */
216 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
218 /** Sone locked notification ticker objects. */
219 private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
221 /** The “Sone locked” notification. */
222 private final ListNotification<Sone> lockedSonesNotification;
224 /** The “new version” notification. */
225 private final TemplateNotification newVersionNotification;
227 /** The “inserting images” notification. */
228 private final ListNotification<Image> insertingImagesNotification;
230 /** The “inserted images” notification. */
231 private final ListNotification<Image> insertedImagesNotification;
233 /** The “image insert failed” notification. */
234 private final ListNotification<Image> imageInsertFailedNotification;
236 /** Scheduled executor for time-based notifications. */
237 private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
240 * Creates a new web interface.
246 public WebInterface(SonePlugin sonePlugin) {
247 this.sonePlugin = sonePlugin;
248 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
249 soneTextParser = new SoneTextParser(getCore().getDatabase());
251 templateContextFactory = new TemplateContextFactory();
252 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
253 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
254 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
255 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
256 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
257 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
258 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
259 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
260 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
261 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
262 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
263 templateContextFactory.addFilter("date", new DateFilter());
264 templateContextFactory.addFilter("html", new HtmlFilter());
265 templateContextFactory.addFilter("replace", new ReplaceFilter());
266 templateContextFactory.addFilter("store", new StoreFilter());
267 templateContextFactory.addFilter("l10n", new L10nFilter(this));
268 templateContextFactory.addFilter("substring", new SubstringFilter());
269 templateContextFactory.addFilter("xml", new XmlFilter());
270 templateContextFactory.addFilter("change", new RequestChangeFilter());
271 templateContextFactory.addFilter("match", new MatchFilter());
272 templateContextFactory.addFilter("css", new CssClassNameFilter());
273 templateContextFactory.addFilter("js", new JavascriptFilter());
274 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
275 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
276 templateContextFactory.addFilter("format", new FormatFilter());
277 templateContextFactory.addFilter("sort", new CollectionSortFilter());
278 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
279 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
280 templateContextFactory.addFilter("in", new ContainsFilter());
281 templateContextFactory.addFilter("unique", new UniqueElementFilter());
282 templateContextFactory.addFilter("mod", new ModFilter());
283 templateContextFactory.addFilter("paginate", new PaginationFilter());
284 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
285 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
286 templateContextFactory.addTemplateObject("webInterface", this);
287 templateContextFactory.addTemplateObject("formPassword", formPassword);
289 /* create notifications. */
290 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
291 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
293 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
294 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
296 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
297 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
299 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
300 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
302 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
303 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
305 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
306 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
308 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
309 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
311 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
312 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
314 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
315 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
317 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
318 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
320 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
321 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
329 * Returns the Sone core used by the Sone plugin.
331 * @return The Sone core
333 public Core getCore() {
334 return sonePlugin.core();
338 * Returns the template context factory of the web interface.
340 * @return The template context factory
342 public TemplateContextFactory getTemplateContextFactory() {
343 return templateContextFactory;
347 * Returns the current session, creating a new session if there is no
350 * @param toadletContenxt
351 * The toadlet context
352 * @return The current session, or {@code null} if there is no current
355 public Session getCurrentSession(ToadletContext toadletContenxt) {
356 return getCurrentSession(toadletContenxt, true);
360 * Returns the current session, creating a new session if there is no
361 * current session and {@code create} is {@code true}.
363 * @param toadletContenxt
364 * The toadlet context
366 * {@code true} to create a new session if there is no current
367 * session, {@code false} otherwise
368 * @return The current session, or {@code null} if there is no current
371 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
372 Session session = getSessionManager().useSession(toadletContenxt);
373 if (create && (session == null)) {
374 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
380 * Returns the currently logged in Sone.
382 * @param toadletContext
383 * The toadlet context
384 * @return The currently logged in Sone, or {@code null} if no Sone is
385 * currently logged in
387 public Sone getCurrentSone(ToadletContext toadletContext) {
388 return getCurrentSone(toadletContext, true);
392 * Returns the currently logged in Sone.
394 * @param toadletContext
395 * The toadlet context
397 * {@code true} to create a new session if no session exists,
398 * {@code false} to not create a new session
399 * @return The currently logged in Sone, or {@code null} if no Sone is
400 * currently logged in
402 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
403 Collection<Sone> localSones = getCore().getLocalSones();
404 if (localSones.size() == 1) {
405 return localSones.iterator().next();
407 return getCurrentSone(getCurrentSession(toadletContext, create));
411 * Returns the currently logged in Sone.
415 * @return The currently logged in Sone, or {@code null} if no Sone is
416 * currently logged in
418 public Sone getCurrentSone(Session session) {
419 if (session == null) {
422 String soneId = (String) session.getAttribute("Sone.CurrentSone");
423 if (soneId == null) {
426 return getCore().getLocalSone(soneId).orNull();
430 * Sets the currently logged in Sone.
432 * @param toadletContext
433 * The toadlet context
435 * The Sone to set as currently logged in
437 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
438 Session session = getCurrentSession(toadletContext);
440 session.removeAttribute("Sone.CurrentSone");
442 session.setAttribute("Sone.CurrentSone", sone.getId());
447 * Returns the notification manager.
449 * @return The notification manager
451 public NotificationManager getNotifications() {
452 return notificationManager;
456 * Returns the l10n helper of the node.
458 * @return The node’s l10n helper
460 public BaseL10n getL10n() {
461 return sonePlugin.l10n().getBase();
465 * Returns the session manager of the node.
467 * @return The node’s session manager
469 public SessionManager getSessionManager() {
470 return sonePlugin.pluginRespirator().getSessionManager("Sone");
474 * Returns the node’s form password.
476 * @return The form password
478 public String getFormPassword() {
483 * Returns the posts that have been announced as new in the
484 * {@link #newPostNotification}.
486 * @return The new posts
488 public Set<Post> getNewPosts() {
489 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
493 * Returns the replies that have been announced as new in the
494 * {@link #newReplyNotification}.
496 * @return The new replies
498 public Set<PostReply> getNewReplies() {
499 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
503 * Sets whether the current start of the plugin is the first start. It is
504 * considered a first start if the configuration file does not exist.
507 * {@code true} if no configuration file existed when Sone was
508 * loaded, {@code false} otherwise
510 public void setFirstStart(boolean firstStart) {
512 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
513 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
514 notificationManager.addNotification(firstStartNotification);
519 * Sets whether Sone was started with a fresh configuration file.
522 * {@code true} if Sone was started with a fresh configuration,
523 * {@code false} if the existing configuration could be read
525 public void setNewConfig(boolean newConfig) {
526 if (newConfig && !hasFirstStartNotification()) {
527 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
528 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
529 notificationManager.addNotification(configNotReadNotification);
538 * Returns whether the first start notification is currently displayed.
540 * @return {@code true} if the first-start notification is currently
541 * displayed, {@code false} otherwise
543 private boolean hasFirstStartNotification() {
544 return notificationManager.getNotification("first-start-notification") != null;
552 * Starts the web interface and registers all toadlets.
554 public void start() {
557 /* notification templates. */
558 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
560 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
561 notificationManager.addNotification(startupNotification);
563 ticker.schedule(new Runnable() {
567 startupNotification.dismiss();
569 }, 2, TimeUnit.MINUTES);
571 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
572 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
573 ticker.scheduleAtFixedRate(new Runnable() {
576 @SuppressWarnings("synthetic-access")
578 if (getCore().getIdentityManager().isConnected()) {
579 wotMissingNotification.dismiss();
581 notificationManager.addNotification(wotMissingNotification);
585 }, 15, 15, TimeUnit.SECONDS);
589 * Stops the web interface and unregisters all toadlets.
592 unregisterToadlets();
593 ticker.shutdownNow();
601 * Register all toadlets.
603 private void registerToadlets() {
604 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
605 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
606 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
607 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
608 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
609 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
610 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
611 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
612 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
613 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
614 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
615 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
616 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
617 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
618 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
619 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
620 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
621 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
622 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
623 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
624 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
625 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
626 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
627 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
628 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
629 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
630 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
631 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
632 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
633 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
635 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
715 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
716 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
717 for (PageToadlet toadlet : pageToadlets) {
718 String menuName = toadlet.getMenuName();
719 if (menuName != null) {
720 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
722 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
728 * Unregisters all toadlets.
730 private void unregisterToadlets() {
731 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
732 for (PageToadlet pageToadlet : pageToadlets) {
733 toadletContainer.unregister(pageToadlet);
735 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
739 * Creates a {@link Reader} from the {@link InputStream} for the resource
740 * with the given name.
742 * @param resourceName
743 * The name of the resource
744 * @return A {@link Reader} for the resource
746 private Reader createReader(String resourceName) {
748 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
749 } catch (UnsupportedEncodingException uee1) {
755 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
756 * {@link SonePart}s in the given text (after parsing it using
757 * {@link SoneTextParser}).
761 * @return All mentioned local Sones
763 private Collection<Sone> getMentionedSones(String text) {
764 /* we need no context to find mentioned Sones. */
765 Set<Sone> mentionedSones = new HashSet<Sone>();
767 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
768 if (part instanceof SonePart) {
769 mentionedSones.add(((SonePart) part).getSone());
772 } catch (IOException ioe1) {
773 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
775 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
779 * Returns the Sone insert notification for the given Sone. If no
780 * notification for the given Sone exists, a new notification is created and
784 * The Sone to get the insert notification for
785 * @return The Sone insert notification
787 private TemplateNotification getSoneInsertNotification(Sone sone) {
788 synchronized (soneInsertNotifications) {
789 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
790 if (templateNotification == null) {
791 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
792 templateNotification.set("insertSone", sone);
793 soneInsertNotifications.put(sone, templateNotification);
795 return templateNotification;
804 * Notifies the web interface that a new {@link Sone} was found.
806 * @param newSoneFoundEvent
810 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
811 newSoneNotification.add(newSoneFoundEvent.sone());
812 if (!hasFirstStartNotification()) {
813 notificationManager.addNotification(newSoneNotification);
818 * Notifies the web interface that a new {@link Post} was found.
820 * @param newPostFoundEvent
824 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
825 Post post = newPostFoundEvent.post();
826 boolean isLocal = post.getSone().isLocal();
828 localPostNotification.add(post);
830 newPostNotification.add(post);
832 if (!hasFirstStartNotification()) {
833 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
834 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
835 mentionNotification.add(post);
836 notificationManager.addNotification(mentionNotification);
839 getCore().markPostKnown(post);
844 * Notifies the web interface that a new {@link PostReply} was found.
846 * @param newPostReplyFoundEvent
850 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
851 PostReply reply = newPostReplyFoundEvent.postReply();
852 boolean isLocal = reply.getSone().isLocal();
854 localReplyNotification.add(reply);
856 newReplyNotification.add(reply);
858 if (!hasFirstStartNotification()) {
859 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
860 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && reply.getPost().isPresent() && (reply.getTime() <= System.currentTimeMillis())) {
861 mentionNotification.add(reply.getPost().get());
862 notificationManager.addNotification(mentionNotification);
865 reply.modify().setKnown().update(getCore().postReplyUpdated());
870 * Notifies the web interface that a {@link Sone} was marked as known.
872 * @param markSoneKnownEvent
876 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
877 newSoneNotification.remove(markSoneKnownEvent.sone());
881 * Notifies the web interface that a {@link Post} was marked as known.
883 * @param markPostKnownEvent
887 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
888 newPostNotification.remove(markPostKnownEvent.post());
889 localPostNotification.remove(markPostKnownEvent.post());
890 mentionNotification.remove(markPostKnownEvent.post());
894 * Notifies the web interface that a {@link PostReply} was marked as known.
896 * @param markPostReplyKnownEvent
900 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
901 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
902 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
903 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost().get());
907 * Notifies the web interface that a {@link Sone} was removed.
909 * @param soneRemovedEvent
913 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
914 newSoneNotification.remove(soneRemovedEvent.sone());
918 * Notifies the web interface that a {@link Post} was removed.
920 * @param postRemovedEvent
924 public void postRemoved(PostRemovedEvent postRemovedEvent) {
925 newPostNotification.remove(postRemovedEvent.post());
926 localPostNotification.remove(postRemovedEvent.post());
927 mentionNotification.remove(postRemovedEvent.post());
931 * Notifies the web interface that a {@link PostReply} was removed.
933 * @param postReplyRemovedEvent
937 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
938 PostReply reply = postReplyRemovedEvent.postReply();
939 newReplyNotification.remove(reply);
940 localReplyNotification.remove(reply);
941 if (!getMentionedSones(reply.getText()).isEmpty() && reply.getPost().isPresent()) {
942 boolean isMentioned = false;
943 for (PostReply existingReply : reply.getPost().transform(Post.TO_REPLIES).get()) {
944 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
947 mentionNotification.remove(reply.getPost().get());
953 * Notifies the web interface that a Sone was locked.
955 * @param soneLockedEvent
959 public void soneLocked(SoneLockedEvent soneLockedEvent) {
960 final Sone sone = soneLockedEvent.sone();
961 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
964 @SuppressWarnings("synthetic-access")
966 lockedSonesNotification.add(sone);
967 notificationManager.addNotification(lockedSonesNotification);
969 }, 5, TimeUnit.MINUTES);
970 lockedSonesTickerObjects.put(sone, tickerObject);
974 * Notifies the web interface that a Sone was unlocked.
976 * @param soneUnlockedEvent
980 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
981 lockedSonesNotification.remove(soneUnlockedEvent.sone());
982 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
986 * Notifies the web interface that a {@link Sone} is being inserted.
988 * @param soneInsertingEvent
992 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
993 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
994 soneInsertNotification.set("soneStatus", "inserting");
995 if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
996 notificationManager.addNotification(soneInsertNotification);
1001 * Notifies the web interface that a {@link Sone} was inserted.
1003 * @param soneInsertedEvent
1007 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1008 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1009 soneInsertNotification.set("soneStatus", "inserted");
1010 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1011 if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1012 notificationManager.addNotification(soneInsertNotification);
1017 * Notifies the web interface that a {@link Sone} insert was aborted.
1019 * @param soneInsertAbortedEvent
1023 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1024 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1025 soneInsertNotification.set("soneStatus", "insert-aborted");
1026 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1027 if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1028 notificationManager.addNotification(soneInsertNotification);
1033 * Notifies the web interface that a new Sone version was found.
1035 * @param updateFoundEvent
1039 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1040 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1041 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1042 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1043 notificationManager.addNotification(newVersionNotification);
1047 * Notifies the web interface that an image insert was started
1049 * @param imageInsertStartedEvent
1053 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1054 insertingImagesNotification.add(imageInsertStartedEvent.image());
1055 notificationManager.addNotification(insertingImagesNotification);
1059 * Notifies the web interface that an {@link Image} insert was aborted.
1061 * @param imageInsertAbortedEvent
1065 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1066 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1070 * Notifies the web interface that an {@link Image} insert is finished.
1072 * @param imageInsertFinishedEvent
1076 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1077 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1078 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1079 notificationManager.addNotification(insertedImagesNotification);
1083 * Notifies the web interface that an {@link Image} insert has failed.
1085 * @param imageInsertFailedEvent
1089 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1090 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1091 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1092 notificationManager.addNotification(imageInsertFailedNotification);