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