Use time text converter to display “last updated” text
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * Sone - WebInterface.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import static com.google.common.collect.FluentIterable.from;
21 import static java.util.logging.Logger.getLogger;
22 import static net.pterodactylus.util.template.TemplateParser.parse;
23
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;
31 import java.util.Map;
32 import java.util.Set;
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.Logger;
39 import javax.annotation.Nonnull;
40 import javax.annotation.Nullable;
41
42 import net.pterodactylus.sone.core.Core;
43 import net.pterodactylus.sone.core.ElementLoader;
44 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
46 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
47 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
48 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
49 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
50 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
51 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
52 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
53 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
54 import net.pterodactylus.sone.core.event.PostRemovedEvent;
55 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
57 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
58 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
59 import net.pterodactylus.sone.core.event.SoneLockedEvent;
60 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
61 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
62 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
63 import net.pterodactylus.sone.data.Album;
64 import net.pterodactylus.sone.data.Image;
65 import net.pterodactylus.sone.data.Post;
66 import net.pterodactylus.sone.data.PostReply;
67 import net.pterodactylus.sone.data.Profile;
68 import net.pterodactylus.sone.data.Reply;
69 import net.pterodactylus.sone.data.Sone;
70 import net.pterodactylus.sone.freenet.L10nFilter;
71 import net.pterodactylus.sone.freenet.wot.Identity;
72 import net.pterodactylus.sone.freenet.wot.Trust;
73 import net.pterodactylus.sone.main.Loaders;
74 import net.pterodactylus.sone.main.ReparseFilter;
75 import net.pterodactylus.sone.main.SonePlugin;
76 import net.pterodactylus.sone.notify.ListNotification;
77 import net.pterodactylus.sone.notify.ListNotificationFilter;
78 import net.pterodactylus.sone.notify.PostVisibilityFilter;
79 import net.pterodactylus.sone.notify.ReplyVisibilityFilter;
80 import net.pterodactylus.sone.template.AlbumAccessor;
81 import net.pterodactylus.sone.template.CollectionAccessor;
82 import net.pterodactylus.sone.template.CssClassNameFilter;
83 import net.pterodactylus.sone.template.HttpRequestAccessor;
84 import net.pterodactylus.sone.template.IdentityAccessor;
85 import net.pterodactylus.sone.template.ImageAccessor;
86 import net.pterodactylus.sone.template.ImageLinkFilter;
87 import net.pterodactylus.sone.template.JavascriptFilter;
88 import net.pterodactylus.sone.template.LinkedElementRenderFilter;
89 import net.pterodactylus.sone.template.LinkedElementsFilter;
90 import net.pterodactylus.sone.template.ParserFilter;
91 import net.pterodactylus.sone.template.PostAccessor;
92 import net.pterodactylus.sone.template.ProfileAccessor;
93 import net.pterodactylus.sone.template.RenderFilter;
94 import net.pterodactylus.sone.template.ReplyAccessor;
95 import net.pterodactylus.sone.template.ReplyGroupFilter;
96 import net.pterodactylus.sone.template.RequestChangeFilter;
97 import net.pterodactylus.sone.template.ShortenFilter;
98 import net.pterodactylus.sone.template.SoneAccessor;
99 import net.pterodactylus.sone.template.SubstringFilter;
100 import net.pterodactylus.sone.template.TrustAccessor;
101 import net.pterodactylus.sone.template.UniqueElementFilter;
102 import net.pterodactylus.sone.template.UnknownDateFilter;
103 import net.pterodactylus.sone.text.Part;
104 import net.pterodactylus.sone.text.SonePart;
105 import net.pterodactylus.sone.text.SoneTextParser;
106 import net.pterodactylus.sone.text.TimeTextConverter;
107 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
108 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
109 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
110 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
111 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
112 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
113 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
114 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
115 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
116 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
117 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
118 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
119 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
120 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
121 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
122 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
123 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
124 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
125 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
126 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
127 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
128 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
129 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
130 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
131 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
132 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
133 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
134 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
135 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
136 import net.pterodactylus.sone.web.page.FreenetRequest;
137 import net.pterodactylus.sone.web.page.PageToadlet;
138 import net.pterodactylus.sone.web.page.PageToadletFactory;
139 import net.pterodactylus.util.notify.Notification;
140 import net.pterodactylus.util.notify.NotificationManager;
141 import net.pterodactylus.util.notify.TemplateNotification;
142 import net.pterodactylus.util.template.CollectionSortFilter;
143 import net.pterodactylus.util.template.ContainsFilter;
144 import net.pterodactylus.util.template.DateFilter;
145 import net.pterodactylus.util.template.FormatFilter;
146 import net.pterodactylus.util.template.HtmlFilter;
147 import net.pterodactylus.util.template.MatchFilter;
148 import net.pterodactylus.util.template.ModFilter;
149 import net.pterodactylus.util.template.PaginationFilter;
150 import net.pterodactylus.util.template.ReflectionAccessor;
151 import net.pterodactylus.util.template.ReplaceFilter;
152 import net.pterodactylus.util.template.StoreFilter;
153 import net.pterodactylus.util.template.Template;
154 import net.pterodactylus.util.template.TemplateContextFactory;
155 import net.pterodactylus.util.template.TemplateProvider;
156 import net.pterodactylus.util.template.XmlFilter;
157 import net.pterodactylus.util.web.RedirectPage;
158 import net.pterodactylus.util.web.TemplatePage;
159
160 import freenet.clients.http.SessionManager;
161 import freenet.clients.http.SessionManager.Session;
162 import freenet.clients.http.ToadletContainer;
163 import freenet.clients.http.ToadletContext;
164 import freenet.l10n.BaseL10n;
165 import freenet.support.api.HTTPRequest;
166
167 import com.google.common.base.Optional;
168 import com.google.common.collect.Collections2;
169 import com.google.common.collect.ImmutableSet;
170 import com.google.common.eventbus.Subscribe;
171 import com.google.inject.Inject;
172
173 /**
174  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
175  * references to l10n helpers.
176  *
177  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
178  */
179 public class WebInterface {
180
181         /** The logger. */
182         private static final Logger logger = getLogger(WebInterface.class.getName());
183
184         /** The loaders for templates, pages, and classpath providers. */
185         private final Loaders loaders;
186
187         /** The notification manager. */
188         private final NotificationManager notificationManager = new NotificationManager();
189
190         /** The Sone plugin. */
191         private final SonePlugin sonePlugin;
192
193         /** The registered toadlets. */
194         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
195
196         /** The form password. */
197         private final String formPassword;
198
199         /** The template context factory. */
200         private final TemplateContextFactory templateContextFactory;
201
202         /** The Sone text parser. */
203         private final SoneTextParser soneTextParser;
204
205         /** The parser filter. */
206         private final ParserFilter parserFilter;
207         private final ShortenFilter shortenFilter;
208         private final RenderFilter renderFilter;
209
210         private final ListNotificationFilter listNotificationFilter;
211         private final PostVisibilityFilter postVisibilityFilter;
212         private final ReplyVisibilityFilter replyVisibilityFilter;
213
214         private final TimeTextConverter timeTextConverter = new TimeTextConverter();
215         private final L10nFilter l10nFilter = new L10nFilter(this);
216
217         /** The “new Sone” notification. */
218         private final ListNotification<Sone> newSoneNotification;
219
220         /** The “new post” notification. */
221         private final ListNotification<Post> newPostNotification;
222
223         /** The “new reply” notification. */
224         private final ListNotification<PostReply> newReplyNotification;
225
226         /** The invisible “local post” notification. */
227         private final ListNotification<Post> localPostNotification;
228
229         /** The invisible “local reply” notification. */
230         private final ListNotification<PostReply> localReplyNotification;
231
232         /** The “you have been mentioned” notification. */
233         private final ListNotification<Post> mentionNotification;
234
235         /** Notifications for sone inserts. */
236         private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
237
238         /** Sone locked notification ticker objects. */
239         private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
240
241         /** The “Sone locked” notification. */
242         private final ListNotification<Sone> lockedSonesNotification;
243
244         /** The “new version” notification. */
245         private final TemplateNotification newVersionNotification;
246
247         /** The “inserting images” notification. */
248         private final ListNotification<Image> insertingImagesNotification;
249
250         /** The “inserted images” notification. */
251         private final ListNotification<Image> insertedImagesNotification;
252
253         /** The “image insert failed” notification. */
254         private final ListNotification<Image> imageInsertFailedNotification;
255
256         /** Scheduled executor for time-based notifications. */
257         private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
258
259         /**
260          * Creates a new web interface.
261          *
262          * @param sonePlugin
263          *            The Sone plugin
264          */
265         @Inject
266         public WebInterface(SonePlugin sonePlugin, Loaders loaders, ListNotificationFilter listNotificationFilter, PostVisibilityFilter postVisibilityFilter, ReplyVisibilityFilter replyVisibilityFilter, ElementLoader elementLoader) {
267                 this.sonePlugin = sonePlugin;
268                 this.loaders = loaders;
269                 this.listNotificationFilter = listNotificationFilter;
270                 this.postVisibilityFilter = postVisibilityFilter;
271                 this.replyVisibilityFilter = replyVisibilityFilter;
272                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
273                 soneTextParser = new SoneTextParser(getCore(), getCore());
274
275                 templateContextFactory = new TemplateContextFactory();
276                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
277                 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
278                 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore(), new TimeTextConverter()));
279                 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
280                 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
281                 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
282                 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
283                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
284                 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
285                 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
286                 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
287                 templateContextFactory.addFilter("date", new DateFilter());
288                 templateContextFactory.addFilter("html", new HtmlFilter());
289                 templateContextFactory.addFilter("replace", new ReplaceFilter());
290                 templateContextFactory.addFilter("store", new StoreFilter());
291                 templateContextFactory.addFilter("l10n", new L10nFilter(this));
292                 templateContextFactory.addFilter("substring", new SubstringFilter());
293                 templateContextFactory.addFilter("xml", new XmlFilter());
294                 templateContextFactory.addFilter("change", new RequestChangeFilter());
295                 templateContextFactory.addFilter("match", new MatchFilter());
296                 templateContextFactory.addFilter("css", new CssClassNameFilter());
297                 templateContextFactory.addFilter("js", new JavascriptFilter());
298                 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), soneTextParser));
299                 templateContextFactory.addFilter("shorten", shortenFilter = new ShortenFilter());
300                 templateContextFactory.addFilter("render", renderFilter = new RenderFilter(getCore(), templateContextFactory));
301                 templateContextFactory.addFilter("linked-elements", new LinkedElementsFilter(elementLoader));
302                 templateContextFactory.addFilter("render-linked-element", new LinkedElementRenderFilter(templateContextFactory));
303                 templateContextFactory.addFilter("reparse", new ReparseFilter());
304                 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
305                 templateContextFactory.addFilter("format", new FormatFilter());
306                 templateContextFactory.addFilter("sort", new CollectionSortFilter());
307                 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
308                 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
309                 templateContextFactory.addFilter("in", new ContainsFilter());
310                 templateContextFactory.addFilter("unique", new UniqueElementFilter());
311                 templateContextFactory.addFilter("mod", new ModFilter());
312                 templateContextFactory.addFilter("paginate", new PaginationFilter());
313                 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
314                 templateContextFactory.addProvider(loaders.getTemplateProvider());
315                 templateContextFactory.addTemplateObject("webInterface", this);
316                 templateContextFactory.addTemplateObject("formPassword", formPassword);
317
318                 /* create notifications. */
319                 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
320                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
321
322                 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
323                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
324
325                 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
326                 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
327
328                 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
329                 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
330
331                 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
332                 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
333
334                 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
335                 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
336
337                 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
338                 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
339
340                 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
341                 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
342
343                 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
344                 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
345
346                 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
347                 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
348
349                 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
350                 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
351         }
352
353         //
354         // ACCESSORS
355         //
356
357         /**
358          * Returns the Sone core used by the Sone plugin.
359          *
360          * @return The Sone core
361          */
362         public Core getCore() {
363                 return sonePlugin.core();
364         }
365
366         /**
367          * Returns the template context factory of the web interface.
368          *
369          * @return The template context factory
370          */
371         public TemplateContextFactory getTemplateContextFactory() {
372                 return templateContextFactory;
373         }
374
375         /**
376          * Returns the current session, creating a new session if there is no
377          * current session.
378          *
379          * @param toadletContenxt
380          *            The toadlet context
381          * @return The current session, or {@code null} if there is no current
382          *         session
383          */
384         public Session getCurrentSession(ToadletContext toadletContenxt) {
385                 return getCurrentSession(toadletContenxt, true);
386         }
387
388         /**
389          * Returns the current session, creating a new session if there is no
390          * current session and {@code create} is {@code true}.
391          *
392          * @param toadletContenxt
393          *            The toadlet context
394          * @param create
395          *            {@code true} to create a new session if there is no current
396          *            session, {@code false} otherwise
397          * @return The current session, or {@code null} if there is no current
398          *         session
399          */
400         public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
401                 Session session = getSessionManager().useSession(toadletContenxt);
402                 if (create && (session == null)) {
403                         session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
404                 }
405                 return session;
406         }
407
408         /**
409          * Returns the currently logged in Sone.
410          *
411          * @param toadletContext
412          *            The toadlet context
413          * @return The currently logged in Sone, or {@code null} if no Sone is
414          *         currently logged in
415          */
416         public Sone getCurrentSone(ToadletContext toadletContext) {
417                 return getCurrentSone(toadletContext, true);
418         }
419
420         /**
421          * Returns the currently logged in Sone.
422          *
423          * @param toadletContext
424          *            The toadlet context
425          * @param create
426          *            {@code true} to create a new session if no session exists,
427          *            {@code false} to not create a new session
428          * @return The currently logged in Sone, or {@code null} if no Sone is
429          *         currently logged in
430          */
431         public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
432                 Collection<Sone> localSones = getCore().getLocalSones();
433                 if (localSones.size() == 1) {
434                         return localSones.iterator().next();
435                 }
436                 return getCurrentSone(getCurrentSession(toadletContext, create));
437         }
438
439         /**
440          * Returns the currently logged in Sone.
441          *
442          * @param session
443          *            The session
444          * @return The currently logged in Sone, or {@code null} if no Sone is
445          *         currently logged in
446          */
447         public Sone getCurrentSone(Session session) {
448                 if (session == null) {
449                         return null;
450                 }
451                 String soneId = (String) session.getAttribute("Sone.CurrentSone");
452                 if (soneId == null) {
453                         return null;
454                 }
455                 return getCore().getLocalSone(soneId);
456         }
457
458         /**
459          * Sets the currently logged in Sone.
460          *
461          * @param toadletContext
462          *            The toadlet context
463          * @param sone
464          *            The Sone to set as currently logged in
465          */
466         public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
467                 Session session = getCurrentSession(toadletContext);
468                 if (sone == null) {
469                         session.removeAttribute("Sone.CurrentSone");
470                 } else {
471                         session.setAttribute("Sone.CurrentSone", sone.getId());
472                 }
473         }
474
475         /**
476          * Returns the notification manager.
477          *
478          * @return The notification manager
479          */
480         public NotificationManager getNotifications() {
481                 return notificationManager;
482         }
483
484         @Nonnull
485         public Optional<Notification> getNotification(@Nonnull String notificationId) {
486                 return Optional.fromNullable(notificationManager.getNotification(notificationId));
487         }
488
489         @Nonnull
490         public Collection<Notification> getNotifications(@Nullable Sone currentSone) {
491                 return listNotificationFilter.filterNotifications(notificationManager.getNotifications(), currentSone);
492         }
493
494         /**
495          * Returns the l10n helper of the node.
496          *
497          * @return The node’s l10n helper
498          */
499         public BaseL10n getL10n() {
500                 return sonePlugin.l10n().getBase();
501         }
502
503         /**
504          * Returns the session manager of the node.
505          *
506          * @return The node’s session manager
507          */
508         public SessionManager getSessionManager() {
509                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
510         }
511
512         /**
513          * Returns the node’s form password.
514          *
515          * @return The form password
516          */
517         public String getFormPassword() {
518                 return formPassword;
519         }
520
521         /**
522          * Returns the posts that have been announced as new in the
523          * {@link #newPostNotification}.
524          *
525          * @return The new posts
526          */
527         public Set<Post> getNewPosts() {
528                 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
529         }
530
531         @Nonnull
532         public Collection<Post> getNewPosts(@Nullable Sone currentSone) {
533                 Set<Post> allNewPosts = ImmutableSet.<Post> builder()
534                                 .addAll(newPostNotification.getElements())
535                                 .addAll(localPostNotification.getElements())
536                                 .build();
537                 return from(allNewPosts).filter(postVisibilityFilter.isVisible(currentSone)).toSet();
538         }
539
540         /**
541          * Returns the replies that have been announced as new in the
542          * {@link #newReplyNotification}.
543          *
544          * @return The new replies
545          */
546         public Set<PostReply> getNewReplies() {
547                 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
548         }
549
550         @Nonnull
551         public Collection<PostReply> getNewReplies(@Nullable Sone currentSone) {
552                 Set<PostReply> allNewReplies = ImmutableSet.<PostReply>builder()
553                                 .addAll(newReplyNotification.getElements())
554                                 .addAll(localReplyNotification.getElements())
555                                 .build();
556                 return from(allNewReplies).filter(replyVisibilityFilter.isVisible(currentSone)).toSet();
557         }
558
559         /**
560          * Sets whether the current start of the plugin is the first start. It is
561          * considered a first start if the configuration file does not exist.
562          *
563          * @param firstStart
564          *            {@code true} if no configuration file existed when Sone was
565          *            loaded, {@code false} otherwise
566          */
567         public void setFirstStart(boolean firstStart) {
568                 if (firstStart) {
569                         Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
570                         Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
571                         notificationManager.addNotification(firstStartNotification);
572                 }
573         }
574
575         /**
576          * Sets whether Sone was started with a fresh configuration file.
577          *
578          * @param newConfig
579          *            {@code true} if Sone was started with a fresh configuration,
580          *            {@code false} if the existing configuration could be read
581          */
582         public void setNewConfig(boolean newConfig) {
583                 if (newConfig && !hasFirstStartNotification()) {
584                         Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
585                         Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
586                         notificationManager.addNotification(configNotReadNotification);
587                 }
588         }
589
590         //
591         // PRIVATE ACCESSORS
592         //
593
594         /**
595          * Returns whether the first start notification is currently displayed.
596          *
597          * @return {@code true} if the first-start notification is currently
598          *         displayed, {@code false} otherwise
599          */
600         private boolean hasFirstStartNotification() {
601                 return notificationManager.getNotification("first-start-notification") != null;
602         }
603
604         //
605         // ACTIONS
606         //
607
608         /**
609          * Starts the web interface and registers all toadlets.
610          */
611         public void start() {
612                 registerToadlets();
613
614                 /* notification templates. */
615                 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
616
617                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
618                 notificationManager.addNotification(startupNotification);
619
620                 ticker.schedule(new Runnable() {
621
622                         @Override
623                         public void run() {
624                                 startupNotification.dismiss();
625                         }
626                 }, 2, TimeUnit.MINUTES);
627
628                 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
629                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
630                 ticker.scheduleAtFixedRate(new Runnable() {
631
632                         @Override
633                         @SuppressWarnings("synthetic-access")
634                         public void run() {
635                                 if (getCore().getIdentityManager().isConnected()) {
636                                         wotMissingNotification.dismiss();
637                                 } else {
638                                         notificationManager.addNotification(wotMissingNotification);
639                                 }
640                         }
641
642                 }, 15, 15, TimeUnit.SECONDS);
643         }
644
645         /**
646          * Stops the web interface and unregisters all toadlets.
647          */
648         public void stop() {
649                 unregisterToadlets();
650                 ticker.shutdownNow();
651         }
652
653         //
654         // PRIVATE METHODS
655         //
656
657         /**
658          * Register all toadlets.
659          */
660         private void registerToadlets() {
661                 Template emptyTemplate = parse(new StringReader(""));
662                 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
663                 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
664                 Template newTemplate = loaders.loadTemplate("/templates/new.html");
665                 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
666                 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
667                 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
668                 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
669                 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
670                 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
671                 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
672                 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
673                 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
674                 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
675                 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
676                 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
677                 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
678                 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
679                 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
680                 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
681                 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
682                 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
683                 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
684                 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
685                 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
686                 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
687                 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
688                 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
689                 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
690                 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
691                 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
692                 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
693
694                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
695                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
696                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this, postVisibilityFilter), "Index"));
697                 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
698                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
699                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
700                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
701                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
702                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
703                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
704                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
705                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
706                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
707                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
708                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
709                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
710                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
711                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
712                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
713                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
714                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
715                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
716                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
717                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
718                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
719                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
720                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
721                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
722                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
723                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
724                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
725                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
726                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
727                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
728                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
729                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
730                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
731                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
732                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
733                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
734                 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
735                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.getPluginVersion(), SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
736                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
737                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
738                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
739                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
740                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
741                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css")));
742                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript")));
743                 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png")));
744                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
745                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
746                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
747                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this, timeTextConverter, l10nFilter)));
748                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
749                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
750                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
751                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
752                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
753                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
754                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this, timeTextConverter, l10nFilter)));
755                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
756                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
757                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
758                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
759                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
760                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
761                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
762                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
763                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter, shortenFilter, renderFilter)));
764                 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
765                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
766                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
767                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
768                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
769                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
770                 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
771                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
772                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
773                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
774                 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
775
776                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
777                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
778                 for (PageToadlet toadlet : pageToadlets) {
779                         String menuName = toadlet.getMenuName();
780                         if (menuName != null) {
781                                 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
782                         } else {
783                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
784                         }
785                 }
786         }
787
788         /**
789          * Unregisters all toadlets.
790          */
791         private void unregisterToadlets() {
792                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
793                 for (PageToadlet pageToadlet : pageToadlets) {
794                         toadletContainer.unregister(pageToadlet);
795                 }
796                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
797         }
798
799         /**
800          * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
801          * {@link SonePart}s in the given text (after parsing it using
802          * {@link SoneTextParser}).
803          *
804          * @param text
805          *            The text to parse
806          * @return All mentioned local Sones
807          */
808         private Collection<Sone> getMentionedSones(String text) {
809                 /* we need no context to find mentioned Sones. */
810                 Set<Sone> mentionedSones = new HashSet<Sone>();
811                 for (Part part : soneTextParser.parse(text, null)) {
812                         if (part instanceof SonePart) {
813                                 mentionedSones.add(((SonePart) part).getSone());
814                         }
815                 }
816                 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
817         }
818
819         /**
820          * Returns the Sone insert notification for the given Sone. If no
821          * notification for the given Sone exists, a new notification is created and
822          * cached.
823          *
824          * @param sone
825          *            The Sone to get the insert notification for
826          * @return The Sone insert notification
827          */
828         private TemplateNotification getSoneInsertNotification(Sone sone) {
829                 synchronized (soneInsertNotifications) {
830                         TemplateNotification templateNotification = soneInsertNotifications.get(sone);
831                         if (templateNotification == null) {
832                                 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
833                                 templateNotification.set("insertSone", sone);
834                                 soneInsertNotifications.put(sone, templateNotification);
835                         }
836                         return templateNotification;
837                 }
838         }
839
840         private boolean localSoneMentionedInNewPostOrReply(Post post) {
841                 if (!post.getSone().isLocal()) {
842                         if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
843                                 return true;
844                         }
845                 }
846                 for (PostReply postReply : getCore().getReplies(post.getId())) {
847                         if (postReply.getSone().isLocal()) {
848                                 continue;
849                         }
850                         if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
851                                 return true;
852                         }
853                 }
854                 return false;
855         }
856
857         //
858         // EVENT HANDLERS
859         //
860
861         /**
862          * Notifies the web interface that a new {@link Sone} was found.
863          *
864          * @param newSoneFoundEvent
865          *            The event
866          */
867         @Subscribe
868         public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
869                 newSoneNotification.add(newSoneFoundEvent.sone());
870                 if (!hasFirstStartNotification()) {
871                         notificationManager.addNotification(newSoneNotification);
872                 }
873         }
874
875         /**
876          * Notifies the web interface that a new {@link Post} was found.
877          *
878          * @param newPostFoundEvent
879          *            The event
880          */
881         @Subscribe
882         public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
883                 Post post = newPostFoundEvent.post();
884                 boolean isLocal = post.getSone().isLocal();
885                 if (isLocal) {
886                         localPostNotification.add(post);
887                 } else {
888                         newPostNotification.add(post);
889                 }
890                 if (!hasFirstStartNotification()) {
891                         notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
892                         if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
893                                 mentionNotification.add(post);
894                                 notificationManager.addNotification(mentionNotification);
895                         }
896                 } else {
897                         getCore().markPostKnown(post);
898                 }
899         }
900
901         /**
902          * Notifies the web interface that a new {@link PostReply} was found.
903          *
904          * @param newPostReplyFoundEvent
905          *            The event
906          */
907         @Subscribe
908         public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
909                 PostReply reply = newPostReplyFoundEvent.postReply();
910                 boolean isLocal = reply.getSone().isLocal();
911                 if (isLocal) {
912                         localReplyNotification.add(reply);
913                 } else {
914                         newReplyNotification.add(reply);
915                 }
916                 if (!hasFirstStartNotification()) {
917                         notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
918                         if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
919                                 mentionNotification.add(reply.getPost().get());
920                                 notificationManager.addNotification(mentionNotification);
921                         }
922                 } else {
923                         getCore().markReplyKnown(reply);
924                 }
925         }
926
927         /**
928          * Notifies the web interface that a {@link Sone} was marked as known.
929          *
930          * @param markSoneKnownEvent
931          *            The event
932          */
933         @Subscribe
934         public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
935                 newSoneNotification.remove(markSoneKnownEvent.sone());
936         }
937
938         @Subscribe
939         public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
940                 removePost(markPostKnownEvent.post());
941         }
942
943         @Subscribe
944         public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
945                 removeReply(markPostReplyKnownEvent.postReply());
946         }
947
948         @Subscribe
949         public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
950                 newSoneNotification.remove(soneRemovedEvent.sone());
951         }
952
953         @Subscribe
954         public void postRemoved(PostRemovedEvent postRemovedEvent) {
955                 removePost(postRemovedEvent.post());
956         }
957
958         private void removePost(Post post) {
959                 newPostNotification.remove(post);
960                 localPostNotification.remove(post);
961                 if (!localSoneMentionedInNewPostOrReply(post)) {
962                         mentionNotification.remove(post);
963                 }
964         }
965
966         @Subscribe
967         public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
968                 removeReply(postReplyRemovedEvent.postReply());
969         }
970
971         private void removeReply(PostReply reply) {
972                 newReplyNotification.remove(reply);
973                 localReplyNotification.remove(reply);
974                 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
975                         mentionNotification.remove(reply.getPost().get());
976                 }
977         }
978
979         /**
980          * Notifies the web interface that a Sone was locked.
981          *
982          * @param soneLockedEvent
983          *            The event
984          */
985         @Subscribe
986         public void soneLocked(SoneLockedEvent soneLockedEvent) {
987                 final Sone sone = soneLockedEvent.sone();
988                 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
989
990                         @Override
991                         @SuppressWarnings("synthetic-access")
992                         public void run() {
993                                 lockedSonesNotification.add(sone);
994                                 notificationManager.addNotification(lockedSonesNotification);
995                         }
996                 }, 5, TimeUnit.MINUTES);
997                 lockedSonesTickerObjects.put(sone, tickerObject);
998         }
999
1000         /**
1001          * Notifies the web interface that a Sone was unlocked.
1002          *
1003          * @param soneUnlockedEvent
1004          *            The event
1005          */
1006         @Subscribe
1007         public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
1008                 lockedSonesNotification.remove(soneUnlockedEvent.sone());
1009                 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
1010         }
1011
1012         /**
1013          * Notifies the web interface that a {@link Sone} is being inserted.
1014          *
1015          * @param soneInsertingEvent
1016          *            The event
1017          */
1018         @Subscribe
1019         public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1020                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1021                 soneInsertNotification.set("soneStatus", "inserting");
1022                 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1023                         notificationManager.addNotification(soneInsertNotification);
1024                 }
1025         }
1026
1027         /**
1028          * Notifies the web interface that a {@link Sone} was inserted.
1029          *
1030          * @param soneInsertedEvent
1031          *            The event
1032          */
1033         @Subscribe
1034         public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1035                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1036                 soneInsertNotification.set("soneStatus", "inserted");
1037                 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1038                 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1039                         notificationManager.addNotification(soneInsertNotification);
1040                 }
1041         }
1042
1043         /**
1044          * Notifies the web interface that a {@link Sone} insert was aborted.
1045          *
1046          * @param soneInsertAbortedEvent
1047          *            The event
1048          */
1049         @Subscribe
1050         public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1051                 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1052                 soneInsertNotification.set("soneStatus", "insert-aborted");
1053                 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1054                 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1055                         notificationManager.addNotification(soneInsertNotification);
1056                 }
1057         }
1058
1059         /**
1060          * Notifies the web interface that a new Sone version was found.
1061          *
1062          * @param updateFoundEvent
1063          *            The event
1064          */
1065         @Subscribe
1066         public void updateFound(UpdateFoundEvent updateFoundEvent) {
1067                 newVersionNotification.set("latestVersion", updateFoundEvent.version());
1068                 newVersionNotification.set("latestEdition", updateFoundEvent.latestEdition());
1069                 newVersionNotification.set("releaseTime", updateFoundEvent.releaseTime());
1070                 newVersionNotification.set("disruptive", updateFoundEvent.disruptive());
1071                 notificationManager.addNotification(newVersionNotification);
1072         }
1073
1074         /**
1075          * Notifies the web interface that an image insert was started
1076          *
1077          * @param imageInsertStartedEvent
1078          *            The event
1079          */
1080         @Subscribe
1081         public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1082                 insertingImagesNotification.add(imageInsertStartedEvent.image());
1083                 notificationManager.addNotification(insertingImagesNotification);
1084         }
1085
1086         /**
1087          * Notifies the web interface that an {@link Image} insert was aborted.
1088          *
1089          * @param imageInsertAbortedEvent
1090          *            The event
1091          */
1092         @Subscribe
1093         public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1094                 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1095         }
1096
1097         /**
1098          * Notifies the web interface that an {@link Image} insert is finished.
1099          *
1100          * @param imageInsertFinishedEvent
1101          *            The event
1102          */
1103         @Subscribe
1104         public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1105                 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1106                 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1107                 notificationManager.addNotification(insertedImagesNotification);
1108         }
1109
1110         /**
1111          * Notifies the web interface that an {@link Image} insert has failed.
1112          *
1113          * @param imageInsertFailedEvent
1114          *            The event
1115          */
1116         @Subscribe
1117         public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1118                 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1119                 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1120                 notificationManager.addNotification(imageInsertFailedNotification);
1121         }
1122
1123 }