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.CoreListener;
40 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
41 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
42 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
43 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
44 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
45 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
46 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
47 import net.pterodactylus.sone.core.event.PostRemovedEvent;
48 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
49 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
50 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
51 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
52 import net.pterodactylus.sone.core.event.SoneLockedEvent;
53 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
54 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
55 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
56 import net.pterodactylus.sone.data.Album;
57 import net.pterodactylus.sone.data.Image;
58 import net.pterodactylus.sone.data.Post;
59 import net.pterodactylus.sone.data.PostReply;
60 import net.pterodactylus.sone.data.Profile;
61 import net.pterodactylus.sone.data.Reply;
62 import net.pterodactylus.sone.data.Sone;
63 import net.pterodactylus.sone.freenet.L10nFilter;
64 import net.pterodactylus.sone.freenet.wot.Identity;
65 import net.pterodactylus.sone.freenet.wot.Trust;
66 import net.pterodactylus.sone.main.SonePlugin;
67 import net.pterodactylus.sone.notify.ListNotification;
68 import net.pterodactylus.sone.template.AlbumAccessor;
69 import net.pterodactylus.sone.template.CollectionAccessor;
70 import net.pterodactylus.sone.template.CssClassNameFilter;
71 import net.pterodactylus.sone.template.HttpRequestAccessor;
72 import net.pterodactylus.sone.template.IdentityAccessor;
73 import net.pterodactylus.sone.template.ImageAccessor;
74 import net.pterodactylus.sone.template.ImageLinkFilter;
75 import net.pterodactylus.sone.template.JavascriptFilter;
76 import net.pterodactylus.sone.template.ParserFilter;
77 import net.pterodactylus.sone.template.PostAccessor;
78 import net.pterodactylus.sone.template.ProfileAccessor;
79 import net.pterodactylus.sone.template.ReplyAccessor;
80 import net.pterodactylus.sone.template.ReplyGroupFilter;
81 import net.pterodactylus.sone.template.RequestChangeFilter;
82 import net.pterodactylus.sone.template.SoneAccessor;
83 import net.pterodactylus.sone.template.SubstringFilter;
84 import net.pterodactylus.sone.template.TrustAccessor;
85 import net.pterodactylus.sone.template.UniqueElementFilter;
86 import net.pterodactylus.sone.template.UnknownDateFilter;
87 import net.pterodactylus.sone.text.Part;
88 import net.pterodactylus.sone.text.SonePart;
89 import net.pterodactylus.sone.text.SoneTextParser;
90 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
91 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
92 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
93 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
94 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
95 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
96 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
97 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
98 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
99 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
100 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
101 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
102 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
103 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
104 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
105 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
106 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
107 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
108 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
109 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
110 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
111 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
112 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
113 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
114 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
115 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
116 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
117 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
118 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
119 import net.pterodactylus.sone.web.page.FreenetRequest;
120 import net.pterodactylus.sone.web.page.PageToadlet;
121 import net.pterodactylus.sone.web.page.PageToadletFactory;
122 import net.pterodactylus.util.logging.Logging;
123 import net.pterodactylus.util.notify.Notification;
124 import net.pterodactylus.util.notify.NotificationManager;
125 import net.pterodactylus.util.notify.TemplateNotification;
126 import net.pterodactylus.util.template.ClassPathTemplateProvider;
127 import net.pterodactylus.util.template.CollectionSortFilter;
128 import net.pterodactylus.util.template.ContainsFilter;
129 import net.pterodactylus.util.template.DateFilter;
130 import net.pterodactylus.util.template.FormatFilter;
131 import net.pterodactylus.util.template.HtmlFilter;
132 import net.pterodactylus.util.template.MatchFilter;
133 import net.pterodactylus.util.template.ModFilter;
134 import net.pterodactylus.util.template.PaginationFilter;
135 import net.pterodactylus.util.template.ReflectionAccessor;
136 import net.pterodactylus.util.template.ReplaceFilter;
137 import net.pterodactylus.util.template.StoreFilter;
138 import net.pterodactylus.util.template.Template;
139 import net.pterodactylus.util.template.TemplateContextFactory;
140 import net.pterodactylus.util.template.TemplateParser;
141 import net.pterodactylus.util.template.TemplateProvider;
142 import net.pterodactylus.util.template.XmlFilter;
143 import net.pterodactylus.util.thread.Ticker;
144 import net.pterodactylus.util.web.RedirectPage;
145 import net.pterodactylus.util.web.StaticPage;
146 import net.pterodactylus.util.web.TemplatePage;
148 import com.google.common.collect.Collections2;
149 import com.google.common.collect.ImmutableSet;
150 import com.google.common.eventbus.Subscribe;
151 import com.google.inject.Inject;
153 import freenet.clients.http.SessionManager;
154 import freenet.clients.http.SessionManager.Session;
155 import freenet.clients.http.ToadletContainer;
156 import freenet.clients.http.ToadletContext;
157 import freenet.l10n.BaseL10n;
158 import freenet.support.api.HTTPRequest;
161 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
162 * references to l10n helpers.
164 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
166 public class WebInterface implements CoreListener {
169 private static final Logger logger = Logging.getLogger(WebInterface.class);
171 /** The notification manager. */
172 private final NotificationManager notificationManager = new NotificationManager();
174 /** The Sone plugin. */
175 private final SonePlugin sonePlugin;
177 /** The registered toadlets. */
178 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
180 /** The form password. */
181 private final String formPassword;
183 /** The template context factory. */
184 private final TemplateContextFactory templateContextFactory;
186 /** The Sone text parser. */
187 private final SoneTextParser soneTextParser;
189 /** The parser filter. */
190 private final ParserFilter parserFilter;
192 /** The “new Sone” notification. */
193 private final ListNotification<Sone> newSoneNotification;
195 /** The “new post” notification. */
196 private final ListNotification<Post> newPostNotification;
198 /** The “new reply” notification. */
199 private final ListNotification<PostReply> newReplyNotification;
201 /** The invisible “local post” notification. */
202 private final ListNotification<Post> localPostNotification;
204 /** The invisible “local reply” notification. */
205 private final ListNotification<PostReply> localReplyNotification;
207 /** The “you have been mentioned” notification. */
208 private final ListNotification<Post> mentionNotification;
210 /** Notifications for sone inserts. */
211 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
213 /** Sone locked notification ticker objects. */
214 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
216 /** The “Sone locked” notification. */
217 private final ListNotification<Sone> lockedSonesNotification;
219 /** The “new version” notification. */
220 private final TemplateNotification newVersionNotification;
222 /** The “inserting images” notification. */
223 private final ListNotification<Image> insertingImagesNotification;
225 /** The “inserted images” notification. */
226 private final ListNotification<Image> insertedImagesNotification;
228 /** The “image insert failed” notification. */
229 private final ListNotification<Image> imageInsertFailedNotification;
232 * Creates a new web interface.
238 public WebInterface(SonePlugin sonePlugin) {
239 this.sonePlugin = sonePlugin;
240 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
241 soneTextParser = new SoneTextParser(getCore(), getCore());
243 templateContextFactory = new TemplateContextFactory();
244 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
245 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
246 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
247 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
248 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
249 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
250 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
251 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
252 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
253 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
254 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
255 templateContextFactory.addFilter("date", new DateFilter());
256 templateContextFactory.addFilter("html", new HtmlFilter());
257 templateContextFactory.addFilter("replace", new ReplaceFilter());
258 templateContextFactory.addFilter("store", new StoreFilter());
259 templateContextFactory.addFilter("l10n", new L10nFilter(this));
260 templateContextFactory.addFilter("substring", new SubstringFilter());
261 templateContextFactory.addFilter("xml", new XmlFilter());
262 templateContextFactory.addFilter("change", new RequestChangeFilter());
263 templateContextFactory.addFilter("match", new MatchFilter());
264 templateContextFactory.addFilter("css", new CssClassNameFilter());
265 templateContextFactory.addFilter("js", new JavascriptFilter());
266 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
267 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
268 templateContextFactory.addFilter("format", new FormatFilter());
269 templateContextFactory.addFilter("sort", new CollectionSortFilter());
270 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
271 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
272 templateContextFactory.addFilter("in", new ContainsFilter());
273 templateContextFactory.addFilter("unique", new UniqueElementFilter());
274 templateContextFactory.addFilter("mod", new ModFilter());
275 templateContextFactory.addFilter("paginate", new PaginationFilter());
276 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
277 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
278 templateContextFactory.addTemplateObject("webInterface", this);
279 templateContextFactory.addTemplateObject("formPassword", formPassword);
281 /* create notifications. */
282 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
283 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
285 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
286 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
288 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
289 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
291 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
292 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
294 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
295 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
297 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
298 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
300 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
301 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
303 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
304 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
306 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
307 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
309 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
310 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
312 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
313 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
321 * Returns the Sone core used by the Sone plugin.
323 * @return The Sone core
325 public Core getCore() {
326 return sonePlugin.core();
330 * Returns the template context factory of the web interface.
332 * @return The template context factory
334 public TemplateContextFactory getTemplateContextFactory() {
335 return templateContextFactory;
339 * Returns the current session, creating a new session if there is no
342 * @param toadletContenxt
343 * The toadlet context
344 * @return The current session, or {@code null} if there is no current
347 public Session getCurrentSession(ToadletContext toadletContenxt) {
348 return getCurrentSession(toadletContenxt, true);
352 * Returns the current session, creating a new session if there is no
353 * current session and {@code create} is {@code true}.
355 * @param toadletContenxt
356 * The toadlet context
358 * {@code true} to create a new session if there is no current
359 * session, {@code false} otherwise
360 * @return The current session, or {@code null} if there is no current
363 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
364 Session session = getSessionManager().useSession(toadletContenxt);
365 if (create && (session == null)) {
366 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
372 * Returns the currently logged in Sone.
374 * @param toadletContext
375 * The toadlet context
376 * @return The currently logged in Sone, or {@code null} if no Sone is
377 * currently logged in
379 public Sone getCurrentSone(ToadletContext toadletContext) {
380 return getCurrentSone(toadletContext, true);
384 * Returns the currently logged in Sone.
386 * @param toadletContext
387 * The toadlet context
389 * {@code true} to create a new session if no session exists,
390 * {@code false} to not create a new session
391 * @return The currently logged in Sone, or {@code null} if no Sone is
392 * currently logged in
394 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
395 Collection<Sone> localSones = getCore().getLocalSones();
396 if (localSones.size() == 1) {
397 return localSones.iterator().next();
399 return getCurrentSone(getCurrentSession(toadletContext, create));
403 * Returns the currently logged in Sone.
407 * @return The currently logged in Sone, or {@code null} if no Sone is
408 * currently logged in
410 public Sone getCurrentSone(Session session) {
411 if (session == null) {
414 String soneId = (String) session.getAttribute("Sone.CurrentSone");
415 if (soneId == null) {
418 return getCore().getLocalSone(soneId, false);
422 * Sets the currently logged in Sone.
424 * @param toadletContext
425 * The toadlet context
427 * The Sone to set as currently logged in
429 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
430 Session session = getCurrentSession(toadletContext);
432 session.removeAttribute("Sone.CurrentSone");
434 session.setAttribute("Sone.CurrentSone", sone.getId());
439 * Returns the notification manager.
441 * @return The notification manager
443 public NotificationManager getNotifications() {
444 return notificationManager;
448 * Returns the l10n helper of the node.
450 * @return The node’s l10n helper
452 public BaseL10n getL10n() {
453 return sonePlugin.l10n().getBase();
457 * Returns the session manager of the node.
459 * @return The node’s session manager
461 public SessionManager getSessionManager() {
462 return sonePlugin.pluginRespirator().getSessionManager("Sone");
466 * Returns the node’s form password.
468 * @return The form password
470 public String getFormPassword() {
475 * Returns the posts that have been announced as new in the
476 * {@link #newPostNotification}.
478 * @return The new posts
480 public Set<Post> getNewPosts() {
481 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
485 * Returns the replies that have been announced as new in the
486 * {@link #newReplyNotification}.
488 * @return The new replies
490 public Set<PostReply> getNewReplies() {
491 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
495 * Sets whether the current start of the plugin is the first start. It is
496 * considered a first start if the configuration file does not exist.
499 * {@code true} if no configuration file existed when Sone was
500 * loaded, {@code false} otherwise
502 public void setFirstStart(boolean firstStart) {
504 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
505 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
506 notificationManager.addNotification(firstStartNotification);
511 * Sets whether Sone was started with a fresh configuration file.
514 * {@code true} if Sone was started with a fresh configuration,
515 * {@code false} if the existing configuration could be read
517 public void setNewConfig(boolean newConfig) {
518 if (newConfig && !hasFirstStartNotification()) {
519 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
520 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
521 notificationManager.addNotification(configNotReadNotification);
530 * Returns whether the first start notification is currently displayed.
532 * @return {@code true} if the first-start notification is currently
533 * displayed, {@code false} otherwise
535 private boolean hasFirstStartNotification() {
536 return notificationManager.getNotification("first-start-notification") != null;
544 * Starts the web interface and registers all toadlets.
546 public void start() {
549 /* notification templates. */
550 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
552 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
553 notificationManager.addNotification(startupNotification);
555 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
559 startupNotification.dismiss();
561 }, "Sone Startup Notification Remover");
563 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
564 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
565 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
568 @SuppressWarnings("synthetic-access")
570 if (getCore().getIdentityManager().isConnected()) {
571 wotMissingNotification.dismiss();
573 notificationManager.addNotification(wotMissingNotification);
575 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
578 }, "Sone WoT Connector Checker");
582 * Stops the web interface and unregisters all toadlets.
585 unregisterToadlets();
586 Ticker.getInstance().stop();
594 * Register all toadlets.
596 private void registerToadlets() {
597 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
598 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
599 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
600 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
601 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
602 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
603 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
604 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
605 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
606 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
607 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
608 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
609 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
610 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
611 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
612 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
613 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
614 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
615 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
616 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
617 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
618 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
619 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
620 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
621 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
622 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
623 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
624 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
625 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
626 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
628 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
708 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
709 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
710 for (PageToadlet toadlet : pageToadlets) {
711 String menuName = toadlet.getMenuName();
712 if (menuName != null) {
713 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
715 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
721 * Unregisters all toadlets.
723 private void unregisterToadlets() {
724 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
725 for (PageToadlet pageToadlet : pageToadlets) {
726 toadletContainer.unregister(pageToadlet);
728 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
732 * Creates a {@link Reader} from the {@link InputStream} for the resource
733 * with the given name.
735 * @param resourceName
736 * The name of the resource
737 * @return A {@link Reader} for the resource
739 private Reader createReader(String resourceName) {
741 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
742 } catch (UnsupportedEncodingException uee1) {
748 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
749 * {@link SonePart}s in the given text (after parsing it using
750 * {@link SoneTextParser}).
754 * @return All mentioned local Sones
756 private Collection<Sone> getMentionedSones(String text) {
757 /* we need no context to find mentioned Sones. */
758 Set<Sone> mentionedSones = new HashSet<Sone>();
760 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
761 if (part instanceof SonePart) {
762 mentionedSones.add(((SonePart) part).getSone());
765 } catch (IOException ioe1) {
766 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
768 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
772 * Returns the Sone insert notification for the given Sone. If no
773 * notification for the given Sone exists, a new notification is created and
777 * The Sone to get the insert notification for
778 * @return The Sone insert notification
780 private TemplateNotification getSoneInsertNotification(Sone sone) {
781 synchronized (soneInsertNotifications) {
782 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
783 if (templateNotification == null) {
784 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
785 templateNotification.set("insertSone", sone);
786 soneInsertNotifications.put(sone, templateNotification);
788 return templateNotification;
797 * Notifies the web interface that a new {@link Sone} was found.
799 * @param newSoneFoundEvent
803 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
804 newSoneNotification.add(newSoneFoundEvent.sone());
805 if (!hasFirstStartNotification()) {
806 notificationManager.addNotification(newSoneNotification);
811 * Notifies the web interface that a new {@link Post} was found.
813 * @param newPostFoundEvent
817 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
818 Post post = newPostFoundEvent.post();
819 boolean isLocal = post.getSone().isLocal();
821 localPostNotification.add(post);
823 newPostNotification.add(post);
825 if (!hasFirstStartNotification()) {
826 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
827 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
828 mentionNotification.add(post);
829 notificationManager.addNotification(mentionNotification);
832 getCore().markPostKnown(post);
837 * Notifies the web interface that a new {@link PostReply} was found.
839 * @param newPostReplyFoundEvent
843 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
844 PostReply reply = newPostReplyFoundEvent.postReply();
845 boolean isLocal = reply.getSone().isLocal();
847 localReplyNotification.add(reply);
849 newReplyNotification.add(reply);
851 if (!hasFirstStartNotification()) {
852 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
853 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
854 mentionNotification.add(reply.getPost());
855 notificationManager.addNotification(mentionNotification);
858 getCore().markReplyKnown(reply);
863 * Notifies the web interface that a {@link Sone} was marked as known.
865 * @param markSoneKnownEvent
869 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
870 newSoneNotification.remove(markSoneKnownEvent.sone());
874 * Notifies the web interface that a {@link Post} was marked as known.
876 * @param markPostKnownEvent
880 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
881 newPostNotification.remove(markPostKnownEvent.post());
882 localPostNotification.remove(markPostKnownEvent.post());
883 mentionNotification.remove(markPostKnownEvent.post());
887 * Notifies the web interface that a {@link PostReply} was marked as known.
889 * @param markPostReplyKnownEvent
893 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
894 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
895 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
896 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
900 * Notifies the web interface that a {@link Sone} was removed.
902 * @param soneRemovedEvent
906 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
907 newSoneNotification.remove(soneRemovedEvent.sone());
911 * Notifies the web interface that a {@link Post} was removed.
913 * @param postRemovedEvent
917 public void postRemoved(PostRemovedEvent postRemovedEvent) {
918 newPostNotification.remove(postRemovedEvent.post());
919 localPostNotification.remove(postRemovedEvent.post());
920 mentionNotification.remove(postRemovedEvent.post());
924 * Notifies the web interface that a {@link PostReply} was removed.
926 * @param postReplyRemovedEvent
930 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
931 PostReply reply = postReplyRemovedEvent.postReply();
932 newReplyNotification.remove(reply);
933 localReplyNotification.remove(reply);
934 if (!getMentionedSones(reply.getText()).isEmpty()) {
935 boolean isMentioned = false;
936 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
937 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
940 mentionNotification.remove(reply.getPost());
946 * Notifies the web interface that a Sone was locked.
948 * @param soneLockedEvent
952 public void soneLocked(SoneLockedEvent soneLockedEvent) {
953 final Sone sone = soneLockedEvent.sone();
954 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
957 @SuppressWarnings("synthetic-access")
959 lockedSonesNotification.add(sone);
960 lockedSonesTickerObjects.remove(sone);
961 notificationManager.addNotification(lockedSonesNotification);
963 }, "Sone Locked Notification");
964 lockedSonesTickerObjects.put(sone, tickerObject);
968 * Notifies the web interface that a Sone was unlocked.
970 * @param soneUnlockedEvent
974 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
975 lockedSonesNotification.remove(soneUnlockedEvent.sone());
976 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()));
980 * Notifies the web interface that a {@link Sone} is being inserted.
982 * @param soneInsertingEvent
986 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
987 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
988 soneInsertNotification.set("soneStatus", "inserting");
989 if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
990 notificationManager.addNotification(soneInsertNotification);
995 * Notifies the web interface that a {@link Sone} was inserted.
997 * @param soneInsertedEvent
1001 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1002 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1003 soneInsertNotification.set("soneStatus", "inserted");
1004 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1005 if (soneInsertedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1006 notificationManager.addNotification(soneInsertNotification);
1011 * Notifies the web interface that a {@link Sone} insert was aborted.
1013 * @param soneInsertAbortedEvent
1017 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1018 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1019 soneInsertNotification.set("soneStatus", "insert-aborted");
1020 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1021 if (soneInsertAbortedEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1022 notificationManager.addNotification(soneInsertNotification);
1027 * Notifies the web interface that a new Sone version was found.
1029 * @param updateFoundEvent
1033 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1034 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1035 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1036 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1037 notificationManager.addNotification(newVersionNotification);
1041 * Notifies the web interface that an image insert was started
1043 * @param imageInsertStartedEvent
1047 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1048 insertingImagesNotification.add(imageInsertStartedEvent.image());
1049 notificationManager.addNotification(insertingImagesNotification);
1053 // CORELISTENER METHODS
1060 public void imageInsertAborted(Image image) {
1061 insertingImagesNotification.remove(image);
1068 public void imageInsertFinished(Image image) {
1069 insertingImagesNotification.remove(image);
1070 insertedImagesNotification.add(image);
1071 notificationManager.addNotification(insertedImagesNotification);
1078 public void imageInsertFailed(Image image, Throwable cause) {
1079 insertingImagesNotification.remove(image);
1080 imageInsertFailedNotification.add(image);
1081 notificationManager.addNotification(imageInsertFailedNotification);