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