2 * Sone - WebInterface.java - Copyright © 2010–2015 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 java.util.logging.Logger.getLogger;
21 import static net.pterodactylus.util.template.TemplateParser.parse;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.UUID;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.ScheduledFuture;
37 import java.util.concurrent.TimeUnit;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
41 import net.pterodactylus.sone.core.Core;
42 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
43 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
46 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
47 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
48 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
49 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
50 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
51 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
52 import net.pterodactylus.sone.core.event.PostRemovedEvent;
53 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
54 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
55 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
57 import net.pterodactylus.sone.core.event.SoneLockedEvent;
58 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
59 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
60 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
61 import net.pterodactylus.sone.data.Album;
62 import net.pterodactylus.sone.data.Image;
63 import net.pterodactylus.sone.data.Post;
64 import net.pterodactylus.sone.data.PostReply;
65 import net.pterodactylus.sone.data.Profile;
66 import net.pterodactylus.sone.data.Reply;
67 import net.pterodactylus.sone.data.Sone;
68 import net.pterodactylus.sone.freenet.L10nFilter;
69 import net.pterodactylus.sone.freenet.wot.Identity;
70 import net.pterodactylus.sone.freenet.wot.Trust;
71 import net.pterodactylus.sone.main.Loaders;
72 import net.pterodactylus.sone.main.ReparseFilter;
73 import net.pterodactylus.sone.main.SonePlugin;
74 import net.pterodactylus.sone.notify.ListNotification;
75 import net.pterodactylus.sone.template.AlbumAccessor;
76 import net.pterodactylus.sone.template.CollectionAccessor;
77 import net.pterodactylus.sone.template.CssClassNameFilter;
78 import net.pterodactylus.sone.template.HttpRequestAccessor;
79 import net.pterodactylus.sone.template.IdentityAccessor;
80 import net.pterodactylus.sone.template.ImageAccessor;
81 import net.pterodactylus.sone.template.ImageLinkFilter;
82 import net.pterodactylus.sone.template.JavascriptFilter;
83 import net.pterodactylus.sone.template.ParserFilter;
84 import net.pterodactylus.sone.template.PostAccessor;
85 import net.pterodactylus.sone.template.ProfileAccessor;
86 import net.pterodactylus.sone.template.ReplyAccessor;
87 import net.pterodactylus.sone.template.ReplyGroupFilter;
88 import net.pterodactylus.sone.template.RequestChangeFilter;
89 import net.pterodactylus.sone.template.SoneAccessor;
90 import net.pterodactylus.sone.template.SubstringFilter;
91 import net.pterodactylus.sone.template.TrustAccessor;
92 import net.pterodactylus.sone.template.UniqueElementFilter;
93 import net.pterodactylus.sone.template.UnknownDateFilter;
94 import net.pterodactylus.sone.text.Part;
95 import net.pterodactylus.sone.text.SonePart;
96 import net.pterodactylus.sone.text.SoneTextParser;
97 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
98 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
99 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
100 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
101 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
102 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
104 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
105 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
106 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
107 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
108 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
109 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
111 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
115 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
116 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
117 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
118 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
119 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
120 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
121 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
122 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
123 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
125 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
126 import net.pterodactylus.sone.web.page.FreenetRequest;
127 import net.pterodactylus.sone.web.page.PageToadlet;
128 import net.pterodactylus.sone.web.page.PageToadletFactory;
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.CollectionSortFilter;
133 import net.pterodactylus.util.template.ContainsFilter;
134 import net.pterodactylus.util.template.DateFilter;
135 import net.pterodactylus.util.template.FormatFilter;
136 import net.pterodactylus.util.template.HtmlFilter;
137 import net.pterodactylus.util.template.MatchFilter;
138 import net.pterodactylus.util.template.ModFilter;
139 import net.pterodactylus.util.template.PaginationFilter;
140 import net.pterodactylus.util.template.ReflectionAccessor;
141 import net.pterodactylus.util.template.ReplaceFilter;
142 import net.pterodactylus.util.template.StoreFilter;
143 import net.pterodactylus.util.template.Template;
144 import net.pterodactylus.util.template.TemplateContextFactory;
145 import net.pterodactylus.util.template.TemplateProvider;
146 import net.pterodactylus.util.template.XmlFilter;
147 import net.pterodactylus.util.web.RedirectPage;
148 import net.pterodactylus.util.web.TemplatePage;
150 import com.google.common.collect.Collections2;
151 import com.google.common.collect.ImmutableSet;
152 import com.google.common.eventbus.Subscribe;
153 import com.google.inject.Inject;
155 import freenet.clients.http.SessionManager;
156 import freenet.clients.http.SessionManager.Session;
157 import freenet.clients.http.ToadletContainer;
158 import freenet.clients.http.ToadletContext;
159 import freenet.l10n.BaseL10n;
160 import freenet.support.api.HTTPRequest;
163 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
164 * references to l10n helpers.
166 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
168 public class WebInterface {
171 private static final Logger logger = getLogger(WebInterface.class.getName());
173 /** The loaders for templates, pages, and classpath providers. */
174 private final Loaders loaders;
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, Loaders loaders) {
247 this.sonePlugin = sonePlugin;
248 this.loaders = loaders;
249 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
250 soneTextParser = new SoneTextParser(getCore(), getCore());
252 templateContextFactory = new TemplateContextFactory();
253 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
254 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
255 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
256 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
257 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
258 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
259 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
260 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
261 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
262 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
263 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
264 templateContextFactory.addFilter("date", new DateFilter());
265 templateContextFactory.addFilter("html", new HtmlFilter());
266 templateContextFactory.addFilter("replace", new ReplaceFilter());
267 templateContextFactory.addFilter("store", new StoreFilter());
268 templateContextFactory.addFilter("l10n", new L10nFilter(this));
269 templateContextFactory.addFilter("substring", new SubstringFilter());
270 templateContextFactory.addFilter("xml", new XmlFilter());
271 templateContextFactory.addFilter("change", new RequestChangeFilter());
272 templateContextFactory.addFilter("match", new MatchFilter());
273 templateContextFactory.addFilter("css", new CssClassNameFilter());
274 templateContextFactory.addFilter("js", new JavascriptFilter());
275 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
276 templateContextFactory.addFilter("reparse", new ReparseFilter());
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(loaders.getTemplateProvider());
288 templateContextFactory.addTemplateObject("webInterface", this);
289 templateContextFactory.addTemplateObject("formPassword", formPassword);
291 /* create notifications. */
292 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
293 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
295 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
296 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
298 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
299 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
301 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
302 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
304 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
305 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
307 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
308 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
310 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
311 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
313 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
314 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
316 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
317 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
319 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
320 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
322 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
323 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
331 * Returns the Sone core used by the Sone plugin.
333 * @return The Sone core
335 public Core getCore() {
336 return sonePlugin.core();
340 * Returns the template context factory of the web interface.
342 * @return The template context factory
344 public TemplateContextFactory getTemplateContextFactory() {
345 return templateContextFactory;
349 * Returns the current session, creating a new session if there is no
352 * @param toadletContenxt
353 * The toadlet context
354 * @return The current session, or {@code null} if there is no current
357 public Session getCurrentSession(ToadletContext toadletContenxt) {
358 return getCurrentSession(toadletContenxt, true);
362 * Returns the current session, creating a new session if there is no
363 * current session and {@code create} is {@code true}.
365 * @param toadletContenxt
366 * The toadlet context
368 * {@code true} to create a new session if there is no current
369 * session, {@code false} otherwise
370 * @return The current session, or {@code null} if there is no current
373 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
374 Session session = getSessionManager().useSession(toadletContenxt);
375 if (create && (session == null)) {
376 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
382 * Returns the currently logged in Sone.
384 * @param toadletContext
385 * The toadlet context
386 * @return The currently logged in Sone, or {@code null} if no Sone is
387 * currently logged in
389 public Sone getCurrentSone(ToadletContext toadletContext) {
390 return getCurrentSone(toadletContext, true);
394 * Returns the currently logged in Sone.
396 * @param toadletContext
397 * The toadlet context
399 * {@code true} to create a new session if no session exists,
400 * {@code false} to not create a new session
401 * @return The currently logged in Sone, or {@code null} if no Sone is
402 * currently logged in
404 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
405 Collection<Sone> localSones = getCore().getLocalSones();
406 if (localSones.size() == 1) {
407 return localSones.iterator().next();
409 return getCurrentSone(getCurrentSession(toadletContext, create));
413 * Returns the currently logged in Sone.
417 * @return The currently logged in Sone, or {@code null} if no Sone is
418 * currently logged in
420 public Sone getCurrentSone(Session session) {
421 if (session == null) {
424 String soneId = (String) session.getAttribute("Sone.CurrentSone");
425 if (soneId == null) {
428 return getCore().getLocalSone(soneId);
432 * Sets the currently logged in Sone.
434 * @param toadletContext
435 * The toadlet context
437 * The Sone to set as currently logged in
439 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
440 Session session = getCurrentSession(toadletContext);
442 session.removeAttribute("Sone.CurrentSone");
444 session.setAttribute("Sone.CurrentSone", sone.getId());
449 * Returns the notification manager.
451 * @return The notification manager
453 public NotificationManager getNotifications() {
454 return notificationManager;
458 * Returns the l10n helper of the node.
460 * @return The node’s l10n helper
462 public BaseL10n getL10n() {
463 return sonePlugin.l10n().getBase();
467 * Returns the session manager of the node.
469 * @return The node’s session manager
471 public SessionManager getSessionManager() {
472 return sonePlugin.pluginRespirator().getSessionManager("Sone");
476 * Returns the node’s form password.
478 * @return The form password
480 public String getFormPassword() {
485 * Returns the posts that have been announced as new in the
486 * {@link #newPostNotification}.
488 * @return The new posts
490 public Set<Post> getNewPosts() {
491 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
495 * Returns the replies that have been announced as new in the
496 * {@link #newReplyNotification}.
498 * @return The new replies
500 public Set<PostReply> getNewReplies() {
501 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
505 * Sets whether the current start of the plugin is the first start. It is
506 * considered a first start if the configuration file does not exist.
509 * {@code true} if no configuration file existed when Sone was
510 * loaded, {@code false} otherwise
512 public void setFirstStart(boolean firstStart) {
514 Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
515 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
516 notificationManager.addNotification(firstStartNotification);
521 * Sets whether Sone was started with a fresh configuration file.
524 * {@code true} if Sone was started with a fresh configuration,
525 * {@code false} if the existing configuration could be read
527 public void setNewConfig(boolean newConfig) {
528 if (newConfig && !hasFirstStartNotification()) {
529 Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
530 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
531 notificationManager.addNotification(configNotReadNotification);
540 * Returns whether the first start notification is currently displayed.
542 * @return {@code true} if the first-start notification is currently
543 * displayed, {@code false} otherwise
545 private boolean hasFirstStartNotification() {
546 return notificationManager.getNotification("first-start-notification") != null;
554 * Starts the web interface and registers all toadlets.
556 public void start() {
559 /* notification templates. */
560 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
562 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
563 notificationManager.addNotification(startupNotification);
565 ticker.schedule(new Runnable() {
569 startupNotification.dismiss();
571 }, 2, TimeUnit.MINUTES);
573 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
574 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
575 ticker.scheduleAtFixedRate(new Runnable() {
578 @SuppressWarnings("synthetic-access")
580 if (getCore().getIdentityManager().isConnected()) {
581 wotMissingNotification.dismiss();
583 notificationManager.addNotification(wotMissingNotification);
587 }, 15, 15, TimeUnit.SECONDS);
591 * Stops the web interface and unregisters all toadlets.
594 unregisterToadlets();
595 ticker.shutdownNow();
603 * Register all toadlets.
605 private void registerToadlets() {
606 Template emptyTemplate = parse(new StringReader(""));
607 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
608 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
609 Template newTemplate = loaders.loadTemplate("/templates/new.html");
610 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
611 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
612 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
613 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
614 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
615 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
616 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
617 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
618 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
619 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
620 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
621 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
622 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
623 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
624 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
625 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
626 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
627 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
628 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
629 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
630 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
631 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
632 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
633 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
634 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
635 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
636 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
637 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
639 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION, SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css")));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript")));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png")));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
716 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
717 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
718 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
719 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
721 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
722 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
723 for (PageToadlet toadlet : pageToadlets) {
724 String menuName = toadlet.getMenuName();
725 if (menuName != null) {
726 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
728 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
734 * Unregisters all toadlets.
736 private void unregisterToadlets() {
737 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
738 for (PageToadlet pageToadlet : pageToadlets) {
739 toadletContainer.unregister(pageToadlet);
741 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
745 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
746 * {@link SonePart}s in the given text (after parsing it using
747 * {@link SoneTextParser}).
751 * @return All mentioned local Sones
753 private Collection<Sone> getMentionedSones(String text) {
754 /* we need no context to find mentioned Sones. */
755 Set<Sone> mentionedSones = new HashSet<Sone>();
757 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
758 if (part instanceof SonePart) {
759 mentionedSones.add(((SonePart) part).getSone());
762 } catch (IOException ioe1) {
763 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
765 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
769 * Returns the Sone insert notification for the given Sone. If no
770 * notification for the given Sone exists, a new notification is created and
774 * The Sone to get the insert notification for
775 * @return The Sone insert notification
777 private TemplateNotification getSoneInsertNotification(Sone sone) {
778 synchronized (soneInsertNotifications) {
779 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
780 if (templateNotification == null) {
781 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
782 templateNotification.set("insertSone", sone);
783 soneInsertNotifications.put(sone, templateNotification);
785 return templateNotification;
789 private boolean localSoneMentionedInNewPostOrReply(Post post) {
790 if (!post.getSone().isLocal()) {
791 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
795 for (PostReply postReply : getCore().getReplies(post.getId())) {
796 if (postReply.getSone().isLocal()) {
799 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
811 * Notifies the web interface that a new {@link Sone} was found.
813 * @param newSoneFoundEvent
817 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
818 newSoneNotification.add(newSoneFoundEvent.sone());
819 if (!hasFirstStartNotification()) {
820 notificationManager.addNotification(newSoneNotification);
825 * Notifies the web interface that a new {@link Post} was found.
827 * @param newPostFoundEvent
831 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
832 Post post = newPostFoundEvent.post();
833 boolean isLocal = post.getSone().isLocal();
835 localPostNotification.add(post);
837 newPostNotification.add(post);
839 if (!hasFirstStartNotification()) {
840 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
841 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
842 mentionNotification.add(post);
843 notificationManager.addNotification(mentionNotification);
846 getCore().markPostKnown(post);
851 * Notifies the web interface that a new {@link PostReply} was found.
853 * @param newPostReplyFoundEvent
857 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
858 PostReply reply = newPostReplyFoundEvent.postReply();
859 boolean isLocal = reply.getSone().isLocal();
861 localReplyNotification.add(reply);
863 newReplyNotification.add(reply);
865 if (!hasFirstStartNotification()) {
866 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
867 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
868 mentionNotification.add(reply.getPost().get());
869 notificationManager.addNotification(mentionNotification);
872 getCore().markReplyKnown(reply);
877 * Notifies the web interface that a {@link Sone} was marked as known.
879 * @param markSoneKnownEvent
883 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
884 newSoneNotification.remove(markSoneKnownEvent.sone());
888 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
889 removePost(markPostKnownEvent.post());
893 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
894 removeReply(markPostReplyKnownEvent.postReply());
898 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
899 newSoneNotification.remove(soneRemovedEvent.sone());
900 for (Post post : soneRemovedEvent.sone().getPosts()) {
903 for (PostReply postReply : soneRemovedEvent.sone().getReplies()) {
904 removeReply(postReply);
909 public void postRemoved(PostRemovedEvent postRemovedEvent) {
910 removePost(postRemovedEvent.post());
913 private void removePost(Post post) {
914 newPostNotification.remove(post);
915 localPostNotification.remove(post);
916 if (!localSoneMentionedInNewPostOrReply(post)) {
917 mentionNotification.remove(post);
922 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
923 removeReply(postReplyRemovedEvent.postReply());
926 private void removeReply(PostReply reply) {
927 newReplyNotification.remove(reply);
928 localReplyNotification.remove(reply);
929 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
930 mentionNotification.remove(reply.getPost().get());
935 * Notifies the web interface that a Sone was locked.
937 * @param soneLockedEvent
941 public void soneLocked(SoneLockedEvent soneLockedEvent) {
942 final Sone sone = soneLockedEvent.sone();
943 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
946 @SuppressWarnings("synthetic-access")
948 lockedSonesNotification.add(sone);
949 notificationManager.addNotification(lockedSonesNotification);
951 }, 5, TimeUnit.MINUTES);
952 lockedSonesTickerObjects.put(sone, tickerObject);
956 * Notifies the web interface that a Sone was unlocked.
958 * @param soneUnlockedEvent
962 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
963 lockedSonesNotification.remove(soneUnlockedEvent.sone());
964 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
968 * Notifies the web interface that a {@link Sone} is being inserted.
970 * @param soneInsertingEvent
974 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
975 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
976 soneInsertNotification.set("soneStatus", "inserting");
977 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
978 notificationManager.addNotification(soneInsertNotification);
983 * Notifies the web interface that a {@link Sone} was inserted.
985 * @param soneInsertedEvent
989 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
990 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
991 soneInsertNotification.set("soneStatus", "inserted");
992 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
993 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
994 notificationManager.addNotification(soneInsertNotification);
999 * Notifies the web interface that a {@link Sone} insert was aborted.
1001 * @param soneInsertAbortedEvent
1005 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1006 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1007 soneInsertNotification.set("soneStatus", "insert-aborted");
1008 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1009 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1010 notificationManager.addNotification(soneInsertNotification);
1015 * Notifies the web interface that a new Sone version was found.
1017 * @param updateFoundEvent
1021 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1022 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1023 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1024 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1025 notificationManager.addNotification(newVersionNotification);
1029 * Notifies the web interface that an image insert was started
1031 * @param imageInsertStartedEvent
1035 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1036 insertingImagesNotification.add(imageInsertStartedEvent.image());
1037 notificationManager.addNotification(insertingImagesNotification);
1041 * Notifies the web interface that an {@link Image} insert was aborted.
1043 * @param imageInsertAbortedEvent
1047 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1048 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1052 * Notifies the web interface that an {@link Image} insert is finished.
1054 * @param imageInsertFinishedEvent
1058 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1059 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1060 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1061 notificationManager.addNotification(insertedImagesNotification);
1065 * Notifies the web interface that an {@link Image} insert has failed.
1067 * @param imageInsertFailedEvent
1071 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1072 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1073 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1074 notificationManager.addNotification(imageInsertFailedNotification);