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