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