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