2 * Sone - WebInterface.java - Copyright © 2010–2013 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 java.util.logging.Logger.getLogger;
21 import static net.pterodactylus.util.template.TemplateParser.parse;
23 import java.io.IOException;
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.UUID;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.ScheduledExecutorService;
36 import java.util.concurrent.ScheduledFuture;
37 import java.util.concurrent.TimeUnit;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
41 import net.pterodactylus.sone.core.Core;
42 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
43 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
44 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
45 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
46 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
47 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
48 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
49 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
50 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
51 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
52 import net.pterodactylus.sone.core.event.PostRemovedEvent;
53 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
54 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
55 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
56 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
57 import net.pterodactylus.sone.core.event.SoneLockedEvent;
58 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
59 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
60 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
61 import net.pterodactylus.sone.data.Album;
62 import net.pterodactylus.sone.data.Image;
63 import net.pterodactylus.sone.data.Post;
64 import net.pterodactylus.sone.data.PostReply;
65 import net.pterodactylus.sone.data.Profile;
66 import net.pterodactylus.sone.data.Reply;
67 import net.pterodactylus.sone.data.Sone;
68 import net.pterodactylus.sone.freenet.L10nFilter;
69 import net.pterodactylus.sone.freenet.wot.Identity;
70 import net.pterodactylus.sone.freenet.wot.Trust;
71 import net.pterodactylus.sone.main.Loaders;
72 import net.pterodactylus.sone.main.ReparseFilter;
73 import net.pterodactylus.sone.main.SonePlugin;
74 import net.pterodactylus.sone.notify.ListNotification;
75 import net.pterodactylus.sone.template.AlbumAccessor;
76 import net.pterodactylus.sone.template.CollectionAccessor;
77 import net.pterodactylus.sone.template.CssClassNameFilter;
78 import net.pterodactylus.sone.template.HttpRequestAccessor;
79 import net.pterodactylus.sone.template.IdentityAccessor;
80 import net.pterodactylus.sone.template.ImageAccessor;
81 import net.pterodactylus.sone.template.ImageLinkFilter;
82 import net.pterodactylus.sone.template.JavascriptFilter;
83 import net.pterodactylus.sone.template.ParserFilter;
84 import net.pterodactylus.sone.template.PostAccessor;
85 import net.pterodactylus.sone.template.ProfileAccessor;
86 import net.pterodactylus.sone.template.ReplyAccessor;
87 import net.pterodactylus.sone.template.ReplyGroupFilter;
88 import net.pterodactylus.sone.template.RequestChangeFilter;
89 import net.pterodactylus.sone.template.SoneAccessor;
90 import net.pterodactylus.sone.template.SubstringFilter;
91 import net.pterodactylus.sone.template.TrustAccessor;
92 import net.pterodactylus.sone.template.UniqueElementFilter;
93 import net.pterodactylus.sone.template.UnknownDateFilter;
94 import net.pterodactylus.sone.text.Part;
95 import net.pterodactylus.sone.text.SonePart;
96 import net.pterodactylus.sone.text.SoneTextParser;
97 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
98 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
99 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
100 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
101 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
102 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
104 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
105 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
106 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
107 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
108 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
109 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
111 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
115 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
116 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
117 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
118 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
119 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
120 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
121 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
122 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
123 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
125 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
126 import net.pterodactylus.sone.web.page.FreenetRequest;
127 import net.pterodactylus.sone.web.page.PageToadlet;
128 import net.pterodactylus.sone.web.page.PageToadletFactory;
129 import net.pterodactylus.util.notify.Notification;
130 import net.pterodactylus.util.notify.NotificationManager;
131 import net.pterodactylus.util.notify.TemplateNotification;
132 import net.pterodactylus.util.template.ClassPathTemplateProvider;
133 import net.pterodactylus.util.template.CollectionSortFilter;
134 import net.pterodactylus.util.template.ContainsFilter;
135 import net.pterodactylus.util.template.DateFilter;
136 import net.pterodactylus.util.template.FormatFilter;
137 import net.pterodactylus.util.template.HtmlFilter;
138 import net.pterodactylus.util.template.MatchFilter;
139 import net.pterodactylus.util.template.ModFilter;
140 import net.pterodactylus.util.template.PaginationFilter;
141 import net.pterodactylus.util.template.ReflectionAccessor;
142 import net.pterodactylus.util.template.ReplaceFilter;
143 import net.pterodactylus.util.template.StoreFilter;
144 import net.pterodactylus.util.template.Template;
145 import net.pterodactylus.util.template.TemplateContextFactory;
146 import net.pterodactylus.util.template.TemplateProvider;
147 import net.pterodactylus.util.template.XmlFilter;
148 import net.pterodactylus.util.web.RedirectPage;
149 import net.pterodactylus.util.web.TemplatePage;
151 import com.google.common.collect.Collections2;
152 import com.google.common.collect.ImmutableSet;
153 import com.google.common.eventbus.Subscribe;
154 import com.google.inject.Inject;
156 import freenet.clients.http.SessionManager;
157 import freenet.clients.http.SessionManager.Session;
158 import freenet.clients.http.ToadletContainer;
159 import freenet.clients.http.ToadletContext;
160 import freenet.l10n.BaseL10n;
161 import freenet.support.api.HTTPRequest;
164 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
165 * references to l10n helpers.
167 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
169 public class WebInterface {
172 private static final Logger logger = getLogger(WebInterface.class.getName());
174 /** The loaders for templates, pages, and classpath providers. */
175 private final Loaders loaders;
177 /** The notification manager. */
178 private final NotificationManager notificationManager = new NotificationManager();
180 /** The Sone plugin. */
181 private final SonePlugin sonePlugin;
183 /** The registered toadlets. */
184 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
186 /** The form password. */
187 private final String formPassword;
189 /** The template context factory. */
190 private final TemplateContextFactory templateContextFactory;
192 /** The Sone text parser. */
193 private final SoneTextParser soneTextParser;
195 /** The parser filter. */
196 private final ParserFilter parserFilter;
198 /** The “new Sone” notification. */
199 private final ListNotification<Sone> newSoneNotification;
201 /** The “new post” notification. */
202 private final ListNotification<Post> newPostNotification;
204 /** The “new reply” notification. */
205 private final ListNotification<PostReply> newReplyNotification;
207 /** The invisible “local post” notification. */
208 private final ListNotification<Post> localPostNotification;
210 /** The invisible “local reply” notification. */
211 private final ListNotification<PostReply> localReplyNotification;
213 /** The “you have been mentioned” notification. */
214 private final ListNotification<Post> mentionNotification;
216 /** Notifications for sone inserts. */
217 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
219 /** Sone locked notification ticker objects. */
220 private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
222 /** The “Sone locked” notification. */
223 private final ListNotification<Sone> lockedSonesNotification;
225 /** The “new version” notification. */
226 private final TemplateNotification newVersionNotification;
228 /** The “inserting images” notification. */
229 private final ListNotification<Image> insertingImagesNotification;
231 /** The “inserted images” notification. */
232 private final ListNotification<Image> insertedImagesNotification;
234 /** The “image insert failed” notification. */
235 private final ListNotification<Image> imageInsertFailedNotification;
237 /** Scheduled executor for time-based notifications. */
238 private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
241 * Creates a new web interface.
247 public WebInterface(SonePlugin sonePlugin, Loaders loaders) {
248 this.sonePlugin = sonePlugin;
249 this.loaders = loaders;
250 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
251 soneTextParser = new SoneTextParser(getCore(), getCore());
253 templateContextFactory = new TemplateContextFactory();
254 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
255 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
256 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
257 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
258 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
259 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
260 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
261 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
262 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
263 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
264 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
265 templateContextFactory.addFilter("date", new DateFilter());
266 templateContextFactory.addFilter("html", new HtmlFilter());
267 templateContextFactory.addFilter("replace", new ReplaceFilter());
268 templateContextFactory.addFilter("store", new StoreFilter());
269 templateContextFactory.addFilter("l10n", new L10nFilter(this));
270 templateContextFactory.addFilter("substring", new SubstringFilter());
271 templateContextFactory.addFilter("xml", new XmlFilter());
272 templateContextFactory.addFilter("change", new RequestChangeFilter());
273 templateContextFactory.addFilter("match", new MatchFilter());
274 templateContextFactory.addFilter("css", new CssClassNameFilter());
275 templateContextFactory.addFilter("js", new JavascriptFilter());
276 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
277 templateContextFactory.addFilter("reparse", new ReparseFilter());
278 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
279 templateContextFactory.addFilter("format", new FormatFilter());
280 templateContextFactory.addFilter("sort", new CollectionSortFilter());
281 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
282 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
283 templateContextFactory.addFilter("in", new ContainsFilter());
284 templateContextFactory.addFilter("unique", new UniqueElementFilter());
285 templateContextFactory.addFilter("mod", new ModFilter());
286 templateContextFactory.addFilter("paginate", new PaginationFilter());
287 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
288 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
289 templateContextFactory.addTemplateObject("webInterface", this);
290 templateContextFactory.addTemplateObject("formPassword", formPassword);
292 /* create notifications. */
293 Template newSoneNotificationTemplate = loaders.loadTemplate("/templates/notify/newSoneNotification.html");
294 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
296 Template newPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
297 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
299 Template localPostNotificationTemplate = loaders.loadTemplate("/templates/notify/newPostNotification.html");
300 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
302 Template newReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
303 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
305 Template localReplyNotificationTemplate = loaders.loadTemplate("/templates/notify/newReplyNotification.html");
306 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
308 Template mentionNotificationTemplate = loaders.loadTemplate("/templates/notify/mentionNotification.html");
309 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
311 Template lockedSonesTemplate = loaders.loadTemplate("/templates/notify/lockedSonesNotification.html");
312 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
314 Template newVersionTemplate = loaders.loadTemplate("/templates/notify/newVersionNotification.html");
315 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
317 Template insertingImagesTemplate = loaders.loadTemplate("/templates/notify/inserting-images-notification.html");
318 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
320 Template insertedImagesTemplate = loaders.loadTemplate("/templates/notify/inserted-images-notification.html");
321 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
323 Template imageInsertFailedTemplate = loaders.loadTemplate("/templates/notify/image-insert-failed-notification.html");
324 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
332 * Returns the Sone core used by the Sone plugin.
334 * @return The Sone core
336 public Core getCore() {
337 return sonePlugin.core();
341 * Returns the template context factory of the web interface.
343 * @return The template context factory
345 public TemplateContextFactory getTemplateContextFactory() {
346 return templateContextFactory;
350 * Returns the current session, creating a new session if there is no
353 * @param toadletContenxt
354 * The toadlet context
355 * @return The current session, or {@code null} if there is no current
358 public Session getCurrentSession(ToadletContext toadletContenxt) {
359 return getCurrentSession(toadletContenxt, true);
363 * Returns the current session, creating a new session if there is no
364 * current session and {@code create} is {@code true}.
366 * @param toadletContenxt
367 * The toadlet context
369 * {@code true} to create a new session if there is no current
370 * session, {@code false} otherwise
371 * @return The current session, or {@code null} if there is no current
374 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
375 Session session = getSessionManager().useSession(toadletContenxt);
376 if (create && (session == null)) {
377 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
383 * Returns the currently logged in Sone.
385 * @param toadletContext
386 * The toadlet context
387 * @return The currently logged in Sone, or {@code null} if no Sone is
388 * currently logged in
390 public Sone getCurrentSone(ToadletContext toadletContext) {
391 return getCurrentSone(toadletContext, true);
395 * Returns the currently logged in Sone.
397 * @param toadletContext
398 * The toadlet context
400 * {@code true} to create a new session if no session exists,
401 * {@code false} to not create a new session
402 * @return The currently logged in Sone, or {@code null} if no Sone is
403 * currently logged in
405 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
406 Collection<Sone> localSones = getCore().getLocalSones();
407 if (localSones.size() == 1) {
408 return localSones.iterator().next();
410 return getCurrentSone(getCurrentSession(toadletContext, create));
414 * Returns the currently logged in Sone.
418 * @return The currently logged in Sone, or {@code null} if no Sone is
419 * currently logged in
421 public Sone getCurrentSone(Session session) {
422 if (session == null) {
425 String soneId = (String) session.getAttribute("Sone.CurrentSone");
426 if (soneId == null) {
429 return getCore().getLocalSone(soneId);
433 * Sets the currently logged in Sone.
435 * @param toadletContext
436 * The toadlet context
438 * The Sone to set as currently logged in
440 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
441 Session session = getCurrentSession(toadletContext);
443 session.removeAttribute("Sone.CurrentSone");
445 session.setAttribute("Sone.CurrentSone", sone.getId());
450 * Returns the notification manager.
452 * @return The notification manager
454 public NotificationManager getNotifications() {
455 return notificationManager;
459 * Returns the l10n helper of the node.
461 * @return The node’s l10n helper
463 public BaseL10n getL10n() {
464 return sonePlugin.l10n().getBase();
468 * Returns the session manager of the node.
470 * @return The node’s session manager
472 public SessionManager getSessionManager() {
473 return sonePlugin.pluginRespirator().getSessionManager("Sone");
477 * Returns the node’s form password.
479 * @return The form password
481 public String getFormPassword() {
486 * Returns the posts that have been announced as new in the
487 * {@link #newPostNotification}.
489 * @return The new posts
491 public Set<Post> getNewPosts() {
492 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
496 * Returns the replies that have been announced as new in the
497 * {@link #newReplyNotification}.
499 * @return The new replies
501 public Set<PostReply> getNewReplies() {
502 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
506 * Sets whether the current start of the plugin is the first start. It is
507 * considered a first start if the configuration file does not exist.
510 * {@code true} if no configuration file existed when Sone was
511 * loaded, {@code false} otherwise
513 public void setFirstStart(boolean firstStart) {
515 Template firstStartNotificationTemplate = loaders.loadTemplate("/templates/notify/firstStartNotification.html");
516 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
517 notificationManager.addNotification(firstStartNotification);
522 * Sets whether Sone was started with a fresh configuration file.
525 * {@code true} if Sone was started with a fresh configuration,
526 * {@code false} if the existing configuration could be read
528 public void setNewConfig(boolean newConfig) {
529 if (newConfig && !hasFirstStartNotification()) {
530 Template configNotReadNotificationTemplate = loaders.loadTemplate("/templates/notify/configNotReadNotification.html");
531 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
532 notificationManager.addNotification(configNotReadNotification);
541 * Returns whether the first start notification is currently displayed.
543 * @return {@code true} if the first-start notification is currently
544 * displayed, {@code false} otherwise
546 private boolean hasFirstStartNotification() {
547 return notificationManager.getNotification("first-start-notification") != null;
555 * Starts the web interface and registers all toadlets.
557 public void start() {
560 /* notification templates. */
561 Template startupNotificationTemplate = loaders.loadTemplate("/templates/notify/startupNotification.html");
563 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
564 notificationManager.addNotification(startupNotification);
566 ticker.schedule(new Runnable() {
570 startupNotification.dismiss();
572 }, 2, TimeUnit.MINUTES);
574 Template wotMissingNotificationTemplate = loaders.loadTemplate("/templates/notify/wotMissingNotification.html");
575 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
576 ticker.scheduleAtFixedRate(new Runnable() {
579 @SuppressWarnings("synthetic-access")
581 if (getCore().getIdentityManager().isConnected()) {
582 wotMissingNotification.dismiss();
584 notificationManager.addNotification(wotMissingNotification);
588 }, 15, 15, TimeUnit.SECONDS);
592 * Stops the web interface and unregisters all toadlets.
595 unregisterToadlets();
596 ticker.shutdownNow();
604 * Register all toadlets.
606 private void registerToadlets() {
607 Template emptyTemplate = parse(new StringReader(""));
608 Template loginTemplate = loaders.loadTemplate("/templates/login.html");
609 Template indexTemplate = loaders.loadTemplate("/templates/index.html");
610 Template newTemplate = loaders.loadTemplate("/templates/new.html");
611 Template knownSonesTemplate = loaders.loadTemplate("/templates/knownSones.html");
612 Template createSoneTemplate = loaders.loadTemplate("/templates/createSone.html");
613 Template createPostTemplate = loaders.loadTemplate("/templates/createPost.html");
614 Template createReplyTemplate = loaders.loadTemplate("/templates/createReply.html");
615 Template bookmarksTemplate = loaders.loadTemplate("/templates/bookmarks.html");
616 Template searchTemplate = loaders.loadTemplate("/templates/search.html");
617 Template editProfileTemplate = loaders.loadTemplate("/templates/editProfile.html");
618 Template editProfileFieldTemplate = loaders.loadTemplate("/templates/editProfileField.html");
619 Template deleteProfileFieldTemplate = loaders.loadTemplate("/templates/deleteProfileField.html");
620 Template viewSoneTemplate = loaders.loadTemplate("/templates/viewSone.html");
621 Template viewPostTemplate = loaders.loadTemplate("/templates/viewPost.html");
622 Template deletePostTemplate = loaders.loadTemplate("/templates/deletePost.html");
623 Template deleteReplyTemplate = loaders.loadTemplate("/templates/deleteReply.html");
624 Template deleteSoneTemplate = loaders.loadTemplate("/templates/deleteSone.html");
625 Template imageBrowserTemplate = loaders.loadTemplate("/templates/imageBrowser.html");
626 Template createAlbumTemplate = loaders.loadTemplate("/templates/createAlbum.html");
627 Template deleteAlbumTemplate = loaders.loadTemplate("/templates/deleteAlbum.html");
628 Template deleteImageTemplate = loaders.loadTemplate("/templates/deleteImage.html");
629 Template noPermissionTemplate = loaders.loadTemplate("/templates/noPermission.html");
630 Template emptyImageTitleTemplate = loaders.loadTemplate("/templates/emptyImageTitle.html");
631 Template emptyAlbumTitleTemplate = loaders.loadTemplate("/templates/emptyAlbumTitle.html");
632 Template optionsTemplate = loaders.loadTemplate("/templates/options.html");
633 Template rescueTemplate = loaders.loadTemplate("/templates/rescue.html");
634 Template aboutTemplate = loaders.loadTemplate("/templates/about.html");
635 Template invalidTemplate = loaders.loadTemplate("/templates/invalid.html");
636 Template postTemplate = loaders.loadTemplate("/templates/include/viewPost.html");
637 Template replyTemplate = loaders.loadTemplate("/templates/include/viewReply.html");
638 Template openSearchTemplate = loaders.loadTemplate("/templates/xml/OpenSearch.xml");
640 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION, SonePlugin.getYear(), SonePlugin.getHomepage()), "About"));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("css/", "/static/css/", "text/css")));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("javascript/", "/static/javascript/", "text/javascript")));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(loaders.<FreenetRequest>loadStaticPage("images/", "/static/images/", "image/png")));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
716 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
717 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
718 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
719 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
720 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
722 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
723 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
724 for (PageToadlet toadlet : pageToadlets) {
725 String menuName = toadlet.getMenuName();
726 if (menuName != null) {
727 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
729 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
735 * Unregisters all toadlets.
737 private void unregisterToadlets() {
738 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
739 for (PageToadlet pageToadlet : pageToadlets) {
740 toadletContainer.unregister(pageToadlet);
742 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
746 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
747 * {@link SonePart}s in the given text (after parsing it using
748 * {@link SoneTextParser}).
752 * @return All mentioned local Sones
754 private Collection<Sone> getMentionedSones(String text) {
755 /* we need no context to find mentioned Sones. */
756 Set<Sone> mentionedSones = new HashSet<Sone>();
758 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
759 if (part instanceof SonePart) {
760 mentionedSones.add(((SonePart) part).getSone());
763 } catch (IOException ioe1) {
764 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
766 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
770 * Returns the Sone insert notification for the given Sone. If no
771 * notification for the given Sone exists, a new notification is created and
775 * The Sone to get the insert notification for
776 * @return The Sone insert notification
778 private TemplateNotification getSoneInsertNotification(Sone sone) {
779 synchronized (soneInsertNotifications) {
780 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
781 if (templateNotification == null) {
782 templateNotification = new TemplateNotification(loaders.loadTemplate("/templates/notify/soneInsertNotification.html"));
783 templateNotification.set("insertSone", sone);
784 soneInsertNotifications.put(sone, templateNotification);
786 return templateNotification;
790 private boolean localSoneMentionedInNewPostOrReply(Post post) {
791 if (!post.getSone().isLocal()) {
792 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
796 for (PostReply postReply : getCore().getReplies(post.getId())) {
797 if (postReply.getSone().isLocal()) {
800 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
812 * Notifies the web interface that a new {@link Sone} was found.
814 * @param newSoneFoundEvent
818 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
819 newSoneNotification.add(newSoneFoundEvent.sone());
820 if (!hasFirstStartNotification()) {
821 notificationManager.addNotification(newSoneNotification);
826 * Notifies the web interface that a new {@link Post} was found.
828 * @param newPostFoundEvent
832 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
833 Post post = newPostFoundEvent.post();
834 boolean isLocal = post.getSone().isLocal();
836 localPostNotification.add(post);
838 newPostNotification.add(post);
840 if (!hasFirstStartNotification()) {
841 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
842 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
843 mentionNotification.add(post);
844 notificationManager.addNotification(mentionNotification);
847 getCore().markPostKnown(post);
852 * Notifies the web interface that a new {@link PostReply} was found.
854 * @param newPostReplyFoundEvent
858 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
859 PostReply reply = newPostReplyFoundEvent.postReply();
860 boolean isLocal = reply.getSone().isLocal();
862 localReplyNotification.add(reply);
864 newReplyNotification.add(reply);
866 if (!hasFirstStartNotification()) {
867 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
868 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
869 mentionNotification.add(reply.getPost().get());
870 notificationManager.addNotification(mentionNotification);
873 getCore().markReplyKnown(reply);
878 * Notifies the web interface that a {@link Sone} was marked as known.
880 * @param markSoneKnownEvent
884 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
885 newSoneNotification.remove(markSoneKnownEvent.sone());
889 * Notifies the web interface that a {@link Post} was marked as known.
891 * @param markPostKnownEvent
895 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
896 newPostNotification.remove(markPostKnownEvent.post());
897 localPostNotification.remove(markPostKnownEvent.post());
898 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
899 mentionNotification.remove(markPostKnownEvent.post());
904 * Notifies the web interface that a {@link PostReply} was marked as known.
906 * @param markPostReplyKnownEvent
910 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
911 PostReply postReply = markPostReplyKnownEvent.postReply();
912 newReplyNotification.remove(postReply);
913 localReplyNotification.remove(postReply);
914 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
915 mentionNotification.remove(postReply.getPost().get());
920 * Notifies the web interface that a {@link Sone} was removed.
922 * @param soneRemovedEvent
926 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
927 newSoneNotification.remove(soneRemovedEvent.sone());
931 * Notifies the web interface that a {@link Post} was removed.
933 * @param postRemovedEvent
937 public void postRemoved(PostRemovedEvent postRemovedEvent) {
938 newPostNotification.remove(postRemovedEvent.post());
939 localPostNotification.remove(postRemovedEvent.post());
940 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
941 mentionNotification.remove(postRemovedEvent.post());
946 * Notifies the web interface that a {@link PostReply} was removed.
948 * @param postReplyRemovedEvent
952 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
953 PostReply reply = postReplyRemovedEvent.postReply();
954 newReplyNotification.remove(reply);
955 localReplyNotification.remove(reply);
956 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
957 mentionNotification.remove(reply.getPost().get());
962 * Notifies the web interface that a Sone was locked.
964 * @param soneLockedEvent
968 public void soneLocked(SoneLockedEvent soneLockedEvent) {
969 final Sone sone = soneLockedEvent.sone();
970 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
973 @SuppressWarnings("synthetic-access")
975 lockedSonesNotification.add(sone);
976 notificationManager.addNotification(lockedSonesNotification);
978 }, 5, TimeUnit.MINUTES);
979 lockedSonesTickerObjects.put(sone, tickerObject);
983 * Notifies the web interface that a Sone was unlocked.
985 * @param soneUnlockedEvent
989 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
990 lockedSonesNotification.remove(soneUnlockedEvent.sone());
991 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
995 * Notifies the web interface that a {@link Sone} is being inserted.
997 * @param soneInsertingEvent
1001 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
1002 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
1003 soneInsertNotification.set("soneStatus", "inserting");
1004 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1005 notificationManager.addNotification(soneInsertNotification);
1010 * Notifies the web interface that a {@link Sone} was inserted.
1012 * @param soneInsertedEvent
1016 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1017 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1018 soneInsertNotification.set("soneStatus", "inserted");
1019 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1020 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1021 notificationManager.addNotification(soneInsertNotification);
1026 * Notifies the web interface that a {@link Sone} insert was aborted.
1028 * @param soneInsertAbortedEvent
1032 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1033 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1034 soneInsertNotification.set("soneStatus", "insert-aborted");
1035 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1036 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1037 notificationManager.addNotification(soneInsertNotification);
1042 * Notifies the web interface that a new Sone version was found.
1044 * @param updateFoundEvent
1048 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1049 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1050 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1051 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1052 notificationManager.addNotification(newVersionNotification);
1056 * Notifies the web interface that an image insert was started
1058 * @param imageInsertStartedEvent
1062 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1063 insertingImagesNotification.add(imageInsertStartedEvent.image());
1064 notificationManager.addNotification(insertingImagesNotification);
1068 * Notifies the web interface that an {@link Image} insert was aborted.
1070 * @param imageInsertAbortedEvent
1074 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1075 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1079 * Notifies the web interface that an {@link Image} insert is finished.
1081 * @param imageInsertFinishedEvent
1085 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1086 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1087 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1088 notificationManager.addNotification(insertedImagesNotification);
1092 * Notifies the web interface that an {@link Image} insert has failed.
1094 * @param imageInsertFailedEvent
1098 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1099 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1100 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1101 notificationManager.addNotification(imageInsertFailedNotification);