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