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 static net.pterodactylus.util.template.TemplateParser.parse;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.io.UnsupportedEncodingException;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
36 import java.util.UUID;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.ScheduledExecutorService;
39 import java.util.concurrent.ScheduledFuture;
40 import java.util.concurrent.TimeUnit;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
44 import net.pterodactylus.sone.core.Core;
45 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
46 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
47 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
48 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
49 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
50 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
51 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
52 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
53 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
54 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
55 import net.pterodactylus.sone.core.event.PostRemovedEvent;
56 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
57 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
58 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
59 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
60 import net.pterodactylus.sone.core.event.SoneLockedEvent;
61 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
62 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
63 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
64 import net.pterodactylus.sone.data.Album;
65 import net.pterodactylus.sone.data.Image;
66 import net.pterodactylus.sone.data.Post;
67 import net.pterodactylus.sone.data.PostReply;
68 import net.pterodactylus.sone.data.Profile;
69 import net.pterodactylus.sone.data.Reply;
70 import net.pterodactylus.sone.data.Sone;
71 import net.pterodactylus.sone.freenet.L10nFilter;
72 import net.pterodactylus.sone.freenet.wot.Identity;
73 import net.pterodactylus.sone.freenet.wot.Trust;
74 import net.pterodactylus.sone.main.SonePlugin;
75 import net.pterodactylus.sone.notify.ListNotification;
76 import net.pterodactylus.sone.template.AlbumAccessor;
77 import net.pterodactylus.sone.template.CollectionAccessor;
78 import net.pterodactylus.sone.template.CssClassNameFilter;
79 import net.pterodactylus.sone.template.HttpRequestAccessor;
80 import net.pterodactylus.sone.template.IdentityAccessor;
81 import net.pterodactylus.sone.template.ImageAccessor;
82 import net.pterodactylus.sone.template.ImageLinkFilter;
83 import net.pterodactylus.sone.template.JavascriptFilter;
84 import net.pterodactylus.sone.template.ParserFilter;
85 import net.pterodactylus.sone.template.PostAccessor;
86 import net.pterodactylus.sone.template.ProfileAccessor;
87 import net.pterodactylus.sone.template.ReplyAccessor;
88 import net.pterodactylus.sone.template.ReplyGroupFilter;
89 import net.pterodactylus.sone.template.RequestChangeFilter;
90 import net.pterodactylus.sone.template.SoneAccessor;
91 import net.pterodactylus.sone.template.SubstringFilter;
92 import net.pterodactylus.sone.template.TrustAccessor;
93 import net.pterodactylus.sone.template.UniqueElementFilter;
94 import net.pterodactylus.sone.template.UnknownDateFilter;
95 import net.pterodactylus.sone.text.Part;
96 import net.pterodactylus.sone.text.SonePart;
97 import net.pterodactylus.sone.text.SoneTextParser;
98 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
99 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
100 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
101 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
102 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
104 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
105 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
106 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
107 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
108 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
109 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
111 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
115 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
116 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
117 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
118 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
119 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
120 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
121 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
122 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
123 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
125 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
126 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
127 import net.pterodactylus.sone.web.page.FreenetRequest;
128 import net.pterodactylus.sone.web.page.PageToadlet;
129 import net.pterodactylus.sone.web.page.PageToadletFactory;
130 import net.pterodactylus.util.io.Closer;
131 import net.pterodactylus.util.logging.Logging;
132 import net.pterodactylus.util.notify.Notification;
133 import net.pterodactylus.util.notify.NotificationManager;
134 import net.pterodactylus.util.notify.TemplateNotification;
135 import net.pterodactylus.util.template.ClassPathTemplateProvider;
136 import net.pterodactylus.util.template.CollectionSortFilter;
137 import net.pterodactylus.util.template.ContainsFilter;
138 import net.pterodactylus.util.template.DateFilter;
139 import net.pterodactylus.util.template.FormatFilter;
140 import net.pterodactylus.util.template.HtmlFilter;
141 import net.pterodactylus.util.template.MatchFilter;
142 import net.pterodactylus.util.template.ModFilter;
143 import net.pterodactylus.util.template.PaginationFilter;
144 import net.pterodactylus.util.template.ReflectionAccessor;
145 import net.pterodactylus.util.template.ReplaceFilter;
146 import net.pterodactylus.util.template.StoreFilter;
147 import net.pterodactylus.util.template.Template;
148 import net.pterodactylus.util.template.TemplateContextFactory;
149 import net.pterodactylus.util.template.TemplateProvider;
150 import net.pterodactylus.util.template.XmlFilter;
151 import net.pterodactylus.util.web.RedirectPage;
152 import net.pterodactylus.util.web.StaticPage;
153 import net.pterodactylus.util.web.TemplatePage;
155 import com.google.common.collect.Collections2;
156 import com.google.common.collect.ImmutableSet;
157 import com.google.common.eventbus.Subscribe;
158 import com.google.inject.Inject;
160 import freenet.clients.http.SessionManager;
161 import freenet.clients.http.SessionManager.Session;
162 import freenet.clients.http.ToadletContainer;
163 import freenet.clients.http.ToadletContext;
164 import freenet.l10n.BaseL10n;
165 import freenet.support.api.HTTPRequest;
168 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
169 * references to l10n helpers.
171 * @author <a href="mailto:bombe@pterodactylus.net">David âBombeâ Roden</a>
173 public class WebInterface {
176 private static final Logger logger = Logging.getLogger(WebInterface.class);
178 /** The notification manager. */
179 private final NotificationManager notificationManager = new NotificationManager();
181 /** The Sone plugin. */
182 private final SonePlugin sonePlugin;
184 /** The registered toadlets. */
185 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
187 /** The form password. */
188 private final String formPassword;
190 /** The template context factory. */
191 private final TemplateContextFactory templateContextFactory;
193 /** The Sone text parser. */
194 private final SoneTextParser soneTextParser;
196 /** The parser filter. */
197 private final ParserFilter parserFilter;
199 /** The ânew Soneâ notification. */
200 private final ListNotification<Sone> newSoneNotification;
202 /** The ânew postâ notification. */
203 private final ListNotification<Post> newPostNotification;
205 /** The ânew replyâ notification. */
206 private final ListNotification<PostReply> newReplyNotification;
208 /** The invisible âlocal postâ notification. */
209 private final ListNotification<Post> localPostNotification;
211 /** The invisible âlocal replyâ notification. */
212 private final ListNotification<PostReply> localReplyNotification;
214 /** The âyou have been mentionedâ notification. */
215 private final ListNotification<Post> mentionNotification;
217 /** Notifications for sone inserts. */
218 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
220 /** Sone locked notification ticker objects. */
221 private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
223 /** The âSone lockedâ notification. */
224 private final ListNotification<Sone> lockedSonesNotification;
226 /** The ânew versionâ notification. */
227 private final TemplateNotification newVersionNotification;
229 /** The âinserting imagesâ notification. */
230 private final ListNotification<Image> insertingImagesNotification;
232 /** The âinserted imagesâ notification. */
233 private final ListNotification<Image> insertedImagesNotification;
235 /** The âimage insert failedâ notification. */
236 private final ListNotification<Image> imageInsertFailedNotification;
238 /** Scheduled executor for time-based notifications. */
239 private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
242 * Creates a new web interface.
248 public WebInterface(SonePlugin sonePlugin) {
249 this.sonePlugin = sonePlugin;
250 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
251 soneTextParser = new SoneTextParser(getCore(), getCore());
253 templateContextFactory = new TemplateContextFactory();
254 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
255 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
256 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
257 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
258 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
259 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
260 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
261 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
262 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
263 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
264 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
265 templateContextFactory.addFilter("date", new DateFilter());
266 templateContextFactory.addFilter("html", new HtmlFilter());
267 templateContextFactory.addFilter("replace", new ReplaceFilter());
268 templateContextFactory.addFilter("store", new StoreFilter());
269 templateContextFactory.addFilter("l10n", new L10nFilter(this));
270 templateContextFactory.addFilter("substring", new SubstringFilter());
271 templateContextFactory.addFilter("xml", new XmlFilter());
272 templateContextFactory.addFilter("change", new RequestChangeFilter());
273 templateContextFactory.addFilter("match", new MatchFilter());
274 templateContextFactory.addFilter("css", new CssClassNameFilter());
275 templateContextFactory.addFilter("js", new JavascriptFilter());
276 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
277 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
278 templateContextFactory.addFilter("format", new FormatFilter());
279 templateContextFactory.addFilter("sort", new CollectionSortFilter());
280 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
281 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
282 templateContextFactory.addFilter("in", new ContainsFilter());
283 templateContextFactory.addFilter("unique", new UniqueElementFilter());
284 templateContextFactory.addFilter("mod", new ModFilter());
285 templateContextFactory.addFilter("paginate", new PaginationFilter());
286 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
287 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
288 templateContextFactory.addTemplateObject("webInterface", this);
289 templateContextFactory.addTemplateObject("formPassword", formPassword);
291 /* create notifications. */
292 Template newSoneNotificationTemplate = parseTemplate("/templates/notify/newSoneNotification.html");
293 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
295 Template newPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html");
296 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
298 Template localPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html");
299 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
301 Template newReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html");
302 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
304 Template localReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html");
305 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
307 Template mentionNotificationTemplate = parseTemplate("/templates/notify/mentionNotification.html");
308 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
310 Template lockedSonesTemplate = parseTemplate("/templates/notify/lockedSonesNotification.html");
311 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
313 Template newVersionTemplate = parseTemplate("/templates/notify/newVersionNotification.html");
314 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
316 Template insertingImagesTemplate = parseTemplate("/templates/notify/inserting-images-notification.html");
317 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
319 Template insertedImagesTemplate = parseTemplate("/templates/notify/inserted-images-notification.html");
320 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
322 Template imageInsertFailedTemplate = parseTemplate("/templates/notify/image-insert-failed-notification.html");
323 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
326 private Template parseTemplate(String resourceName) {
327 Reader reader = createReader(resourceName);
329 return parse(reader);
331 Closer.close(reader);
340 * Returns the Sone core used by the Sone plugin.
342 * @return The Sone core
344 public Core getCore() {
345 return sonePlugin.core();
349 * Returns the template context factory of the web interface.
351 * @return The template context factory
353 public TemplateContextFactory getTemplateContextFactory() {
354 return templateContextFactory;
358 * Returns the current session, creating a new session if there is no
361 * @param toadletContenxt
362 * The toadlet context
363 * @return The current session, or {@code null} if there is no current
366 public Session getCurrentSession(ToadletContext toadletContenxt) {
367 return getCurrentSession(toadletContenxt, true);
371 * Returns the current session, creating a new session if there is no
372 * current session and {@code create} is {@code true}.
374 * @param toadletContenxt
375 * The toadlet context
377 * {@code true} to create a new session if there is no current
378 * session, {@code false} otherwise
379 * @return The current session, or {@code null} if there is no current
382 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
383 Session session = getSessionManager().useSession(toadletContenxt);
384 if (create && (session == null)) {
385 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
391 * Returns the currently logged in Sone.
393 * @param toadletContext
394 * The toadlet context
395 * @return The currently logged in Sone, or {@code null} if no Sone is
396 * currently logged in
398 public Sone getCurrentSone(ToadletContext toadletContext) {
399 return getCurrentSone(toadletContext, true);
403 * Returns the currently logged in Sone.
405 * @param toadletContext
406 * The toadlet context
408 * {@code true} to create a new session if no session exists,
409 * {@code false} to not create a new session
410 * @return The currently logged in Sone, or {@code null} if no Sone is
411 * currently logged in
413 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
414 Collection<Sone> localSones = getCore().getLocalSones();
415 if (localSones.size() == 1) {
416 return localSones.iterator().next();
418 return getCurrentSone(getCurrentSession(toadletContext, create));
422 * Returns the currently logged in Sone.
426 * @return The currently logged in Sone, or {@code null} if no Sone is
427 * currently logged in
429 public Sone getCurrentSone(Session session) {
430 if (session == null) {
433 String soneId = (String) session.getAttribute("Sone.CurrentSone");
434 if (soneId == null) {
437 return getCore().getLocalSone(soneId, false);
441 * Sets the currently logged in Sone.
443 * @param toadletContext
444 * The toadlet context
446 * The Sone to set as currently logged in
448 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
449 Session session = getCurrentSession(toadletContext);
451 session.removeAttribute("Sone.CurrentSone");
453 session.setAttribute("Sone.CurrentSone", sone.getId());
458 * Returns the notification manager.
460 * @return The notification manager
462 public NotificationManager getNotifications() {
463 return notificationManager;
467 * Returns the l10n helper of the node.
469 * @return The nodeâs l10n helper
471 public BaseL10n getL10n() {
472 return sonePlugin.l10n().getBase();
476 * Returns the session manager of the node.
478 * @return The nodeâs session manager
480 public SessionManager getSessionManager() {
481 return sonePlugin.pluginRespirator().getSessionManager("Sone");
485 * Returns the nodeâs form password.
487 * @return The form password
489 public String getFormPassword() {
494 * Returns the posts that have been announced as new in the
495 * {@link #newPostNotification}.
497 * @return The new posts
499 public Set<Post> getNewPosts() {
500 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
504 * Returns the replies that have been announced as new in the
505 * {@link #newReplyNotification}.
507 * @return The new replies
509 public Set<PostReply> getNewReplies() {
510 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
514 * Sets whether the current start of the plugin is the first start. It is
515 * considered a first start if the configuration file does not exist.
518 * {@code true} if no configuration file existed when Sone was
519 * loaded, {@code false} otherwise
521 public void setFirstStart(boolean firstStart) {
523 Template firstStartNotificationTemplate = parseTemplate("/templates/notify/firstStartNotification.html");
524 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
525 notificationManager.addNotification(firstStartNotification);
530 * Sets whether Sone was started with a fresh configuration file.
533 * {@code true} if Sone was started with a fresh configuration,
534 * {@code false} if the existing configuration could be read
536 public void setNewConfig(boolean newConfig) {
537 if (newConfig && !hasFirstStartNotification()) {
538 Template configNotReadNotificationTemplate = parseTemplate("/templates/notify/configNotReadNotification.html");
539 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
540 notificationManager.addNotification(configNotReadNotification);
549 * Returns whether the first start notification is currently displayed.
551 * @return {@code true} if the first-start notification is currently
552 * displayed, {@code false} otherwise
554 private boolean hasFirstStartNotification() {
555 return notificationManager.getNotification("first-start-notification") != null;
563 * Starts the web interface and registers all toadlets.
565 public void start() {
568 /* notification templates. */
569 Template startupNotificationTemplate = parseTemplate("/templates/notify/startupNotification.html");
571 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
572 notificationManager.addNotification(startupNotification);
574 ticker.schedule(new Runnable() {
578 startupNotification.dismiss();
580 }, 2, TimeUnit.MINUTES);
582 Template wotMissingNotificationTemplate = parseTemplate("/templates/notify/wotMissingNotification.html");
583 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
584 ticker.scheduleAtFixedRate(new Runnable() {
587 @SuppressWarnings("synthetic-access")
589 if (getCore().getIdentityManager().isConnected()) {
590 wotMissingNotification.dismiss();
592 notificationManager.addNotification(wotMissingNotification);
596 }, 15, 15, TimeUnit.SECONDS);
600 * Stops the web interface and unregisters all toadlets.
603 unregisterToadlets();
604 ticker.shutdownNow();
612 * Register all toadlets.
614 private void registerToadlets() {
615 Template emptyTemplate = parse(new StringReader(""));
616 Template loginTemplate = parseTemplate("/templates/login.html");
617 Template indexTemplate = parseTemplate("/templates/index.html");
618 Template newTemplate = parseTemplate("/templates/new.html");
619 Template knownSonesTemplate = parseTemplate("/templates/knownSones.html");
620 Template createSoneTemplate = parseTemplate("/templates/createSone.html");
621 Template createPostTemplate = parseTemplate("/templates/createPost.html");
622 Template createReplyTemplate = parseTemplate("/templates/createReply.html");
623 Template bookmarksTemplate = parseTemplate("/templates/bookmarks.html");
624 Template searchTemplate = parseTemplate("/templates/search.html");
625 Template editProfileTemplate = parseTemplate("/templates/editProfile.html");
626 Template editProfileFieldTemplate = parseTemplate("/templates/editProfileField.html");
627 Template deleteProfileFieldTemplate = parseTemplate("/templates/deleteProfileField.html");
628 Template viewSoneTemplate = parseTemplate("/templates/viewSone.html");
629 Template viewPostTemplate = parseTemplate("/templates/viewPost.html");
630 Template deletePostTemplate = parseTemplate("/templates/deletePost.html");
631 Template deleteReplyTemplate = parseTemplate("/templates/deleteReply.html");
632 Template deleteSoneTemplate = parseTemplate("/templates/deleteSone.html");
633 Template imageBrowserTemplate = parseTemplate("/templates/imageBrowser.html");
634 Template createAlbumTemplate = parseTemplate("/templates/createAlbum.html");
635 Template deleteAlbumTemplate = parseTemplate("/templates/deleteAlbum.html");
636 Template deleteImageTemplate = parseTemplate("/templates/deleteImage.html");
637 Template noPermissionTemplate = parseTemplate("/templates/noPermission.html");
638 Template optionsTemplate = parseTemplate("/templates/options.html");
639 Template rescueTemplate = parseTemplate("/templates/rescue.html");
640 Template aboutTemplate = parseTemplate("/templates/about.html");
641 Template invalidTemplate = parseTemplate("/templates/invalid.html");
642 Template postTemplate = parseTemplate("/templates/include/viewPost.html");
643 Template replyTemplate = parseTemplate("/templates/include/viewReply.html");
644 Template openSearchTemplate = parseTemplate("/templates/xml/OpenSearch.xml");
646 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
716 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
717 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
718 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
719 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
720 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
721 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
722 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
723 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
724 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
726 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
727 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
728 for (PageToadlet toadlet : pageToadlets) {
729 String menuName = toadlet.getMenuName();
730 if (menuName != null) {
731 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
733 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
739 * Unregisters all toadlets.
741 private void unregisterToadlets() {
742 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
743 for (PageToadlet pageToadlet : pageToadlets) {
744 toadletContainer.unregister(pageToadlet);
746 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
750 * Creates a {@link Reader} from the {@link InputStream} for the resource
751 * with the given name.
753 * @param resourceName
754 * The name of the resource
755 * @return A {@link Reader} for the resource
757 private Reader createReader(String resourceName) {
759 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
760 } catch (UnsupportedEncodingException uee1) {
766 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
767 * {@link SonePart}s in the given text (after parsing it using
768 * {@link SoneTextParser}).
772 * @return All mentioned local Sones
774 private Collection<Sone> getMentionedSones(String text) {
775 /* we need no context to find mentioned Sones. */
776 Set<Sone> mentionedSones = new HashSet<Sone>();
778 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
779 if (part instanceof SonePart) {
780 mentionedSones.add(((SonePart) part).getSone());
783 } catch (IOException ioe1) {
784 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
786 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
790 * Returns the Sone insert notification for the given Sone. If no
791 * notification for the given Sone exists, a new notification is created and
795 * The Sone to get the insert notification for
796 * @return The Sone insert notification
798 private TemplateNotification getSoneInsertNotification(Sone sone) {
799 synchronized (soneInsertNotifications) {
800 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
801 if (templateNotification == null) {
802 templateNotification = new TemplateNotification(parseTemplate("/templates/notify/soneInsertNotification.html"));
803 templateNotification.set("insertSone", sone);
804 soneInsertNotifications.put(sone, templateNotification);
806 return templateNotification;
810 private boolean localSoneMentionedInNewPostOrReply(Post post) {
811 if (!post.getSone().isLocal()) {
812 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
816 for (PostReply postReply : getCore().getReplies(post.getId())) {
817 if (postReply.getSone().isLocal()) {
820 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
832 * Notifies the web interface that a new {@link Sone} was found.
834 * @param newSoneFoundEvent
838 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
839 newSoneNotification.add(newSoneFoundEvent.sone());
840 if (!hasFirstStartNotification()) {
841 notificationManager.addNotification(newSoneNotification);
846 * Notifies the web interface that a new {@link Post} was found.
848 * @param newPostFoundEvent
852 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
853 Post post = newPostFoundEvent.post();
854 boolean isLocal = post.getSone().isLocal();
856 localPostNotification.add(post);
858 newPostNotification.add(post);
860 if (!hasFirstStartNotification()) {
861 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
862 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
863 mentionNotification.add(post);
864 notificationManager.addNotification(mentionNotification);
867 getCore().markPostKnown(post);
872 * Notifies the web interface that a new {@link PostReply} was found.
874 * @param newPostReplyFoundEvent
878 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
879 PostReply reply = newPostReplyFoundEvent.postReply();
880 boolean isLocal = reply.getSone().isLocal();
882 localReplyNotification.add(reply);
884 newReplyNotification.add(reply);
886 if (!hasFirstStartNotification()) {
887 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
888 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
889 mentionNotification.add(reply.getPost().get());
890 notificationManager.addNotification(mentionNotification);
893 getCore().markReplyKnown(reply);
898 * Notifies the web interface that a {@link Sone} was marked as known.
900 * @param markSoneKnownEvent
904 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
905 newSoneNotification.remove(markSoneKnownEvent.sone());
909 * Notifies the web interface that a {@link Post} was marked as known.
911 * @param markPostKnownEvent
915 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
916 newPostNotification.remove(markPostKnownEvent.post());
917 localPostNotification.remove(markPostKnownEvent.post());
918 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
919 mentionNotification.remove(markPostKnownEvent.post());
924 * Notifies the web interface that a {@link PostReply} was marked as known.
926 * @param markPostReplyKnownEvent
930 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
931 PostReply postReply = markPostReplyKnownEvent.postReply();
932 newReplyNotification.remove(postReply);
933 localReplyNotification.remove(postReply);
934 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
935 mentionNotification.remove(postReply.getPost().get());
940 * Notifies the web interface that a {@link Sone} was removed.
942 * @param soneRemovedEvent
946 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
947 newSoneNotification.remove(soneRemovedEvent.sone());
951 * Notifies the web interface that a {@link Post} was removed.
953 * @param postRemovedEvent
957 public void postRemoved(PostRemovedEvent postRemovedEvent) {
958 newPostNotification.remove(postRemovedEvent.post());
959 localPostNotification.remove(postRemovedEvent.post());
960 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
961 mentionNotification.remove(postRemovedEvent.post());
966 * Notifies the web interface that a {@link PostReply} was removed.
968 * @param postReplyRemovedEvent
972 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
973 PostReply reply = postReplyRemovedEvent.postReply();
974 newReplyNotification.remove(reply);
975 localReplyNotification.remove(reply);
976 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
977 mentionNotification.remove(reply.getPost().get());
982 * Notifies the web interface that a Sone was locked.
984 * @param soneLockedEvent
988 public void soneLocked(SoneLockedEvent soneLockedEvent) {
989 final Sone sone = soneLockedEvent.sone();
990 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
993 @SuppressWarnings("synthetic-access")
995 lockedSonesNotification.add(sone);
996 notificationManager.addNotification(lockedSonesNotification);
998 }, 5, TimeUnit.MINUTES);
999 lockedSonesTickerObjects.put(sone, tickerObject);
1003 * Notifies the web interface that a Sone was unlocked.
1005 * @param soneUnlockedEvent
1009 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
1010 lockedSonesNotification.remove(soneUnlockedEvent.sone());
1011 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
1015 * Notifies the web interface that a {@link Sone} is being inserted.
1017 * @param soneInsertingEvent
1021 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1022 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1023 soneInsertNotification.set("soneStatus", "inserting");
1024 if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1025 notificationManager.addNotification(soneInsertNotification);
1030 * Notifies the web interface that a {@link Sone} was inserted.
1032 * @param soneInsertedEvent
1036 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1037 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1038 soneInsertNotification.set("soneStatus", "inserted");
1039 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1040 if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1041 notificationManager.addNotification(soneInsertNotification);
1046 * Notifies the web interface that a {@link Sone} insert was aborted.
1048 * @param soneInsertAbortedEvent
1052 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1053 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1054 soneInsertNotification.set("soneStatus", "insert-aborted");
1055 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1056 if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1057 notificationManager.addNotification(soneInsertNotification);
1062 * Notifies the web interface that a new Sone version was found.
1064 * @param updateFoundEvent
1068 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1069 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1070 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1071 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1072 notificationManager.addNotification(newVersionNotification);
1076 * Notifies the web interface that an image insert was started
1078 * @param imageInsertStartedEvent
1082 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1083 insertingImagesNotification.add(imageInsertStartedEvent.image());
1084 notificationManager.addNotification(insertingImagesNotification);
1088 * Notifies the web interface that an {@link Image} insert was aborted.
1090 * @param imageInsertAbortedEvent
1094 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1095 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1099 * Notifies the web interface that an {@link Image} insert is finished.
1101 * @param imageInsertFinishedEvent
1105 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1106 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1107 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1108 notificationManager.addNotification(insertedImagesNotification);
1112 * Notifies the web interface that an {@link Image} insert has failed.
1114 * @param imageInsertFailedEvent
1118 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1119 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1120 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1121 notificationManager.addNotification(imageInsertFailedNotification);