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