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