2 * Sone - WebInterface.java - Copyright © 2010–2012 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 java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.UUID;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import net.pterodactylus.sone.core.Core;
39 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
40 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
41 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
42 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
43 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
44 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
45 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
46 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
47 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
48 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
49 import net.pterodactylus.sone.core.event.PostRemovedEvent;
50 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
51 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
52 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
53 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
54 import net.pterodactylus.sone.core.event.SoneLockedEvent;
55 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
56 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
57 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
58 import net.pterodactylus.sone.data.Album;
59 import net.pterodactylus.sone.data.Image;
60 import net.pterodactylus.sone.data.Post;
61 import net.pterodactylus.sone.data.PostReply;
62 import net.pterodactylus.sone.data.Profile;
63 import net.pterodactylus.sone.data.Reply;
64 import net.pterodactylus.sone.data.Sone;
65 import net.pterodactylus.sone.freenet.L10nFilter;
66 import net.pterodactylus.sone.freenet.wot.Identity;
67 import net.pterodactylus.sone.freenet.wot.Trust;
68 import net.pterodactylus.sone.main.SonePlugin;
69 import net.pterodactylus.sone.notify.ListNotification;
70 import net.pterodactylus.sone.template.AlbumAccessor;
71 import net.pterodactylus.sone.template.CollectionAccessor;
72 import net.pterodactylus.sone.template.CssClassNameFilter;
73 import net.pterodactylus.sone.template.HttpRequestAccessor;
74 import net.pterodactylus.sone.template.IdentityAccessor;
75 import net.pterodactylus.sone.template.ImageAccessor;
76 import net.pterodactylus.sone.template.ImageLinkFilter;
77 import net.pterodactylus.sone.template.JavascriptFilter;
78 import net.pterodactylus.sone.template.ParserFilter;
79 import net.pterodactylus.sone.template.PostAccessor;
80 import net.pterodactylus.sone.template.ProfileAccessor;
81 import net.pterodactylus.sone.template.ReplyAccessor;
82 import net.pterodactylus.sone.template.ReplyGroupFilter;
83 import net.pterodactylus.sone.template.RequestChangeFilter;
84 import net.pterodactylus.sone.template.SoneAccessor;
85 import net.pterodactylus.sone.template.SubstringFilter;
86 import net.pterodactylus.sone.template.TrustAccessor;
87 import net.pterodactylus.sone.template.UniqueElementFilter;
88 import net.pterodactylus.sone.template.UnknownDateFilter;
89 import net.pterodactylus.sone.text.Part;
90 import net.pterodactylus.sone.text.SonePart;
91 import net.pterodactylus.sone.text.SoneTextParser;
92 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
93 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
94 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
95 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
96 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
97 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
98 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
99 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
100 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
101 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
102 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
103 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
104 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
105 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
106 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
107 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
108 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
109 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
110 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
111 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
112 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
113 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
114 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
115 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
116 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
117 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
118 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
119 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
120 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
121 import net.pterodactylus.sone.web.page.FreenetRequest;
122 import net.pterodactylus.sone.web.page.PageToadlet;
123 import net.pterodactylus.sone.web.page.PageToadletFactory;
124 import net.pterodactylus.util.logging.Logging;
125 import net.pterodactylus.util.notify.Notification;
126 import net.pterodactylus.util.notify.NotificationManager;
127 import net.pterodactylus.util.notify.TemplateNotification;
128 import net.pterodactylus.util.template.ClassPathTemplateProvider;
129 import net.pterodactylus.util.template.CollectionSortFilter;
130 import net.pterodactylus.util.template.ContainsFilter;
131 import net.pterodactylus.util.template.DateFilter;
132 import net.pterodactylus.util.template.FormatFilter;
133 import net.pterodactylus.util.template.HtmlFilter;
134 import net.pterodactylus.util.template.MatchFilter;
135 import net.pterodactylus.util.template.ModFilter;
136 import net.pterodactylus.util.template.PaginationFilter;
137 import net.pterodactylus.util.template.ReflectionAccessor;
138 import net.pterodactylus.util.template.ReplaceFilter;
139 import net.pterodactylus.util.template.StoreFilter;
140 import net.pterodactylus.util.template.Template;
141 import net.pterodactylus.util.template.TemplateContextFactory;
142 import net.pterodactylus.util.template.TemplateParser;
143 import net.pterodactylus.util.template.TemplateProvider;
144 import net.pterodactylus.util.template.XmlFilter;
145 import net.pterodactylus.util.thread.Ticker;
146 import net.pterodactylus.util.web.RedirectPage;
147 import net.pterodactylus.util.web.StaticPage;
148 import net.pterodactylus.util.web.TemplatePage;
150 import com.google.common.collect.Collections2;
151 import com.google.common.collect.ImmutableSet;
152 import com.google.common.eventbus.Subscribe;
153 import com.google.inject.Inject;
155 import freenet.clients.http.SessionManager;
156 import freenet.clients.http.SessionManager.Session;
157 import freenet.clients.http.ToadletContainer;
158 import freenet.clients.http.ToadletContext;
159 import freenet.l10n.BaseL10n;
160 import freenet.support.api.HTTPRequest;
163 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
164 * references to l10n helpers.
166 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
168 public class WebInterface {
171 private static final Logger logger = Logging.getLogger(WebInterface.class);
173 /** The notification manager. */
174 private final NotificationManager notificationManager = new NotificationManager();
176 /** The Sone plugin. */
177 private final SonePlugin sonePlugin;
179 /** The registered toadlets. */
180 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
182 /** The form password. */
183 private final String formPassword;
185 /** The template context factory. */
186 private final TemplateContextFactory templateContextFactory;
188 /** The Sone text parser. */
189 private final SoneTextParser soneTextParser;
191 /** The parser filter. */
192 private final ParserFilter parserFilter;
194 /** The “new Sone” notification. */
195 private final ListNotification<Sone> newSoneNotification;
197 /** The “new post” notification. */
198 private final ListNotification<Post> newPostNotification;
200 /** The “new reply” notification. */
201 private final ListNotification<PostReply> newReplyNotification;
203 /** The invisible “local post” notification. */
204 private final ListNotification<Post> localPostNotification;
206 /** The invisible “local reply” notification. */
207 private final ListNotification<PostReply> localReplyNotification;
209 /** The “you have been mentioned” notification. */
210 private final ListNotification<Post> mentionNotification;
212 /** Notifications for sone inserts. */
213 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
215 /** Sone locked notification ticker objects. */
216 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
218 /** The “Sone locked” notification. */
219 private final ListNotification<Sone> lockedSonesNotification;
221 /** The “new version” notification. */
222 private final TemplateNotification newVersionNotification;
224 /** The “inserting images” notification. */
225 private final ListNotification<Image> insertingImagesNotification;
227 /** The “inserted images” notification. */
228 private final ListNotification<Image> insertedImagesNotification;
230 /** The “image insert failed” notification. */
231 private final ListNotification<Image> imageInsertFailedNotification;
234 * Creates a new web interface.
240 public WebInterface(SonePlugin sonePlugin) {
241 this.sonePlugin = sonePlugin;
242 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
243 soneTextParser = new SoneTextParser(getCore(), getCore());
245 templateContextFactory = new TemplateContextFactory();
246 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
247 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
248 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
249 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
250 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
251 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
252 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
253 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
254 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
255 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
256 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
257 templateContextFactory.addFilter("date", new DateFilter());
258 templateContextFactory.addFilter("html", new HtmlFilter());
259 templateContextFactory.addFilter("replace", new ReplaceFilter());
260 templateContextFactory.addFilter("store", new StoreFilter());
261 templateContextFactory.addFilter("l10n", new L10nFilter(this));
262 templateContextFactory.addFilter("substring", new SubstringFilter());
263 templateContextFactory.addFilter("xml", new XmlFilter());
264 templateContextFactory.addFilter("change", new RequestChangeFilter());
265 templateContextFactory.addFilter("match", new MatchFilter());
266 templateContextFactory.addFilter("css", new CssClassNameFilter());
267 templateContextFactory.addFilter("js", new JavascriptFilter());
268 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
269 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
270 templateContextFactory.addFilter("format", new FormatFilter());
271 templateContextFactory.addFilter("sort", new CollectionSortFilter());
272 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
273 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
274 templateContextFactory.addFilter("in", new ContainsFilter());
275 templateContextFactory.addFilter("unique", new UniqueElementFilter());
276 templateContextFactory.addFilter("mod", new ModFilter());
277 templateContextFactory.addFilter("paginate", new PaginationFilter());
278 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
279 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
280 templateContextFactory.addTemplateObject("webInterface", this);
281 templateContextFactory.addTemplateObject("formPassword", formPassword);
283 /* create notifications. */
284 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
285 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
287 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
288 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
290 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
291 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
293 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
294 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
296 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
297 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
299 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
300 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
302 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
303 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
305 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
306 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
308 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
309 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
311 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
312 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
314 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
315 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
323 * Returns the Sone core used by the Sone plugin.
325 * @return The Sone core
327 public Core getCore() {
328 return sonePlugin.core();
332 * Returns the template context factory of the web interface.
334 * @return The template context factory
336 public TemplateContextFactory getTemplateContextFactory() {
337 return templateContextFactory;
341 * Returns the current session, creating a new session if there is no
344 * @param toadletContenxt
345 * The toadlet context
346 * @return The current session, or {@code null} if there is no current
349 public Session getCurrentSession(ToadletContext toadletContenxt) {
350 return getCurrentSession(toadletContenxt, true);
354 * Returns the current session, creating a new session if there is no
355 * current session and {@code create} is {@code true}.
357 * @param toadletContenxt
358 * The toadlet context
360 * {@code true} to create a new session if there is no current
361 * session, {@code false} otherwise
362 * @return The current session, or {@code null} if there is no current
365 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
366 Session session = getSessionManager().useSession(toadletContenxt);
367 if (create && (session == null)) {
368 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
374 * Returns the currently logged in Sone.
376 * @param toadletContext
377 * The toadlet context
378 * @return The currently logged in Sone, or {@code null} if no Sone is
379 * currently logged in
381 public Sone getCurrentSone(ToadletContext toadletContext) {
382 return getCurrentSone(toadletContext, true);
386 * Returns the currently logged in Sone.
388 * @param toadletContext
389 * The toadlet context
391 * {@code true} to create a new session if no session exists,
392 * {@code false} to not create a new session
393 * @return The currently logged in Sone, or {@code null} if no Sone is
394 * currently logged in
396 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
397 Collection<Sone> localSones = getCore().getLocalSones();
398 if (localSones.size() == 1) {
399 return localSones.iterator().next();
401 return getCurrentSone(getCurrentSession(toadletContext, create));
405 * Returns the currently logged in Sone.
409 * @return The currently logged in Sone, or {@code null} if no Sone is
410 * currently logged in
412 public Sone getCurrentSone(Session session) {
413 if (session == null) {
416 String soneId = (String) session.getAttribute("Sone.CurrentSone");
417 if (soneId == null) {
420 return getCore().getLocalSone(soneId, false);
424 * Sets the currently logged in Sone.
426 * @param toadletContext
427 * The toadlet context
429 * The Sone to set as currently logged in
431 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
432 Session session = getCurrentSession(toadletContext);
434 session.removeAttribute("Sone.CurrentSone");
436 session.setAttribute("Sone.CurrentSone", sone.getId());
441 * Returns the notification manager.
443 * @return The notification manager
445 public NotificationManager getNotifications() {
446 return notificationManager;
450 * Returns the l10n helper of the node.
452 * @return The node’s l10n helper
454 public BaseL10n getL10n() {
455 return sonePlugin.l10n().getBase();
459 * Returns the session manager of the node.
461 * @return The node’s session manager
463 public SessionManager getSessionManager() {
464 return sonePlugin.pluginRespirator().getSessionManager("Sone");
468 * Returns the node’s form password.
470 * @return The form password
472 public String getFormPassword() {
477 * Returns the posts that have been announced as new in the
478 * {@link #newPostNotification}.
480 * @return The new posts
482 public Set<Post> getNewPosts() {
483 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
487 * Returns the replies that have been announced as new in the
488 * {@link #newReplyNotification}.
490 * @return The new replies
492 public Set<PostReply> getNewReplies() {
493 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
497 * Sets whether the current start of the plugin is the first start. It is
498 * considered a first start if the configuration file does not exist.
501 * {@code true} if no configuration file existed when Sone was
502 * loaded, {@code false} otherwise
504 public void setFirstStart(boolean firstStart) {
506 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
507 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
508 notificationManager.addNotification(firstStartNotification);
513 * Sets whether Sone was started with a fresh configuration file.
516 * {@code true} if Sone was started with a fresh configuration,
517 * {@code false} if the existing configuration could be read
519 public void setNewConfig(boolean newConfig) {
520 if (newConfig && !hasFirstStartNotification()) {
521 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
522 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
523 notificationManager.addNotification(configNotReadNotification);
532 * Returns whether the first start notification is currently displayed.
534 * @return {@code true} if the first-start notification is currently
535 * displayed, {@code false} otherwise
537 private boolean hasFirstStartNotification() {
538 return notificationManager.getNotification("first-start-notification") != null;
546 * Starts the web interface and registers all toadlets.
548 public void start() {
551 /* notification templates. */
552 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
554 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
555 notificationManager.addNotification(startupNotification);
557 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
561 startupNotification.dismiss();
563 }, "Sone Startup Notification Remover");
565 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
566 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
567 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
570 @SuppressWarnings("synthetic-access")
572 if (getCore().getIdentityManager().isConnected()) {
573 wotMissingNotification.dismiss();
575 notificationManager.addNotification(wotMissingNotification);
577 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
580 }, "Sone WoT Connector Checker");
584 * Stops the web interface and unregisters all toadlets.
587 unregisterToadlets();
588 Ticker.getInstance().stop();
596 * Register all toadlets.
598 private void registerToadlets() {
599 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
600 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
601 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
602 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
603 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
604 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
605 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
606 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
607 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
608 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
609 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
610 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
611 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
612 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
613 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
614 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
615 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
616 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
617 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
618 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
619 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
620 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
621 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
622 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
623 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
624 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
625 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
626 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
627 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
628 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
630 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
710 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
711 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
712 for (PageToadlet toadlet : pageToadlets) {
713 String menuName = toadlet.getMenuName();
714 if (menuName != null) {
715 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
717 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
723 * Unregisters all toadlets.
725 private void unregisterToadlets() {
726 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
727 for (PageToadlet pageToadlet : pageToadlets) {
728 toadletContainer.unregister(pageToadlet);
730 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
734 * Creates a {@link Reader} from the {@link InputStream} for the resource
735 * with the given name.
737 * @param resourceName
738 * The name of the resource
739 * @return A {@link Reader} for the resource
741 private Reader createReader(String resourceName) {
743 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
744 } catch (UnsupportedEncodingException uee1) {
750 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
751 * {@link SonePart}s in the given text (after parsing it using
752 * {@link SoneTextParser}).
756 * @return All mentioned local Sones
758 private Collection<Sone> getMentionedSones(String text) {
759 /* we need no context to find mentioned Sones. */
760 Set<Sone> mentionedSones = new HashSet<Sone>();
762 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
763 if (part instanceof SonePart) {
764 mentionedSones.add(((SonePart) part).getSone());
767 } catch (IOException ioe1) {
768 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
770 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
774 * Returns the Sone insert notification for the given Sone. If no
775 * notification for the given Sone exists, a new notification is created and
779 * The Sone to get the insert notification for
780 * @return The Sone insert notification
782 private TemplateNotification getSoneInsertNotification(Sone sone) {
783 synchronized (soneInsertNotifications) {
784 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
785 if (templateNotification == null) {
786 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
787 templateNotification.set("insertSone", sone);
788 soneInsertNotifications.put(sone, templateNotification);
790 return templateNotification;
799 * Notifies the web interface that a new {@link Sone} was found.
801 * @param newSoneFoundEvent
805 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
806 newSoneNotification.add(newSoneFoundEvent.sone());
807 if (!hasFirstStartNotification()) {
808 notificationManager.addNotification(newSoneNotification);
813 * Notifies the web interface that a new {@link Post} was found.
815 * @param newPostFoundEvent
819 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
820 Post post = newPostFoundEvent.post();
821 boolean isLocal = post.getSone().isLocal();
823 localPostNotification.add(post);
825 newPostNotification.add(post);
827 if (!hasFirstStartNotification()) {
828 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
829 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
830 mentionNotification.add(post);
831 notificationManager.addNotification(mentionNotification);
834 getCore().markPostKnown(post);
839 * Notifies the web interface that a new {@link PostReply} was found.
841 * @param newPostReplyFoundEvent
845 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
846 PostReply reply = newPostReplyFoundEvent.postReply();
847 boolean isLocal = reply.getSone().isLocal();
849 localReplyNotification.add(reply);
851 newReplyNotification.add(reply);
853 if (!hasFirstStartNotification()) {
854 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
855 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
856 mentionNotification.add(reply.getPost());
857 notificationManager.addNotification(mentionNotification);
860 getCore().markReplyKnown(reply);
865 * Notifies the web interface that a {@link Sone} was marked as known.
867 * @param markSoneKnownEvent
871 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
872 newSoneNotification.remove(markSoneKnownEvent.sone());
876 * Notifies the web interface that a {@link Post} was marked as known.
878 * @param markPostKnownEvent
882 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
883 newPostNotification.remove(markPostKnownEvent.post());
884 localPostNotification.remove(markPostKnownEvent.post());
885 mentionNotification.remove(markPostKnownEvent.post());
889 * Notifies the web interface that a {@link PostReply} was marked as known.
891 * @param markPostReplyKnownEvent
895 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
896 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
897 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
898 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
902 * Notifies the web interface that a {@link Sone} was removed.
904 * @param soneRemovedEvent
908 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
909 newSoneNotification.remove(soneRemovedEvent.sone());
913 * Notifies the web interface that a {@link Post} was removed.
915 * @param postRemovedEvent
919 public void postRemoved(PostRemovedEvent postRemovedEvent) {
920 newPostNotification.remove(postRemovedEvent.post());
921 localPostNotification.remove(postRemovedEvent.post());
922 mentionNotification.remove(postRemovedEvent.post());
926 * Notifies the web interface that a {@link PostReply} was removed.
928 * @param postReplyRemovedEvent
932 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
933 PostReply reply = postReplyRemovedEvent.postReply();
934 newReplyNotification.remove(reply);
935 localReplyNotification.remove(reply);
936 if (!getMentionedSones(reply.getText()).isEmpty()) {
937 boolean isMentioned = false;
938 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
939 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
942 mentionNotification.remove(reply.getPost());
948 * Notifies the web interface that a Sone was locked.
950 * @param soneLockedEvent
954 public void soneLocked(SoneLockedEvent soneLockedEvent) {
955 final Sone sone = soneLockedEvent.sone();
956 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
959 @SuppressWarnings("synthetic-access")
961 lockedSonesNotification.add(sone);
962 lockedSonesTickerObjects.remove(sone);
963 notificationManager.addNotification(lockedSonesNotification);
965 }, "Sone Locked Notification");
966 lockedSonesTickerObjects.put(sone, tickerObject);
970 * Notifies the web interface that a Sone was unlocked.
972 * @param soneUnlockedEvent
976 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
977 lockedSonesNotification.remove(soneUnlockedEvent.sone());
978 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()));
982 * Notifies the web interface that a {@link Sone} is being inserted.
984 * @param soneInsertingEvent
988 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
989 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
990 soneInsertNotification.set("soneStatus", "inserting");
991 if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
992 notificationManager.addNotification(soneInsertNotification);
997 * Notifies the web interface that a {@link Sone} was inserted.
999 * @param soneInsertedEvent
1003 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1004 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1005 soneInsertNotification.set("soneStatus", "inserted");
1006 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1007 if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1008 notificationManager.addNotification(soneInsertNotification);
1013 * Notifies the web interface that a {@link Sone} insert was aborted.
1015 * @param soneInsertAbortedEvent
1019 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1020 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1021 soneInsertNotification.set("soneStatus", "insert-aborted");
1022 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1023 if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1024 notificationManager.addNotification(soneInsertNotification);
1029 * Notifies the web interface that a new Sone version was found.
1031 * @param updateFoundEvent
1035 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1036 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1037 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1038 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1039 notificationManager.addNotification(newVersionNotification);
1043 * Notifies the web interface that an image insert was started
1045 * @param imageInsertStartedEvent
1049 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1050 insertingImagesNotification.add(imageInsertStartedEvent.image());
1051 notificationManager.addNotification(insertingImagesNotification);
1055 * Notifies the web interface that an {@link Image} insert was aborted.
1057 * @param imageInsertAbortedEvent
1061 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1062 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1066 * Notifies the web interface that an {@link Image} insert is finished.
1068 * @param imageInsertFinishedEvent
1072 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1073 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1074 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1075 notificationManager.addNotification(insertedImagesNotification);
1079 * Notifies the web interface that an {@link Image} insert has failed.
1081 * @param imageInsertFailedEvent
1085 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1086 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1087 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1088 notificationManager.addNotification(imageInsertFailedNotification);