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 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.ClassPathTemplateProvider;
133 import net.pterodactylus.util.template.CollectionSortFilter;
134 import net.pterodactylus.util.template.ContainsFilter;
135 import net.pterodactylus.util.template.DateFilter;
136 import net.pterodactylus.util.template.FormatFilter;
137 import net.pterodactylus.util.template.HtmlFilter;
138 import net.pterodactylus.util.template.MatchFilter;
139 import net.pterodactylus.util.template.ModFilter;
140 import net.pterodactylus.util.template.PaginationFilter;
141 import net.pterodactylus.util.template.ReflectionAccessor;
142 import net.pterodactylus.util.template.ReplaceFilter;
143 import net.pterodactylus.util.template.StoreFilter;
144 import net.pterodactylus.util.template.Template;
145 import net.pterodactylus.util.template.TemplateContextFactory;
146 import net.pterodactylus.util.template.TemplateProvider;
147 import net.pterodactylus.util.template.XmlFilter;
148 import net.pterodactylus.util.web.RedirectPage;
149 import net.pterodactylus.util.web.StaticPage;
150 import net.pterodactylus.util.web.TemplatePage;
152 import com.google.common.collect.Collections2;
153 import com.google.common.collect.ImmutableSet;
154 import com.google.common.eventbus.Subscribe;
155 import com.google.inject.Inject;
157 import freenet.clients.http.SessionManager;
158 import freenet.clients.http.SessionManager.Session;
159 import freenet.clients.http.ToadletContainer;
160 import freenet.clients.http.ToadletContext;
161 import freenet.l10n.BaseL10n;
162 import freenet.support.api.HTTPRequest;
165 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
166 * references to l10n helpers.
168 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
170 public class WebInterface {
173 private static final Logger logger = getLogger(WebInterface.class.getName());
175 /** The loaders for templates, pages, and classpath providers. */
176 private final Loaders loaders;
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, Loaders loaders) {
249 this.sonePlugin = sonePlugin;
250 this.loaders = loaders;
251 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
252 soneTextParser = new SoneTextParser(getCore(), getCore());
254 templateContextFactory = new TemplateContextFactory();
255 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
256 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
257 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
258 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
259 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
260 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
261 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
262 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
263 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
264 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
265 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
266 templateContextFactory.addFilter("date", new DateFilter());
267 templateContextFactory.addFilter("html", new HtmlFilter());
268 templateContextFactory.addFilter("replace", new ReplaceFilter());
269 templateContextFactory.addFilter("store", new StoreFilter());
270 templateContextFactory.addFilter("l10n", new L10nFilter(this));
271 templateContextFactory.addFilter("substring", new SubstringFilter());
272 templateContextFactory.addFilter("xml", new XmlFilter());
273 templateContextFactory.addFilter("change", new RequestChangeFilter());
274 templateContextFactory.addFilter("match", new MatchFilter());
275 templateContextFactory.addFilter("css", new CssClassNameFilter());
276 templateContextFactory.addFilter("js", new JavascriptFilter());
277 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
278 templateContextFactory.addFilter("reparse", new ReparseFilter());
279 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
280 templateContextFactory.addFilter("format", new FormatFilter());
281 templateContextFactory.addFilter("sort", new CollectionSortFilter());
282 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
283 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
284 templateContextFactory.addFilter("in", new ContainsFilter());
285 templateContextFactory.addFilter("unique", new UniqueElementFilter());
286 templateContextFactory.addFilter("mod", new ModFilter());
287 templateContextFactory.addFilter("paginate", new PaginationFilter());
288 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
289 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
290 templateContextFactory.addTemplateObject("webInterface", this);
291 templateContextFactory.addTemplateObject("formPassword", formPassword);
293 /* create notifications. */
294 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
295 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
297 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
298 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
300 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
301 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
303 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
304 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
306 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
307 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
309 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
310 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
312 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
313 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
315 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
316 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
318 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
319 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
321 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
322 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
324 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
325 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
333 * Returns the Sone core used by the Sone plugin.
335 * @return The Sone core
337 public Core getCore() {
338 return sonePlugin.core();
342 * Returns the template context factory of the web interface.
344 * @return The template context factory
346 public TemplateContextFactory getTemplateContextFactory() {
347 return templateContextFactory;
351 * Returns the current session, creating a new session if there is no
354 * @param toadletContenxt
355 * The toadlet context
356 * @return The current session, or {@code null} if there is no current
359 public Session getCurrentSession(ToadletContext toadletContenxt) {
360 return getCurrentSession(toadletContenxt, true);
364 * Returns the current session, creating a new session if there is no
365 * current session and {@code create} is {@code true}.
367 * @param toadletContenxt
368 * The toadlet context
370 * {@code true} to create a new session if there is no current
371 * session, {@code false} otherwise
372 * @return The current session, or {@code null} if there is no current
375 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
376 Session session = getSessionManager().useSession(toadletContenxt);
377 if (create && (session == null)) {
378 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
384 * Returns the currently logged in Sone.
386 * @param toadletContext
387 * The toadlet context
388 * @return The currently logged in Sone, or {@code null} if no Sone is
389 * currently logged in
391 public Sone getCurrentSone(ToadletContext toadletContext) {
392 return getCurrentSone(toadletContext, true);
396 * Returns the currently logged in Sone.
398 * @param toadletContext
399 * The toadlet context
401 * {@code true} to create a new session if no session exists,
402 * {@code false} to not create a new session
403 * @return The currently logged in Sone, or {@code null} if no Sone is
404 * currently logged in
406 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
407 Collection<Sone> localSones = getCore().getLocalSones();
408 if (localSones.size() == 1) {
409 return localSones.iterator().next();
411 return getCurrentSone(getCurrentSession(toadletContext, create));
415 * Returns the currently logged in Sone.
419 * @return The currently logged in Sone, or {@code null} if no Sone is
420 * currently logged in
422 public Sone getCurrentSone(Session session) {
423 if (session == null) {
426 String soneId = (String) session.getAttribute("Sone.CurrentSone");
427 if (soneId == null) {
430 return getCore().getLocalSone(soneId);
434 * Sets the currently logged in Sone.
436 * @param toadletContext
437 * The toadlet context
439 * The Sone to set as currently logged in
441 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
442 Session session = getCurrentSession(toadletContext);
444 session.removeAttribute("Sone.CurrentSone");
446 session.setAttribute("Sone.CurrentSone", sone.getId());
451 * Returns the notification manager.
453 * @return The notification manager
455 public NotificationManager getNotifications() {
456 return notificationManager;
460 * Returns the l10n helper of the node.
462 * @return The node’s l10n helper
464 public BaseL10n getL10n() {
465 return sonePlugin.l10n().getBase();
469 * Returns the session manager of the node.
471 * @return The node’s session manager
473 public SessionManager getSessionManager() {
474 return sonePlugin.pluginRespirator().getSessionManager("Sone");
478 * Returns the node’s form password.
480 * @return The form password
482 public String getFormPassword() {
487 * Returns the posts that have been announced as new in the
488 * {@link #newPostNotification}.
490 * @return The new posts
492 public Set<Post> getNewPosts() {
493 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
497 * Returns the replies that have been announced as new in the
498 * {@link #newReplyNotification}.
500 * @return The new replies
502 public Set<PostReply> getNewReplies() {
503 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
507 * Sets whether the current start of the plugin is the first start. It is
508 * considered a first start if the configuration file does not exist.
511 * {@code true} if no configuration file existed when Sone was
512 * loaded, {@code false} otherwise
514 public void setFirstStart(boolean firstStart) {
516 Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
517 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
518 notificationManager.addNotification(firstStartNotification);
523 * Sets whether Sone was started with a fresh configuration file.
526 * {@code true} if Sone was started with a fresh configuration,
527 * {@code false} if the existing configuration could be read
529 public void setNewConfig(boolean newConfig) {
530 if (newConfig && !hasFirstStartNotification()) {
531 Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
532 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
533 notificationManager.addNotification(configNotReadNotification);
542 * Returns whether the first start notification is currently displayed.
544 * @return {@code true} if the first-start notification is currently
545 * displayed, {@code false} otherwise
547 private boolean hasFirstStartNotification() {
548 return notificationManager.getNotification("first-start-notification") != null;
556 * Starts the web interface and registers all toadlets.
558 public void start() {
561 /* notification templates. */
562 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
564 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
565 notificationManager.addNotification(startupNotification);
567 ticker.schedule(new Runnable() {
571 startupNotification.dismiss();
573 }, 2, TimeUnit.MINUTES);
575 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
576 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
577 ticker.scheduleAtFixedRate(new Runnable() {
580 @SuppressWarnings("synthetic-access")
582 if (getCore().getIdentityManager().isConnected()) {
583 wotMissingNotification.dismiss();
585 notificationManager.addNotification(wotMissingNotification);
589 }, 15, 15, TimeUnit.SECONDS);
593 * Stops the web interface and unregisters all toadlets.
596 unregisterToadlets();
597 ticker.shutdownNow();
605 * Register all toadlets.
607 private void registerToadlets() {
608 Template emptyTemplate = parse(new StringReader(""));
609 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
610 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
611 Template newTemplate = loaders.loadTemplate("/templates/new.html");
612 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
613 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
614 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
615 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
616 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
617 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
618 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
619 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
620 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
621 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
622 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
623 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
624 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
625 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
626 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
627 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
628 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
629 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
630 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
631 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
632 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
633 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
634 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
635 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
636 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
637 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
638 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
639 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
641 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION, SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
716 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
717 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
718 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
719 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
720 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
721 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
723 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
724 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
725 for (PageToadlet toadlet : pageToadlets) {
726 String menuName = toadlet.getMenuName();
727 if (menuName != null) {
728 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
730 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
736 * Unregisters all toadlets.
738 private void unregisterToadlets() {
739 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
740 for (PageToadlet pageToadlet : pageToadlets) {
741 toadletContainer.unregister(pageToadlet);
743 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
747 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
748 * {@link SonePart}s in the given text (after parsing it using
749 * {@link SoneTextParser}).
753 * @return All mentioned local Sones
755 private Collection<Sone> getMentionedSones(String text) {
756 /* we need no context to find mentioned Sones. */
757 Set<Sone> mentionedSones = new HashSet<Sone>();
759 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
760 if (part instanceof SonePart) {
761 mentionedSones.add(((SonePart) part).getSone());
764 } catch (IOException ioe1) {
765 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
767 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
771 * Returns the Sone insert notification for the given Sone. If no
772 * notification for the given Sone exists, a new notification is created and
776 * The Sone to get the insert notification for
777 * @return The Sone insert notification
779 private TemplateNotification getSoneInsertNotification(Sone sone) {
780 synchronized (soneInsertNotifications) {
781 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
782 if (templateNotification == null) {
783 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
784 templateNotification.set("insertSone", sone);
785 soneInsertNotifications.put(sone, templateNotification);
787 return templateNotification;
791 private boolean localSoneMentionedInNewPostOrReply(Post post) {
792 if (!post.getSone().isLocal()) {
793 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
797 for (PostReply postReply : getCore().getReplies(post.getId())) {
798 if (postReply.getSone().isLocal()) {
801 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
813 * Notifies the web interface that a new {@link Sone} was found.
815 * @param newSoneFoundEvent
819 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
820 newSoneNotification.add(newSoneFoundEvent.sone());
821 if (!hasFirstStartNotification()) {
822 notificationManager.addNotification(newSoneNotification);
827 * Notifies the web interface that a new {@link Post} was found.
829 * @param newPostFoundEvent
833 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
834 Post post = newPostFoundEvent.post();
835 boolean isLocal = post.getSone().isLocal();
837 localPostNotification.add(post);
839 newPostNotification.add(post);
841 if (!hasFirstStartNotification()) {
842 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
843 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
844 mentionNotification.add(post);
845 notificationManager.addNotification(mentionNotification);
848 getCore().markPostKnown(post);
853 * Notifies the web interface that a new {@link PostReply} was found.
855 * @param newPostReplyFoundEvent
859 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
860 PostReply reply = newPostReplyFoundEvent.postReply();
861 boolean isLocal = reply.getSone().isLocal();
863 localReplyNotification.add(reply);
865 newReplyNotification.add(reply);
867 if (!hasFirstStartNotification()) {
868 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
869 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
870 mentionNotification.add(reply.getPost().get());
871 notificationManager.addNotification(mentionNotification);
874 getCore().markReplyKnown(reply);
879 * Notifies the web interface that a {@link Sone} was marked as known.
881 * @param markSoneKnownEvent
885 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
886 newSoneNotification.remove(markSoneKnownEvent.sone());
890 * Notifies the web interface that a {@link Post} was marked as known.
892 * @param markPostKnownEvent
896 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
897 newPostNotification.remove(markPostKnownEvent.post());
898 localPostNotification.remove(markPostKnownEvent.post());
899 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
900 mentionNotification.remove(markPostKnownEvent.post());
905 * Notifies the web interface that a {@link PostReply} was marked as known.
907 * @param markPostReplyKnownEvent
911 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
912 PostReply postReply = markPostReplyKnownEvent.postReply();
913 newReplyNotification.remove(postReply);
914 localReplyNotification.remove(postReply);
915 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
916 mentionNotification.remove(postReply.getPost().get());
921 * Notifies the web interface that a {@link Sone} was removed.
923 * @param soneRemovedEvent
927 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
928 newSoneNotification.remove(soneRemovedEvent.sone());
932 * Notifies the web interface that a {@link Post} was removed.
934 * @param postRemovedEvent
938 public void postRemoved(PostRemovedEvent postRemovedEvent) {
939 newPostNotification.remove(postRemovedEvent.post());
940 localPostNotification.remove(postRemovedEvent.post());
941 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
942 mentionNotification.remove(postRemovedEvent.post());
947 * Notifies the web interface that a {@link PostReply} was removed.
949 * @param postReplyRemovedEvent
953 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
954 PostReply reply = postReplyRemovedEvent.postReply();
955 newReplyNotification.remove(reply);
956 localReplyNotification.remove(reply);
957 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
958 mentionNotification.remove(reply.getPost().get());
963 * Notifies the web interface that a Sone was locked.
965 * @param soneLockedEvent
969 public void soneLocked(SoneLockedEvent soneLockedEvent) {
970 final Sone sone = soneLockedEvent.sone();
971 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
974 @SuppressWarnings("synthetic-access")
976 lockedSonesNotification.add(sone);
977 notificationManager.addNotification(lockedSonesNotification);
979 }, 5, TimeUnit.MINUTES);
980 lockedSonesTickerObjects.put(sone, tickerObject);
984 * Notifies the web interface that a Sone was unlocked.
986 * @param soneUnlockedEvent
990 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
991 lockedSonesNotification.remove(soneUnlockedEvent.sone());
992 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
996 * Notifies the web interface that a {@link Sone} is being inserted.
998 * @param soneInsertingEvent
1002 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1003 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1004 soneInsertNotification.set("soneStatus", "inserting");
1005 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1006 notificationManager.addNotification(soneInsertNotification);
1011 * Notifies the web interface that a {@link Sone} was inserted.
1013 * @param soneInsertedEvent
1017 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1018 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1019 soneInsertNotification.set("soneStatus", "inserted");
1020 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1021 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1022 notificationManager.addNotification(soneInsertNotification);
1027 * Notifies the web interface that a {@link Sone} insert was aborted.
1029 * @param soneInsertAbortedEvent
1033 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1034 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1035 soneInsertNotification.set("soneStatus", "insert-aborted");
1036 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1037 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1038 notificationManager.addNotification(soneInsertNotification);
1043 * Notifies the web interface that a new Sone version was found.
1045 * @param updateFoundEvent
1049 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1050 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1051 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1052 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1053 notificationManager.addNotification(newVersionNotification);
1057 * Notifies the web interface that an image insert was started
1059 * @param imageInsertStartedEvent
1063 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1064 insertingImagesNotification.add(imageInsertStartedEvent.image());
1065 notificationManager.addNotification(insertingImagesNotification);
1069 * Notifies the web interface that an {@link Image} insert was aborted.
1071 * @param imageInsertAbortedEvent
1075 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1076 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1080 * Notifies the web interface that an {@link Image} insert is finished.
1082 * @param imageInsertFinishedEvent
1086 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1087 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1088 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1089 notificationManager.addNotification(insertedImagesNotification);
1093 * Notifies the web interface that an {@link Image} insert has failed.
1095 * @param imageInsertFailedEvent
1099 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1100 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1101 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1102 notificationManager.addNotification(imageInsertFailedNotification);