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.MarkPostKnownEvent;
41 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
42 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
43 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
44 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
45 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
46 import net.pterodactylus.sone.core.event.PostRemovedEvent;
47 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
48 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
49 import net.pterodactylus.sone.core.event.SoneLockedEvent;
50 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
51 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
52 import net.pterodactylus.sone.data.Album;
53 import net.pterodactylus.sone.data.Image;
54 import net.pterodactylus.sone.data.Post;
55 import net.pterodactylus.sone.data.PostReply;
56 import net.pterodactylus.sone.data.Profile;
57 import net.pterodactylus.sone.data.Reply;
58 import net.pterodactylus.sone.data.Sone;
59 import net.pterodactylus.sone.freenet.L10nFilter;
60 import net.pterodactylus.sone.freenet.wot.Identity;
61 import net.pterodactylus.sone.freenet.wot.Trust;
62 import net.pterodactylus.sone.main.SonePlugin;
63 import net.pterodactylus.sone.notify.ListNotification;
64 import net.pterodactylus.sone.template.AlbumAccessor;
65 import net.pterodactylus.sone.template.CollectionAccessor;
66 import net.pterodactylus.sone.template.CssClassNameFilter;
67 import net.pterodactylus.sone.template.HttpRequestAccessor;
68 import net.pterodactylus.sone.template.IdentityAccessor;
69 import net.pterodactylus.sone.template.ImageAccessor;
70 import net.pterodactylus.sone.template.ImageLinkFilter;
71 import net.pterodactylus.sone.template.JavascriptFilter;
72 import net.pterodactylus.sone.template.ParserFilter;
73 import net.pterodactylus.sone.template.PostAccessor;
74 import net.pterodactylus.sone.template.ProfileAccessor;
75 import net.pterodactylus.sone.template.ReplyAccessor;
76 import net.pterodactylus.sone.template.ReplyGroupFilter;
77 import net.pterodactylus.sone.template.RequestChangeFilter;
78 import net.pterodactylus.sone.template.SoneAccessor;
79 import net.pterodactylus.sone.template.SubstringFilter;
80 import net.pterodactylus.sone.template.TrustAccessor;
81 import net.pterodactylus.sone.template.UniqueElementFilter;
82 import net.pterodactylus.sone.template.UnknownDateFilter;
83 import net.pterodactylus.sone.text.Part;
84 import net.pterodactylus.sone.text.SonePart;
85 import net.pterodactylus.sone.text.SoneTextParser;
86 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
87 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
88 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
89 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
90 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
91 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
92 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
93 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
94 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
95 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
96 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
97 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
98 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
99 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
100 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
101 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
102 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
103 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
104 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
105 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
106 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
107 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
108 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
109 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
110 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
111 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
112 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
113 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
114 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
115 import net.pterodactylus.sone.web.page.FreenetRequest;
116 import net.pterodactylus.sone.web.page.PageToadlet;
117 import net.pterodactylus.sone.web.page.PageToadletFactory;
118 import net.pterodactylus.util.logging.Logging;
119 import net.pterodactylus.util.notify.Notification;
120 import net.pterodactylus.util.notify.NotificationManager;
121 import net.pterodactylus.util.notify.TemplateNotification;
122 import net.pterodactylus.util.template.ClassPathTemplateProvider;
123 import net.pterodactylus.util.template.CollectionSortFilter;
124 import net.pterodactylus.util.template.ContainsFilter;
125 import net.pterodactylus.util.template.DateFilter;
126 import net.pterodactylus.util.template.FormatFilter;
127 import net.pterodactylus.util.template.HtmlFilter;
128 import net.pterodactylus.util.template.MatchFilter;
129 import net.pterodactylus.util.template.ModFilter;
130 import net.pterodactylus.util.template.PaginationFilter;
131 import net.pterodactylus.util.template.ReflectionAccessor;
132 import net.pterodactylus.util.template.ReplaceFilter;
133 import net.pterodactylus.util.template.StoreFilter;
134 import net.pterodactylus.util.template.Template;
135 import net.pterodactylus.util.template.TemplateContextFactory;
136 import net.pterodactylus.util.template.TemplateParser;
137 import net.pterodactylus.util.template.TemplateProvider;
138 import net.pterodactylus.util.template.XmlFilter;
139 import net.pterodactylus.util.thread.Ticker;
140 import net.pterodactylus.util.version.Version;
141 import net.pterodactylus.util.web.RedirectPage;
142 import net.pterodactylus.util.web.StaticPage;
143 import net.pterodactylus.util.web.TemplatePage;
145 import com.google.common.collect.Collections2;
146 import com.google.common.collect.ImmutableSet;
147 import com.google.common.eventbus.Subscribe;
148 import com.google.inject.Inject;
150 import freenet.clients.http.SessionManager;
151 import freenet.clients.http.SessionManager.Session;
152 import freenet.clients.http.ToadletContainer;
153 import freenet.clients.http.ToadletContext;
154 import freenet.l10n.BaseL10n;
155 import freenet.support.api.HTTPRequest;
158 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
159 * references to l10n helpers.
161 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
163 public class WebInterface implements CoreListener {
166 private static final Logger logger = Logging.getLogger(WebInterface.class);
168 /** The notification manager. */
169 private final NotificationManager notificationManager = new NotificationManager();
171 /** The Sone plugin. */
172 private final SonePlugin sonePlugin;
174 /** The registered toadlets. */
175 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
177 /** The form password. */
178 private final String formPassword;
180 /** The template context factory. */
181 private final TemplateContextFactory templateContextFactory;
183 /** The Sone text parser. */
184 private final SoneTextParser soneTextParser;
186 /** The parser filter. */
187 private final ParserFilter parserFilter;
189 /** The “new Sone” notification. */
190 private final ListNotification<Sone> newSoneNotification;
192 /** The “new post” notification. */
193 private final ListNotification<Post> newPostNotification;
195 /** The “new reply” notification. */
196 private final ListNotification<PostReply> newReplyNotification;
198 /** The invisible “local post” notification. */
199 private final ListNotification<Post> localPostNotification;
201 /** The invisible “local reply” notification. */
202 private final ListNotification<PostReply> localReplyNotification;
204 /** The “you have been mentioned” notification. */
205 private final ListNotification<Post> mentionNotification;
207 /** Notifications for sone inserts. */
208 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
210 /** Sone locked notification ticker objects. */
211 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
213 /** The “Sone locked” notification. */
214 private final ListNotification<Sone> lockedSonesNotification;
216 /** The “new version” notification. */
217 private final TemplateNotification newVersionNotification;
219 /** The “inserting images” notification. */
220 private final ListNotification<Image> insertingImagesNotification;
222 /** The “inserted images” notification. */
223 private final ListNotification<Image> insertedImagesNotification;
225 /** The “image insert failed” notification. */
226 private final ListNotification<Image> imageInsertFailedNotification;
229 * Creates a new web interface.
235 public WebInterface(SonePlugin sonePlugin) {
236 this.sonePlugin = sonePlugin;
237 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
238 soneTextParser = new SoneTextParser(getCore(), getCore());
240 templateContextFactory = new TemplateContextFactory();
241 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
242 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
243 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
244 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
245 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
246 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
247 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
248 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
249 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
250 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
251 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
252 templateContextFactory.addFilter("date", new DateFilter());
253 templateContextFactory.addFilter("html", new HtmlFilter());
254 templateContextFactory.addFilter("replace", new ReplaceFilter());
255 templateContextFactory.addFilter("store", new StoreFilter());
256 templateContextFactory.addFilter("l10n", new L10nFilter(this));
257 templateContextFactory.addFilter("substring", new SubstringFilter());
258 templateContextFactory.addFilter("xml", new XmlFilter());
259 templateContextFactory.addFilter("change", new RequestChangeFilter());
260 templateContextFactory.addFilter("match", new MatchFilter());
261 templateContextFactory.addFilter("css", new CssClassNameFilter());
262 templateContextFactory.addFilter("js", new JavascriptFilter());
263 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
264 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
265 templateContextFactory.addFilter("format", new FormatFilter());
266 templateContextFactory.addFilter("sort", new CollectionSortFilter());
267 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
268 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
269 templateContextFactory.addFilter("in", new ContainsFilter());
270 templateContextFactory.addFilter("unique", new UniqueElementFilter());
271 templateContextFactory.addFilter("mod", new ModFilter());
272 templateContextFactory.addFilter("paginate", new PaginationFilter());
273 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
274 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
275 templateContextFactory.addTemplateObject("webInterface", this);
276 templateContextFactory.addTemplateObject("formPassword", formPassword);
278 /* create notifications. */
279 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
280 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
282 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
283 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
285 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
286 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
288 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
289 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
291 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
292 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
294 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
295 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
297 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
298 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
300 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
301 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
303 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
304 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
306 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
307 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
309 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
310 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
318 * Returns the Sone core used by the Sone plugin.
320 * @return The Sone core
322 public Core getCore() {
323 return sonePlugin.core();
327 * Returns the template context factory of the web interface.
329 * @return The template context factory
331 public TemplateContextFactory getTemplateContextFactory() {
332 return templateContextFactory;
336 * Returns the current session, creating a new session if there is no
339 * @param toadletContenxt
340 * The toadlet context
341 * @return The current session, or {@code null} if there is no current
344 public Session getCurrentSession(ToadletContext toadletContenxt) {
345 return getCurrentSession(toadletContenxt, true);
349 * Returns the current session, creating a new session if there is no
350 * current session and {@code create} is {@code true}.
352 * @param toadletContenxt
353 * The toadlet context
355 * {@code true} to create a new session if there is no current
356 * session, {@code false} otherwise
357 * @return The current session, or {@code null} if there is no current
360 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
361 Session session = getSessionManager().useSession(toadletContenxt);
362 if (create && (session == null)) {
363 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
369 * Returns the currently logged in Sone.
371 * @param toadletContext
372 * The toadlet context
373 * @return The currently logged in Sone, or {@code null} if no Sone is
374 * currently logged in
376 public Sone getCurrentSone(ToadletContext toadletContext) {
377 return getCurrentSone(toadletContext, true);
381 * Returns the currently logged in Sone.
383 * @param toadletContext
384 * The toadlet context
386 * {@code true} to create a new session if no session exists,
387 * {@code false} to not create a new session
388 * @return The currently logged in Sone, or {@code null} if no Sone is
389 * currently logged in
391 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
392 Collection<Sone> localSones = getCore().getLocalSones();
393 if (localSones.size() == 1) {
394 return localSones.iterator().next();
396 return getCurrentSone(getCurrentSession(toadletContext, create));
400 * Returns the currently logged in Sone.
404 * @return The currently logged in Sone, or {@code null} if no Sone is
405 * currently logged in
407 public Sone getCurrentSone(Session session) {
408 if (session == null) {
411 String soneId = (String) session.getAttribute("Sone.CurrentSone");
412 if (soneId == null) {
415 return getCore().getLocalSone(soneId, false);
419 * Sets the currently logged in Sone.
421 * @param toadletContext
422 * The toadlet context
424 * The Sone to set as currently logged in
426 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
427 Session session = getCurrentSession(toadletContext);
429 session.removeAttribute("Sone.CurrentSone");
431 session.setAttribute("Sone.CurrentSone", sone.getId());
436 * Returns the notification manager.
438 * @return The notification manager
440 public NotificationManager getNotifications() {
441 return notificationManager;
445 * Returns the l10n helper of the node.
447 * @return The node’s l10n helper
449 public BaseL10n getL10n() {
450 return sonePlugin.l10n().getBase();
454 * Returns the session manager of the node.
456 * @return The node’s session manager
458 public SessionManager getSessionManager() {
459 return sonePlugin.pluginRespirator().getSessionManager("Sone");
463 * Returns the node’s form password.
465 * @return The form password
467 public String getFormPassword() {
472 * Returns the posts that have been announced as new in the
473 * {@link #newPostNotification}.
475 * @return The new posts
477 public Set<Post> getNewPosts() {
478 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
482 * Returns the replies that have been announced as new in the
483 * {@link #newReplyNotification}.
485 * @return The new replies
487 public Set<PostReply> getNewReplies() {
488 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
492 * Sets whether the current start of the plugin is the first start. It is
493 * considered a first start if the configuration file does not exist.
496 * {@code true} if no configuration file existed when Sone was
497 * loaded, {@code false} otherwise
499 public void setFirstStart(boolean firstStart) {
501 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
502 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
503 notificationManager.addNotification(firstStartNotification);
508 * Sets whether Sone was started with a fresh configuration file.
511 * {@code true} if Sone was started with a fresh configuration,
512 * {@code false} if the existing configuration could be read
514 public void setNewConfig(boolean newConfig) {
515 if (newConfig && !hasFirstStartNotification()) {
516 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
517 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
518 notificationManager.addNotification(configNotReadNotification);
527 * Returns whether the first start notification is currently displayed.
529 * @return {@code true} if the first-start notification is currently
530 * displayed, {@code false} otherwise
532 private boolean hasFirstStartNotification() {
533 return notificationManager.getNotification("first-start-notification") != null;
541 * Starts the web interface and registers all toadlets.
543 public void start() {
546 /* notification templates. */
547 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
549 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
550 notificationManager.addNotification(startupNotification);
552 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
556 startupNotification.dismiss();
558 }, "Sone Startup Notification Remover");
560 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
561 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
562 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
565 @SuppressWarnings("synthetic-access")
567 if (getCore().getIdentityManager().isConnected()) {
568 wotMissingNotification.dismiss();
570 notificationManager.addNotification(wotMissingNotification);
572 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
575 }, "Sone WoT Connector Checker");
579 * Stops the web interface and unregisters all toadlets.
582 unregisterToadlets();
583 Ticker.getInstance().stop();
591 * Register all toadlets.
593 private void registerToadlets() {
594 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
595 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
596 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
597 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
598 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
599 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
600 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
601 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
602 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
603 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
604 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
605 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
606 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
607 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
608 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
609 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
610 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
611 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
612 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
613 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
614 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
615 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
616 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
617 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
618 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
619 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
620 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
621 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
622 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
623 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
625 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
705 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
706 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
707 for (PageToadlet toadlet : pageToadlets) {
708 String menuName = toadlet.getMenuName();
709 if (menuName != null) {
710 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
712 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
718 * Unregisters all toadlets.
720 private void unregisterToadlets() {
721 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
722 for (PageToadlet pageToadlet : pageToadlets) {
723 toadletContainer.unregister(pageToadlet);
725 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
729 * Creates a {@link Reader} from the {@link InputStream} for the resource
730 * with the given name.
732 * @param resourceName
733 * The name of the resource
734 * @return A {@link Reader} for the resource
736 private Reader createReader(String resourceName) {
738 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
739 } catch (UnsupportedEncodingException uee1) {
745 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
746 * {@link SonePart}s in the given text (after parsing it using
747 * {@link SoneTextParser}).
751 * @return All mentioned local Sones
753 private Collection<Sone> getMentionedSones(String text) {
754 /* we need no context to find mentioned Sones. */
755 Set<Sone> mentionedSones = new HashSet<Sone>();
757 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
758 if (part instanceof SonePart) {
759 mentionedSones.add(((SonePart) part).getSone());
762 } catch (IOException ioe1) {
763 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
765 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
769 * Returns the Sone insert notification for the given Sone. If no
770 * notification for the given Sone exists, a new notification is created and
774 * The Sone to get the insert notification for
775 * @return The Sone insert notification
777 private TemplateNotification getSoneInsertNotification(Sone sone) {
778 synchronized (soneInsertNotifications) {
779 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
780 if (templateNotification == null) {
781 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
782 templateNotification.set("insertSone", sone);
783 soneInsertNotifications.put(sone, templateNotification);
785 return templateNotification;
794 * Notifies the web interface that a new {@link Sone} was found.
796 * @param newSoneFoundEvent
800 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
801 newSoneNotification.add(newSoneFoundEvent.sone());
802 if (!hasFirstStartNotification()) {
803 notificationManager.addNotification(newSoneNotification);
808 * Notifies the web interface that a new {@link Post} was found.
810 * @param newPostFoundEvent
814 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
815 Post post = newPostFoundEvent.post();
816 boolean isLocal = post.getSone().isLocal();
818 localPostNotification.add(post);
820 newPostNotification.add(post);
822 if (!hasFirstStartNotification()) {
823 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
824 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
825 mentionNotification.add(post);
826 notificationManager.addNotification(mentionNotification);
829 getCore().markPostKnown(post);
834 * Notifies the web interface that a new {@link PostReply} was found.
836 * @param newPostReplyFoundEvent
840 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
841 PostReply reply = newPostReplyFoundEvent.postReply();
842 boolean isLocal = reply.getSone().isLocal();
844 localReplyNotification.add(reply);
846 newReplyNotification.add(reply);
848 if (!hasFirstStartNotification()) {
849 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
850 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
851 mentionNotification.add(reply.getPost());
852 notificationManager.addNotification(mentionNotification);
855 getCore().markReplyKnown(reply);
860 * Notifies the web interface that a {@link Sone} was marked as known.
862 * @param markSoneKnownEvent
866 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
867 newSoneNotification.remove(markSoneKnownEvent.sone());
871 * Notifies the web interface that a {@link Post} was marked as known.
873 * @param markPostKnownEvent
877 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
878 newPostNotification.remove(markPostKnownEvent.post());
879 localPostNotification.remove(markPostKnownEvent.post());
880 mentionNotification.remove(markPostKnownEvent.post());
884 * Notifies the web interface that a {@link PostReply} was marked as known.
886 * @param markPostReplyKnownEvent
890 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
891 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
892 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
893 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
897 * Notifies the web interface that a {@link Sone} was removed.
899 * @param soneRemovedEvent
903 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
904 newSoneNotification.remove(soneRemovedEvent.sone());
908 * Notifies the web interface that a {@link Post} was removed.
910 * @param postRemovedEvent
914 public void postRemoved(PostRemovedEvent postRemovedEvent) {
915 newPostNotification.remove(postRemovedEvent.post());
916 localPostNotification.remove(postRemovedEvent.post());
917 mentionNotification.remove(postRemovedEvent.post());
921 * Notifies the web interface that a {@link PostReply} was removed.
923 * @param postReplyRemovedEvent
927 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
928 PostReply reply = postReplyRemovedEvent.postReply();
929 newReplyNotification.remove(reply);
930 localReplyNotification.remove(reply);
931 if (!getMentionedSones(reply.getText()).isEmpty()) {
932 boolean isMentioned = false;
933 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
934 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
937 mentionNotification.remove(reply.getPost());
943 * Notifies the web interface that a Sone was locked.
945 * @param soneLockedEvent
949 public void soneLocked(SoneLockedEvent soneLockedEvent) {
950 final Sone sone = soneLockedEvent.sone();
951 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
954 @SuppressWarnings("synthetic-access")
956 lockedSonesNotification.add(sone);
957 lockedSonesTickerObjects.remove(sone);
958 notificationManager.addNotification(lockedSonesNotification);
960 }, "Sone Locked Notification");
961 lockedSonesTickerObjects.put(sone, tickerObject);
965 * Notifies the web interface that a Sone was unlocked.
967 * @param soneUnlockedEvent
971 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
972 lockedSonesNotification.remove(soneUnlockedEvent.sone());
973 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()));
977 * Notifies the web interface that a {@link Sone} is being inserted.
979 * @param soneInsertingEvent
983 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
984 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
985 soneInsertNotification.set("soneStatus", "inserting");
986 if (soneInsertingEvent.sone().getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
987 notificationManager.addNotification(soneInsertNotification);
992 // CORELISTENER METHODS
999 public void soneInserted(Sone sone, long insertDuration) {
1000 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
1001 soneInsertNotification.set("soneStatus", "inserted");
1002 soneInsertNotification.set("insertDuration", insertDuration / 1000);
1003 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1004 notificationManager.addNotification(soneInsertNotification);
1012 public void soneInsertAborted(Sone sone, Throwable cause) {
1013 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
1014 soneInsertNotification.set("soneStatus", "insert-aborted");
1015 soneInsertNotification.set("insert-error", cause);
1016 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1017 notificationManager.addNotification(soneInsertNotification);
1025 public void updateFound(Version version, long releaseTime, long latestEdition) {
1026 newVersionNotification.getTemplateContext().set("latestVersion", version);
1027 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
1028 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
1029 notificationManager.addNotification(newVersionNotification);
1036 public void imageInsertStarted(Image image) {
1037 insertingImagesNotification.add(image);
1038 notificationManager.addNotification(insertingImagesNotification);
1045 public void imageInsertAborted(Image image) {
1046 insertingImagesNotification.remove(image);
1053 public void imageInsertFinished(Image image) {
1054 insertingImagesNotification.remove(image);
1055 insertedImagesNotification.add(image);
1056 notificationManager.addNotification(insertedImagesNotification);
1063 public void imageInsertFailed(Image image, Throwable cause) {
1064 insertingImagesNotification.remove(image);
1065 imageInsertFailedNotification.add(image);
1066 notificationManager.addNotification(imageInsertFailedNotification);