2 * Sone - WebInterface.java - Copyright © 2010–2019 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
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;
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;
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;
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.Image;
65 import net.pterodactylus.sone.data.Post;
66 import net.pterodactylus.sone.data.PostReply;
67 import net.pterodactylus.sone.data.Sone;
68 import net.pterodactylus.sone.freenet.L10nFilter;
69 import net.pterodactylus.sone.main.Loaders;
70 import net.pterodactylus.sone.main.PluginHomepage;
71 import net.pterodactylus.sone.main.PluginVersion;
72 import net.pterodactylus.sone.main.PluginYear;
73 import net.pterodactylus.sone.main.SonePlugin;
74 import net.pterodactylus.sone.notify.ListNotification;
75 import net.pterodactylus.sone.notify.ListNotificationFilter;
76 import net.pterodactylus.sone.notify.PostVisibilityFilter;
77 import net.pterodactylus.sone.notify.ReplyVisibilityFilter;
78 import net.pterodactylus.sone.template.LinkedElementRenderFilter;
79 import net.pterodactylus.sone.template.ParserFilter;
80 import net.pterodactylus.sone.template.RenderFilter;
81 import net.pterodactylus.sone.template.ShortenFilter;
82 import net.pterodactylus.sone.text.Part;
83 import net.pterodactylus.sone.text.SonePart;
84 import net.pterodactylus.sone.text.SoneTextParser;
85 import net.pterodactylus.sone.text.TimeTextConverter;
86 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
87 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
88 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
89 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
90 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
91 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
92 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
93 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
94 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
95 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
96 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
97 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
98 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
99 import net.pterodactylus.sone.web.ajax.GetLinkedElementAjaxPage;
100 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
101 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
102 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
103 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
104 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
105 import net.pterodactylus.sone.web.ajax.GetTranslationAjaxPage;
106 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
107 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
108 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
109 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
110 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
111 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
112 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
113 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
114 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
115 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
116 import net.pterodactylus.sone.web.page.FreenetRequest;
117 import net.pterodactylus.sone.web.page.PageToadlet;
118 import net.pterodactylus.sone.web.page.PageToadletFactory;
119 import net.pterodactylus.sone.web.pages.AboutPage;
120 import net.pterodactylus.sone.web.pages.BookmarkPage;
121 import net.pterodactylus.sone.web.pages.BookmarksPage;
122 import net.pterodactylus.sone.web.pages.CreateAlbumPage;
123 import net.pterodactylus.sone.web.pages.CreatePostPage;
124 import net.pterodactylus.sone.web.pages.CreateReplyPage;
125 import net.pterodactylus.sone.web.pages.CreateSonePage;
126 import net.pterodactylus.sone.web.pages.DeleteAlbumPage;
127 import net.pterodactylus.sone.web.pages.DeleteImagePage;
128 import net.pterodactylus.sone.web.pages.DeletePostPage;
129 import net.pterodactylus.sone.web.pages.DeleteProfileFieldPage;
130 import net.pterodactylus.sone.web.pages.DeleteReplyPage;
131 import net.pterodactylus.sone.web.pages.DeleteSonePage;
132 import net.pterodactylus.sone.web.pages.DismissNotificationPage;
133 import net.pterodactylus.sone.web.pages.DistrustPage;
134 import net.pterodactylus.sone.web.pages.EditAlbumPage;
135 import net.pterodactylus.sone.web.pages.EditImagePage;
136 import net.pterodactylus.sone.web.pages.EditProfileFieldPage;
137 import net.pterodactylus.sone.web.pages.EditProfilePage;
138 import net.pterodactylus.sone.web.pages.FollowSonePage;
139 import net.pterodactylus.sone.web.pages.GetImagePage;
140 import net.pterodactylus.sone.web.pages.ImageBrowserPage;
141 import net.pterodactylus.sone.web.pages.IndexPage;
142 import net.pterodactylus.sone.web.pages.KnownSonesPage;
143 import net.pterodactylus.sone.web.pages.LikePage;
144 import net.pterodactylus.sone.web.pages.LockSonePage;
145 import net.pterodactylus.sone.web.pages.LoginPage;
146 import net.pterodactylus.sone.web.pages.LogoutPage;
147 import net.pterodactylus.sone.web.pages.MarkAsKnownPage;
148 import net.pterodactylus.sone.web.pages.NewPage;
149 import net.pterodactylus.sone.web.pages.OptionsPage;
150 import net.pterodactylus.sone.web.pages.RescuePage;
151 import net.pterodactylus.sone.web.pages.SearchPage;
152 import net.pterodactylus.sone.web.pages.SoneTemplatePage;
153 import net.pterodactylus.sone.web.pages.TrustPage;
154 import net.pterodactylus.sone.web.pages.UnbookmarkPage;
155 import net.pterodactylus.sone.web.pages.UnfollowSonePage;
156 import net.pterodactylus.sone.web.pages.UnlikePage;
157 import net.pterodactylus.sone.web.pages.UnlockSonePage;
158 import net.pterodactylus.sone.web.pages.UntrustPage;
159 import net.pterodactylus.sone.web.pages.UploadImagePage;
160 import net.pterodactylus.sone.web.pages.ViewPostPage;
161 import net.pterodactylus.sone.web.pages.ViewSonePage;
162 import net.pterodactylus.util.notify.Notification;
163 import net.pterodactylus.util.notify.NotificationManager;
164 import net.pterodactylus.util.notify.TemplateNotification;
165 import net.pterodactylus.util.template.Template;
166 import net.pterodactylus.util.template.TemplateContextFactory;
167 import net.pterodactylus.util.web.RedirectPage;
168 import net.pterodactylus.util.web.TemplatePage;
170 import freenet.clients.http.SessionManager;
171 import freenet.clients.http.SessionManager.Session;
172 import freenet.clients.http.ToadletContainer;
173 import freenet.clients.http.ToadletContext;
174 import freenet.l10n.BaseL10n;
176 import com.google.common.base.Optional;
177 import com.google.common.collect.Collections2;
178 import com.google.common.collect.ImmutableSet;
179 import com.google.common.eventbus.Subscribe;
180 import com.google.inject.Inject;
183 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
184 * references to l10n helpers.
186 public class WebInterface implements SessionProvider {
189 private static final Logger logger = getLogger(WebInterface.class.getName());
191 /** The loaders for templates, pages, and classpath providers. */
192 private final Loaders loaders;
194 /** The notification manager. */
195 private final NotificationManager notificationManager = new NotificationManager();
197 /** The Sone plugin. */
198 private final SonePlugin sonePlugin;
200 /** The registered toadlets. */
201 private final List<PageToadlet> pageToadlets = new ArrayList<>();
203 /** The form password. */
204 private final String formPassword;
206 /** The template context factory. */
207 private final TemplateContextFactory templateContextFactory;
209 /** The Sone text parser. */
210 private final SoneTextParser soneTextParser;
212 /** The parser filter. */
213 private final ParserFilter parserFilter;
214 private final ShortenFilter shortenFilter;
215 private final RenderFilter renderFilter;
217 private final ListNotificationFilter listNotificationFilter;
218 private final PostVisibilityFilter postVisibilityFilter;
219 private final ReplyVisibilityFilter replyVisibilityFilter;
221 private final ElementLoader elementLoader;
222 private final LinkedElementRenderFilter linkedElementRenderFilter;
223 private final TimeTextConverter timeTextConverter = new TimeTextConverter();
224 private final L10nFilter l10nFilter;
226 /** The “new Sone” notification. */
227 private final ListNotification<Sone> newSoneNotification;
229 /** The “new post” notification. */
230 private final ListNotification<Post> newPostNotification;
232 /** The “new reply” notification. */
233 private final ListNotification<PostReply> newReplyNotification;
235 /** The invisible “local post” notification. */
236 private final ListNotification<Post> localPostNotification;
238 /** The invisible “local reply” notification. */
239 private final ListNotification<PostReply> localReplyNotification;
241 /** The “you have been mentioned” notification. */
242 private final ListNotification<Post> mentionNotification;
244 /** Notifications for sone inserts. */
245 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<>();
247 /** Sone locked notification ticker objects. */
248 private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
250 /** The “Sone locked” notification. */
251 private final ListNotification<Sone> lockedSonesNotification;
253 /** The “new version” notification. */
254 private final TemplateNotification newVersionNotification;
256 /** The “inserting images” notification. */
257 private final ListNotification<Image> insertingImagesNotification;
259 /** The “inserted images” notification. */
260 private final ListNotification<Image> insertedImagesNotification;
262 /** The “image insert failed” notification. */
263 private final ListNotification<Image> imageInsertFailedNotification;
265 /** Scheduled executor for time-based notifications. */
266 private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
269 public WebInterface(SonePlugin sonePlugin, Loaders loaders, ListNotificationFilter listNotificationFilter,
270 PostVisibilityFilter postVisibilityFilter, ReplyVisibilityFilter replyVisibilityFilter,
271 ElementLoader elementLoader, TemplateContextFactory templateContextFactory,
272 ParserFilter parserFilter, ShortenFilter shortenFilter,
273 RenderFilter renderFilter,
274 LinkedElementRenderFilter linkedElementRenderFilter) {
275 this.sonePlugin = sonePlugin;
276 this.loaders = loaders;
277 this.listNotificationFilter = listNotificationFilter;
278 this.postVisibilityFilter = postVisibilityFilter;
279 this.replyVisibilityFilter = replyVisibilityFilter;
280 this.elementLoader = elementLoader;
281 this.parserFilter = parserFilter;
282 this.shortenFilter = shortenFilter;
283 this.renderFilter = renderFilter;
284 this.linkedElementRenderFilter = linkedElementRenderFilter;
285 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
286 soneTextParser = new SoneTextParser(getCore(), getCore());
287 l10nFilter = new L10nFilter(getL10n());
289 this.templateContextFactory = templateContextFactory;
290 templateContextFactory.addTemplateObject("webInterface", this);
291 templateContextFactory.addTemplateObject("formPassword", formPassword);
293 /* create notifications. */
294 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
295 newSoneNotification = new ListNotification<>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
297 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
298 newPostNotification = new ListNotification<>("new-post-notification", "posts", newPostNotificationTemplate, false);
300 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
301 localPostNotification = new ListNotification<>("local-post-notification", "posts", localPostNotificationTemplate, false);
303 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
304 newReplyNotification = new ListNotification<>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
306 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
307 localReplyNotification = new ListNotification<>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
309 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
310 mentionNotification = new ListNotification<>("mention-notification", "posts", mentionNotificationTemplate, false);
312 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
313 lockedSonesNotification = new ListNotification<>("sones-locked-notification", "sones", lockedSonesTemplate);
315 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
316 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
318 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
319 insertingImagesNotification = new ListNotification<>("inserting-images-notification", "images", insertingImagesTemplate);
321 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
322 insertedImagesNotification = new ListNotification<>("inserted-images-notification", "images", insertedImagesTemplate);
324 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
325 imageInsertFailedNotification = new ListNotification<>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
333 * Returns the Sone core used by the Sone plugin.
335 * @return The Sone core
338 public Core getCore() {
339 return sonePlugin.core();
343 * Returns the template context factory of the web interface.
345 * @return The template context factory
347 public TemplateContextFactory getTemplateContextFactory() {
348 return templateContextFactory;
351 private Session getCurrentSessionWithoutCreation(ToadletContext toadletContenxt) {
352 return getSessionManager().useSession(toadletContenxt);
355 private Session getOrCreateCurrentSession(ToadletContext toadletContenxt) {
356 Session session = getCurrentSessionWithoutCreation(toadletContenxt);
357 if (session == null) {
358 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
363 public Sone getCurrentSoneCreatingSession(ToadletContext toadletContext) {
364 Collection<Sone> localSones = getCore().getLocalSones();
365 if (localSones.size() == 1) {
366 return localSones.iterator().next();
368 return getCurrentSone(getOrCreateCurrentSession(toadletContext));
371 public Sone getCurrentSoneWithoutCreatingSession(ToadletContext toadletContext) {
372 Collection<Sone> localSones = getCore().getLocalSones();
373 if (localSones.size() == 1) {
374 return localSones.iterator().next();
376 return getCurrentSone(getCurrentSessionWithoutCreation(toadletContext));
380 * Returns the currently logged in Sone.
384 * @return The currently logged in Sone, or {@code null} if no Sone is
385 * currently logged in
387 private Sone getCurrentSone(Session session) {
388 if (session == null) {
391 String soneId = (String) session.getAttribute("Sone.CurrentSone");
392 if (soneId == null) {
395 return getCore().getLocalSone(soneId);
400 public Sone getCurrentSone(@Nonnull ToadletContext toadletContext, boolean createSession) {
401 return createSession ? getCurrentSoneCreatingSession(toadletContext) : getCurrentSoneWithoutCreatingSession(toadletContext);
405 * Sets the currently logged in Sone.
407 * @param toadletContext
408 * The toadlet context
410 * The Sone to set as currently logged in
413 public void setCurrentSone(@Nonnull ToadletContext toadletContext, @Nullable Sone sone) {
414 Session session = getOrCreateCurrentSession(toadletContext);
416 session.removeAttribute("Sone.CurrentSone");
418 session.setAttribute("Sone.CurrentSone", sone.getId());
423 * Returns the notification manager.
425 * @return The notification manager
427 public NotificationManager getNotifications() {
428 return notificationManager;
432 public Optional<Notification> getNotification(@Nonnull String notificationId) {
433 return Optional.fromNullable(notificationManager.getNotification(notificationId));
437 public Collection<Notification> getNotifications(@Nullable Sone currentSone) {
438 return listNotificationFilter.filterNotifications(notificationManager.getNotifications(), currentSone);
442 * Returns the l10n helper of the node.
444 * @return The node’s l10n helper
446 public BaseL10n getL10n() {
447 return sonePlugin.l10n().getBase();
451 * Returns the session manager of the node.
453 * @return The node’s session manager
455 public SessionManager getSessionManager() {
456 return sonePlugin.pluginRespirator().getSessionManager("Sone");
460 * Returns the node’s form password.
462 * @return The form password
464 public String getFormPassword() {
469 * Returns the posts that have been announced as new in the
470 * {@link #newPostNotification}.
472 * @return The new posts
474 public Set<Post> getNewPosts() {
475 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
479 public Collection<Post> getNewPosts(@Nullable Sone currentSone) {
480 Set<Post> allNewPosts = ImmutableSet.<Post> builder()
481 .addAll(newPostNotification.getElements())
482 .addAll(localPostNotification.getElements())
484 return from(allNewPosts).filter(postVisibilityFilter.isVisible(currentSone)).toSet();
488 * Returns the replies that have been announced as new in the
489 * {@link #newReplyNotification}.
491 * @return The new replies
493 public Set<PostReply> getNewReplies() {
494 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
498 public Collection<PostReply> getNewReplies(@Nullable Sone currentSone) {
499 Set<PostReply> allNewReplies = ImmutableSet.<PostReply>builder()
500 .addAll(newReplyNotification.getElements())
501 .addAll(localReplyNotification.getElements())
503 return from(allNewReplies).filter(replyVisibilityFilter.isVisible(currentSone)).toSet();
507 * Sets whether the current start of the plugin is the first start. It is
508 * considered a first start if the configuration file does not exist.
511 * {@code true} if no configuration file existed when Sone was
512 * loaded, {@code false} otherwise
514 public void setFirstStart(boolean firstStart) {
516 Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
517 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
518 notificationManager.addNotification(firstStartNotification);
523 * Sets whether Sone was started with a fresh configuration file.
526 * {@code true} if Sone was started with a fresh configuration,
527 * {@code false} if the existing configuration could be read
529 public void setNewConfig(boolean newConfig) {
530 if (newConfig && !hasFirstStartNotification()) {
531 Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
532 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
533 notificationManager.addNotification(configNotReadNotification);
542 * Returns whether the first start notification is currently displayed.
544 * @return {@code true} if the first-start notification is currently
545 * displayed, {@code false} otherwise
547 private boolean hasFirstStartNotification() {
548 return notificationManager.getNotification("first-start-notification") != null;
556 * Starts the web interface and registers all toadlets.
558 public void start() {
561 /* notification templates. */
562 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
564 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
565 notificationManager.addNotification(startupNotification);
567 ticker.schedule(new Runnable() {
571 startupNotification.dismiss();
573 }, 2, TimeUnit.MINUTES);
575 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
576 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
577 ticker.scheduleAtFixedRate(new Runnable() {
580 @SuppressWarnings("synthetic-access")
582 if (getCore().getIdentityManager().isConnected()) {
583 wotMissingNotification.dismiss();
585 notificationManager.addNotification(wotMissingNotification);
589 }, 15, 15, TimeUnit.SECONDS);
593 * Stops the web interface and unregisters all toadlets.
596 unregisterToadlets();
597 ticker.shutdownNow();
605 * Register all toadlets.
607 private void registerToadlets() {
608 Template emptyTemplate = parse(new StringReader(""));
609 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
610 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
611 Template newTemplate = loaders.loadTemplate("/templates/new.html");
612 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
613 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
614 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
615 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
616 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
617 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
618 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
619 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
620 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
621 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
622 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
623 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
624 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
625 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
626 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
627 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
628 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
629 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
630 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
631 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
632 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
633 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
634 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
635 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
636 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
637 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
638 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
639 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
641 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this, postVisibilityFilter), "Index"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, new PluginVersion(SonePlugin.getPluginVersion()), new PluginYear(sonePlugin.getYear()), new PluginHomepage(sonePlugin.getHomepage())), "About"));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", this, noPermissionTemplate, "Page.NoPermission.Title")));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", this, emptyImageTitleTemplate, "Page.EmptyImageTitle.Title")));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", this, emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title")));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", this, invalidTemplate, "Page.Invalid.Title")));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css")));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript")));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png")));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this, elementLoader, timeTextConverter, l10nFilter, TimeZone.getDefault())));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLinkedElementAjaxPage(this, elementLoader, linkedElementRenderFilter)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this, timeTextConverter, l10nFilter, TimeZone.getDefault())));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter, shortenFilter, renderFilter)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
716 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
717 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
718 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
719 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
720 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
721 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
722 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
724 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
725 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
726 for (PageToadlet toadlet : pageToadlets) {
727 String menuName = toadlet.getMenuName();
728 if (menuName != null) {
729 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
731 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
737 * Unregisters all toadlets.
739 private void unregisterToadlets() {
740 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
741 for (PageToadlet pageToadlet : pageToadlets) {
742 toadletContainer.unregister(pageToadlet);
744 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
748 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
749 * {@link SonePart}s in the given text (after parsing it using
750 * {@link SoneTextParser}).
754 * @return All mentioned local Sones
756 private Collection<Sone> getMentionedSones(String text) {
757 /* we need no context to find mentioned Sones. */
758 Set<Sone> mentionedSones = new HashSet<>();
759 for (Part part : soneTextParser.parse(text, null)) {
760 if (part instanceof SonePart) {
761 mentionedSones.add(((SonePart) part).getSone());
764 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
768 * Returns the Sone insert notification for the given Sone. If no
769 * notification for the given Sone exists, a new notification is created and
773 * The Sone to get the insert notification for
774 * @return The Sone insert notification
776 private TemplateNotification getSoneInsertNotification(Sone sone) {
777 synchronized (soneInsertNotifications) {
778 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
779 if (templateNotification == null) {
780 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
781 templateNotification.set("insertSone", sone);
782 soneInsertNotifications.put(sone, templateNotification);
784 return templateNotification;
788 private boolean localSoneMentionedInNewPostOrReply(Post post) {
789 if (!post.getSone().isLocal()) {
790 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
794 for (PostReply postReply : getCore().getReplies(post.getId())) {
795 if (postReply.getSone().isLocal()) {
798 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
810 * Notifies the web interface that a new {@link Sone} was found.
812 * @param newSoneFoundEvent
816 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
817 newSoneNotification.add(newSoneFoundEvent.sone());
818 if (!hasFirstStartNotification()) {
819 notificationManager.addNotification(newSoneNotification);
824 * Notifies the web interface that a new {@link Post} was found.
826 * @param newPostFoundEvent
830 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
831 Post post = newPostFoundEvent.post();
832 boolean isLocal = post.getSone().isLocal();
834 localPostNotification.add(post);
836 newPostNotification.add(post);
838 if (!hasFirstStartNotification()) {
839 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
840 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
841 mentionNotification.add(post);
842 notificationManager.addNotification(mentionNotification);
845 getCore().markPostKnown(post);
850 * Notifies the web interface that a new {@link PostReply} was found.
852 * @param newPostReplyFoundEvent
856 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
857 PostReply reply = newPostReplyFoundEvent.postReply();
858 boolean isLocal = reply.getSone().isLocal();
860 localReplyNotification.add(reply);
862 newReplyNotification.add(reply);
864 if (!hasFirstStartNotification()) {
865 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
866 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
867 mentionNotification.add(reply.getPost().get());
868 notificationManager.addNotification(mentionNotification);
871 getCore().markReplyKnown(reply);
876 * Notifies the web interface that a {@link Sone} was marked as known.
878 * @param markSoneKnownEvent
882 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
883 newSoneNotification.remove(markSoneKnownEvent.sone());
887 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
888 removePost(markPostKnownEvent.post());
892 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
893 removeReply(markPostReplyKnownEvent.postReply());
897 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
898 newSoneNotification.remove(soneRemovedEvent.sone());
902 public void postRemoved(PostRemovedEvent postRemovedEvent) {
903 removePost(postRemovedEvent.post());
906 private void removePost(Post post) {
907 newPostNotification.remove(post);
908 localPostNotification.remove(post);
909 if (!localSoneMentionedInNewPostOrReply(post)) {
910 mentionNotification.remove(post);
915 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
916 removeReply(postReplyRemovedEvent.postReply());
919 private void removeReply(PostReply reply) {
920 newReplyNotification.remove(reply);
921 localReplyNotification.remove(reply);
922 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
923 mentionNotification.remove(reply.getPost().get());
928 * Notifies the web interface that a Sone was locked.
930 * @param soneLockedEvent
934 public void soneLocked(SoneLockedEvent soneLockedEvent) {
935 final Sone sone = soneLockedEvent.sone();
936 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
939 @SuppressWarnings("synthetic-access")
941 lockedSonesNotification.add(sone);
942 notificationManager.addNotification(lockedSonesNotification);
944 }, 5, TimeUnit.MINUTES);
945 lockedSonesTickerObjects.put(sone, tickerObject);
949 * Notifies the web interface that a Sone was unlocked.
951 * @param soneUnlockedEvent
955 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
956 lockedSonesNotification.remove(soneUnlockedEvent.sone());
957 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
961 * Notifies the web interface that a {@link Sone} is being inserted.
963 * @param soneInsertingEvent
967 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
968 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
969 soneInsertNotification.set("soneStatus", "inserting");
970 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
971 notificationManager.addNotification(soneInsertNotification);
976 * Notifies the web interface that a {@link Sone} was inserted.
978 * @param soneInsertedEvent
982 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
983 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
984 soneInsertNotification.set("soneStatus", "inserted");
985 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
986 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
987 notificationManager.addNotification(soneInsertNotification);
992 * Notifies the web interface that a {@link Sone} insert was aborted.
994 * @param soneInsertAbortedEvent
998 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
999 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1000 soneInsertNotification.set("soneStatus", "insert-aborted");
1001 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1002 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1003 notificationManager.addNotification(soneInsertNotification);
1008 * Notifies the web interface that a new Sone version was found.
1010 * @param updateFoundEvent
1014 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1015 newVersionNotification.set("latestVersion", updateFoundEvent.version());
1016 newVersionNotification.set("latestEdition", updateFoundEvent.latestEdition());
1017 newVersionNotification.set("releaseTime", updateFoundEvent.releaseTime());
1018 newVersionNotification.set("disruptive", updateFoundEvent.disruptive());
1019 notificationManager.addNotification(newVersionNotification);
1023 * Notifies the web interface that an image insert was started
1025 * @param imageInsertStartedEvent
1029 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1030 insertingImagesNotification.add(imageInsertStartedEvent.image());
1031 notificationManager.addNotification(insertingImagesNotification);
1035 * Notifies the web interface that an {@link Image} insert was aborted.
1037 * @param imageInsertAbortedEvent
1041 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1042 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1046 * Notifies the web interface that an {@link Image} insert is finished.
1048 * @param imageInsertFinishedEvent
1052 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1053 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1054 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1055 notificationManager.addNotification(insertedImagesNotification);
1059 * Notifies the web interface that an {@link Image} insert has failed.
1061 * @param imageInsertFailedEvent
1065 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1066 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1067 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1068 notificationManager.addNotification(imageInsertFailedNotification);