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