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.SoneRemovedEvent;
49 import net.pterodactylus.sone.data.Album;
50 import net.pterodactylus.sone.data.Image;
51 import net.pterodactylus.sone.data.Post;
52 import net.pterodactylus.sone.data.PostReply;
53 import net.pterodactylus.sone.data.Profile;
54 import net.pterodactylus.sone.data.Reply;
55 import net.pterodactylus.sone.data.Sone;
56 import net.pterodactylus.sone.freenet.L10nFilter;
57 import net.pterodactylus.sone.freenet.wot.Identity;
58 import net.pterodactylus.sone.freenet.wot.Trust;
59 import net.pterodactylus.sone.main.SonePlugin;
60 import net.pterodactylus.sone.notify.ListNotification;
61 import net.pterodactylus.sone.template.AlbumAccessor;
62 import net.pterodactylus.sone.template.CollectionAccessor;
63 import net.pterodactylus.sone.template.CssClassNameFilter;
64 import net.pterodactylus.sone.template.HttpRequestAccessor;
65 import net.pterodactylus.sone.template.IdentityAccessor;
66 import net.pterodactylus.sone.template.ImageAccessor;
67 import net.pterodactylus.sone.template.ImageLinkFilter;
68 import net.pterodactylus.sone.template.JavascriptFilter;
69 import net.pterodactylus.sone.template.ParserFilter;
70 import net.pterodactylus.sone.template.PostAccessor;
71 import net.pterodactylus.sone.template.ProfileAccessor;
72 import net.pterodactylus.sone.template.ReplyAccessor;
73 import net.pterodactylus.sone.template.ReplyGroupFilter;
74 import net.pterodactylus.sone.template.RequestChangeFilter;
75 import net.pterodactylus.sone.template.SoneAccessor;
76 import net.pterodactylus.sone.template.SubstringFilter;
77 import net.pterodactylus.sone.template.TrustAccessor;
78 import net.pterodactylus.sone.template.UniqueElementFilter;
79 import net.pterodactylus.sone.template.UnknownDateFilter;
80 import net.pterodactylus.sone.text.Part;
81 import net.pterodactylus.sone.text.SonePart;
82 import net.pterodactylus.sone.text.SoneTextParser;
83 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
84 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
85 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
86 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
87 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
88 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
89 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
90 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
91 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
92 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
93 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
94 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
95 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
96 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
97 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
98 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
99 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
100 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
101 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
102 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
103 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
104 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
105 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
106 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
107 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
108 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
109 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
110 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
111 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
112 import net.pterodactylus.sone.web.page.FreenetRequest;
113 import net.pterodactylus.sone.web.page.PageToadlet;
114 import net.pterodactylus.sone.web.page.PageToadletFactory;
115 import net.pterodactylus.util.logging.Logging;
116 import net.pterodactylus.util.notify.Notification;
117 import net.pterodactylus.util.notify.NotificationManager;
118 import net.pterodactylus.util.notify.TemplateNotification;
119 import net.pterodactylus.util.template.ClassPathTemplateProvider;
120 import net.pterodactylus.util.template.CollectionSortFilter;
121 import net.pterodactylus.util.template.ContainsFilter;
122 import net.pterodactylus.util.template.DateFilter;
123 import net.pterodactylus.util.template.FormatFilter;
124 import net.pterodactylus.util.template.HtmlFilter;
125 import net.pterodactylus.util.template.MatchFilter;
126 import net.pterodactylus.util.template.ModFilter;
127 import net.pterodactylus.util.template.PaginationFilter;
128 import net.pterodactylus.util.template.ReflectionAccessor;
129 import net.pterodactylus.util.template.ReplaceFilter;
130 import net.pterodactylus.util.template.StoreFilter;
131 import net.pterodactylus.util.template.Template;
132 import net.pterodactylus.util.template.TemplateContextFactory;
133 import net.pterodactylus.util.template.TemplateParser;
134 import net.pterodactylus.util.template.TemplateProvider;
135 import net.pterodactylus.util.template.XmlFilter;
136 import net.pterodactylus.util.thread.Ticker;
137 import net.pterodactylus.util.version.Version;
138 import net.pterodactylus.util.web.RedirectPage;
139 import net.pterodactylus.util.web.StaticPage;
140 import net.pterodactylus.util.web.TemplatePage;
142 import com.google.common.collect.Collections2;
143 import com.google.common.collect.ImmutableSet;
144 import com.google.common.eventbus.Subscribe;
145 import com.google.inject.Inject;
147 import freenet.clients.http.SessionManager;
148 import freenet.clients.http.SessionManager.Session;
149 import freenet.clients.http.ToadletContainer;
150 import freenet.clients.http.ToadletContext;
151 import freenet.l10n.BaseL10n;
152 import freenet.support.api.HTTPRequest;
155 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
156 * references to l10n helpers.
158 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
160 public class WebInterface implements CoreListener {
163 private static final Logger logger = Logging.getLogger(WebInterface.class);
165 /** The notification manager. */
166 private final NotificationManager notificationManager = new NotificationManager();
168 /** The Sone plugin. */
169 private final SonePlugin sonePlugin;
171 /** The registered toadlets. */
172 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
174 /** The form password. */
175 private final String formPassword;
177 /** The template context factory. */
178 private final TemplateContextFactory templateContextFactory;
180 /** The Sone text parser. */
181 private final SoneTextParser soneTextParser;
183 /** The parser filter. */
184 private final ParserFilter parserFilter;
186 /** The “new Sone” notification. */
187 private final ListNotification<Sone> newSoneNotification;
189 /** The “new post” notification. */
190 private final ListNotification<Post> newPostNotification;
192 /** The “new reply” notification. */
193 private final ListNotification<PostReply> newReplyNotification;
195 /** The invisible “local post” notification. */
196 private final ListNotification<Post> localPostNotification;
198 /** The invisible “local reply” notification. */
199 private final ListNotification<PostReply> localReplyNotification;
201 /** The “you have been mentioned” notification. */
202 private final ListNotification<Post> mentionNotification;
204 /** Notifications for sone inserts. */
205 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
207 /** Sone locked notification ticker objects. */
208 private final Map<Sone, Object> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, Object>());
210 /** The “Sone locked” notification. */
211 private final ListNotification<Sone> lockedSonesNotification;
213 /** The “new version” notification. */
214 private final TemplateNotification newVersionNotification;
216 /** The “inserting images” notification. */
217 private final ListNotification<Image> insertingImagesNotification;
219 /** The “inserted images” notification. */
220 private final ListNotification<Image> insertedImagesNotification;
222 /** The “image insert failed” notification. */
223 private final ListNotification<Image> imageInsertFailedNotification;
226 * Creates a new web interface.
232 public WebInterface(SonePlugin sonePlugin) {
233 this.sonePlugin = sonePlugin;
234 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
235 soneTextParser = new SoneTextParser(getCore(), getCore());
237 templateContextFactory = new TemplateContextFactory();
238 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
239 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
240 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
241 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
242 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
243 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
244 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
245 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
246 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
247 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
248 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
249 templateContextFactory.addFilter("date", new DateFilter());
250 templateContextFactory.addFilter("html", new HtmlFilter());
251 templateContextFactory.addFilter("replace", new ReplaceFilter());
252 templateContextFactory.addFilter("store", new StoreFilter());
253 templateContextFactory.addFilter("l10n", new L10nFilter(this));
254 templateContextFactory.addFilter("substring", new SubstringFilter());
255 templateContextFactory.addFilter("xml", new XmlFilter());
256 templateContextFactory.addFilter("change", new RequestChangeFilter());
257 templateContextFactory.addFilter("match", new MatchFilter());
258 templateContextFactory.addFilter("css", new CssClassNameFilter());
259 templateContextFactory.addFilter("js", new JavascriptFilter());
260 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
261 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
262 templateContextFactory.addFilter("format", new FormatFilter());
263 templateContextFactory.addFilter("sort", new CollectionSortFilter());
264 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
265 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
266 templateContextFactory.addFilter("in", new ContainsFilter());
267 templateContextFactory.addFilter("unique", new UniqueElementFilter());
268 templateContextFactory.addFilter("mod", new ModFilter());
269 templateContextFactory.addFilter("paginate", new PaginationFilter());
270 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
271 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
272 templateContextFactory.addTemplateObject("webInterface", this);
273 templateContextFactory.addTemplateObject("formPassword", formPassword);
275 /* create notifications. */
276 Template newSoneNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newSoneNotification.html"));
277 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
279 Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
280 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
282 Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html"));
283 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
285 Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
286 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
288 Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
289 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
291 Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
292 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
294 Template lockedSonesTemplate = TemplateParser.parse(createReader("/templates/notify/lockedSonesNotification.html"));
295 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
297 Template newVersionTemplate = TemplateParser.parse(createReader("/templates/notify/newVersionNotification.html"));
298 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
300 Template insertingImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserting-images-notification.html"));
301 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
303 Template insertedImagesTemplate = TemplateParser.parse(createReader("/templates/notify/inserted-images-notification.html"));
304 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
306 Template imageInsertFailedTemplate = TemplateParser.parse(createReader("/templates/notify/image-insert-failed-notification.html"));
307 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
315 * Returns the Sone core used by the Sone plugin.
317 * @return The Sone core
319 public Core getCore() {
320 return sonePlugin.core();
324 * Returns the template context factory of the web interface.
326 * @return The template context factory
328 public TemplateContextFactory getTemplateContextFactory() {
329 return templateContextFactory;
333 * Returns the current session, creating a new session if there is no
336 * @param toadletContenxt
337 * The toadlet context
338 * @return The current session, or {@code null} if there is no current
341 public Session getCurrentSession(ToadletContext toadletContenxt) {
342 return getCurrentSession(toadletContenxt, true);
346 * Returns the current session, creating a new session if there is no
347 * current session and {@code create} is {@code true}.
349 * @param toadletContenxt
350 * The toadlet context
352 * {@code true} to create a new session if there is no current
353 * session, {@code false} otherwise
354 * @return The current session, or {@code null} if there is no current
357 public Session getCurrentSession(ToadletContext toadletContenxt, boolean create) {
358 Session session = getSessionManager().useSession(toadletContenxt);
359 if (create && (session == null)) {
360 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
366 * Returns the currently logged in Sone.
368 * @param toadletContext
369 * The toadlet context
370 * @return The currently logged in Sone, or {@code null} if no Sone is
371 * currently logged in
373 public Sone getCurrentSone(ToadletContext toadletContext) {
374 return getCurrentSone(toadletContext, true);
378 * Returns the currently logged in Sone.
380 * @param toadletContext
381 * The toadlet context
383 * {@code true} to create a new session if no session exists,
384 * {@code false} to not create a new session
385 * @return The currently logged in Sone, or {@code null} if no Sone is
386 * currently logged in
388 public Sone getCurrentSone(ToadletContext toadletContext, boolean create) {
389 Collection<Sone> localSones = getCore().getLocalSones();
390 if (localSones.size() == 1) {
391 return localSones.iterator().next();
393 return getCurrentSone(getCurrentSession(toadletContext, create));
397 * Returns the currently logged in Sone.
401 * @return The currently logged in Sone, or {@code null} if no Sone is
402 * currently logged in
404 public Sone getCurrentSone(Session session) {
405 if (session == null) {
408 String soneId = (String) session.getAttribute("Sone.CurrentSone");
409 if (soneId == null) {
412 return getCore().getLocalSone(soneId, false);
416 * Sets the currently logged in Sone.
418 * @param toadletContext
419 * The toadlet context
421 * The Sone to set as currently logged in
423 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
424 Session session = getCurrentSession(toadletContext);
426 session.removeAttribute("Sone.CurrentSone");
428 session.setAttribute("Sone.CurrentSone", sone.getId());
433 * Returns the notification manager.
435 * @return The notification manager
437 public NotificationManager getNotifications() {
438 return notificationManager;
442 * Returns the l10n helper of the node.
444 * @return The node’s l10n helper
446 public BaseL10n getL10n() {
447 return sonePlugin.l10n().getBase();
451 * Returns the session manager of the node.
453 * @return The node’s session manager
455 public SessionManager getSessionManager() {
456 return sonePlugin.pluginRespirator().getSessionManager("Sone");
460 * Returns the node’s form password.
462 * @return The form password
464 public String getFormPassword() {
469 * Returns the posts that have been announced as new in the
470 * {@link #newPostNotification}.
472 * @return The new posts
474 public Set<Post> getNewPosts() {
475 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
479 * Returns the replies that have been announced as new in the
480 * {@link #newReplyNotification}.
482 * @return The new replies
484 public Set<PostReply> getNewReplies() {
485 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
489 * Sets whether the current start of the plugin is the first start. It is
490 * considered a first start if the configuration file does not exist.
493 * {@code true} if no configuration file existed when Sone was
494 * loaded, {@code false} otherwise
496 public void setFirstStart(boolean firstStart) {
498 Template firstStartNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/firstStartNotification.html"));
499 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
500 notificationManager.addNotification(firstStartNotification);
505 * Sets whether Sone was started with a fresh configuration file.
508 * {@code true} if Sone was started with a fresh configuration,
509 * {@code false} if the existing configuration could be read
511 public void setNewConfig(boolean newConfig) {
512 if (newConfig && !hasFirstStartNotification()) {
513 Template configNotReadNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/configNotReadNotification.html"));
514 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
515 notificationManager.addNotification(configNotReadNotification);
524 * Returns whether the first start notification is currently displayed.
526 * @return {@code true} if the first-start notification is currently
527 * displayed, {@code false} otherwise
529 private boolean hasFirstStartNotification() {
530 return notificationManager.getNotification("first-start-notification") != null;
538 * Starts the web interface and registers all toadlets.
540 public void start() {
543 /* notification templates. */
544 Template startupNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/startupNotification.html"));
546 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
547 notificationManager.addNotification(startupNotification);
549 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
553 startupNotification.dismiss();
555 }, "Sone Startup Notification Remover");
557 Template wotMissingNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/wotMissingNotification.html"));
558 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
559 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
562 @SuppressWarnings("synthetic-access")
564 if (getCore().getIdentityManager().isConnected()) {
565 wotMissingNotification.dismiss();
567 notificationManager.addNotification(wotMissingNotification);
569 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
572 }, "Sone WoT Connector Checker");
576 * Stops the web interface and unregisters all toadlets.
579 unregisterToadlets();
580 Ticker.getInstance().stop();
588 * Register all toadlets.
590 private void registerToadlets() {
591 Template emptyTemplate = TemplateParser.parse(new StringReader(""));
592 Template loginTemplate = TemplateParser.parse(createReader("/templates/login.html"));
593 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
594 Template newTemplate = TemplateParser.parse(createReader("/templates/new.html"));
595 Template knownSonesTemplate = TemplateParser.parse(createReader("/templates/knownSones.html"));
596 Template createSoneTemplate = TemplateParser.parse(createReader("/templates/createSone.html"));
597 Template createPostTemplate = TemplateParser.parse(createReader("/templates/createPost.html"));
598 Template createReplyTemplate = TemplateParser.parse(createReader("/templates/createReply.html"));
599 Template bookmarksTemplate = TemplateParser.parse(createReader("/templates/bookmarks.html"));
600 Template searchTemplate = TemplateParser.parse(createReader("/templates/search.html"));
601 Template editProfileTemplate = TemplateParser.parse(createReader("/templates/editProfile.html"));
602 Template editProfileFieldTemplate = TemplateParser.parse(createReader("/templates/editProfileField.html"));
603 Template deleteProfileFieldTemplate = TemplateParser.parse(createReader("/templates/deleteProfileField.html"));
604 Template viewSoneTemplate = TemplateParser.parse(createReader("/templates/viewSone.html"));
605 Template viewPostTemplate = TemplateParser.parse(createReader("/templates/viewPost.html"));
606 Template deletePostTemplate = TemplateParser.parse(createReader("/templates/deletePost.html"));
607 Template deleteReplyTemplate = TemplateParser.parse(createReader("/templates/deleteReply.html"));
608 Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html"));
609 Template imageBrowserTemplate = TemplateParser.parse(createReader("/templates/imageBrowser.html"));
610 Template createAlbumTemplate = TemplateParser.parse(createReader("/templates/createAlbum.html"));
611 Template deleteAlbumTemplate = TemplateParser.parse(createReader("/templates/deleteAlbum.html"));
612 Template deleteImageTemplate = TemplateParser.parse(createReader("/templates/deleteImage.html"));
613 Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html"));
614 Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html"));
615 Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html"));
616 Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html"));
617 Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html"));
618 Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html"));
619 Template replyTemplate = TemplateParser.parse(createReader("/templates/include/viewReply.html"));
620 Template openSearchTemplate = TemplateParser.parse(createReader("/templates/xml/OpenSearch.xml"));
622 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
623 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
624 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
625 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
626 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
627 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
628 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
629 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
630 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
631 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
632 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
633 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
634 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
635 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
702 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
703 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
704 for (PageToadlet toadlet : pageToadlets) {
705 String menuName = toadlet.getMenuName();
706 if (menuName != null) {
707 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
709 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
715 * Unregisters all toadlets.
717 private void unregisterToadlets() {
718 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
719 for (PageToadlet pageToadlet : pageToadlets) {
720 toadletContainer.unregister(pageToadlet);
722 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
726 * Creates a {@link Reader} from the {@link InputStream} for the resource
727 * with the given name.
729 * @param resourceName
730 * The name of the resource
731 * @return A {@link Reader} for the resource
733 private Reader createReader(String resourceName) {
735 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
736 } catch (UnsupportedEncodingException uee1) {
742 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
743 * {@link SonePart}s in the given text (after parsing it using
744 * {@link SoneTextParser}).
748 * @return All mentioned local Sones
750 private Collection<Sone> getMentionedSones(String text) {
751 /* we need no context to find mentioned Sones. */
752 Set<Sone> mentionedSones = new HashSet<Sone>();
754 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
755 if (part instanceof SonePart) {
756 mentionedSones.add(((SonePart) part).getSone());
759 } catch (IOException ioe1) {
760 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
762 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
766 * Returns the Sone insert notification for the given Sone. If no
767 * notification for the given Sone exists, a new notification is created and
771 * The Sone to get the insert notification for
772 * @return The Sone insert notification
774 private TemplateNotification getSoneInsertNotification(Sone sone) {
775 synchronized (soneInsertNotifications) {
776 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
777 if (templateNotification == null) {
778 templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
779 templateNotification.set("insertSone", sone);
780 soneInsertNotifications.put(sone, templateNotification);
782 return templateNotification;
791 * Notifies the web interface that a new {@link Sone} was found.
793 * @param newSoneFoundEvent
797 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
798 newSoneNotification.add(newSoneFoundEvent.sone());
799 if (!hasFirstStartNotification()) {
800 notificationManager.addNotification(newSoneNotification);
805 * Notifies the web interface that a new {@link Post} was found.
807 * @param newPostFoundEvent
811 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
812 Post post = newPostFoundEvent.post();
813 boolean isLocal = post.getSone().isLocal();
815 localPostNotification.add(post);
817 newPostNotification.add(post);
819 if (!hasFirstStartNotification()) {
820 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
821 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
822 mentionNotification.add(post);
823 notificationManager.addNotification(mentionNotification);
826 getCore().markPostKnown(post);
831 * Notifies the web interface that a new {@link PostReply} was found.
833 * @param newPostReplyFoundEvent
837 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
838 PostReply reply = newPostReplyFoundEvent.postReply();
839 boolean isLocal = reply.getSone().isLocal();
841 localReplyNotification.add(reply);
843 newReplyNotification.add(reply);
845 if (!hasFirstStartNotification()) {
846 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
847 if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
848 mentionNotification.add(reply.getPost());
849 notificationManager.addNotification(mentionNotification);
852 getCore().markReplyKnown(reply);
857 * Notifies the web interface that a {@link Sone} was marked as known.
859 * @param markSoneKnownEvent
863 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
864 newSoneNotification.remove(markSoneKnownEvent.sone());
868 * Notifies the web interface that a {@link Post} was marked as known.
870 * @param markPostKnownEvent
874 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
875 newPostNotification.remove(markPostKnownEvent.post());
876 localPostNotification.remove(markPostKnownEvent.post());
877 mentionNotification.remove(markPostKnownEvent.post());
881 * Notifies the web interface that a {@link PostReply} was marked as known.
883 * @param markPostReplyKnownEvent
887 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
888 newReplyNotification.remove(markPostReplyKnownEvent.postReply());
889 localReplyNotification.remove(markPostReplyKnownEvent.postReply());
890 mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
894 * Notifies the web interface that a {@link Sone} was removed.
896 * @param soneRemovedEvent
900 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
901 newSoneNotification.remove(soneRemovedEvent.sone());
905 * Notifies the web interface that a {@link Post} was removed.
907 * @param postRemovedEvent
911 public void postRemoved(PostRemovedEvent postRemovedEvent) {
912 newPostNotification.remove(postRemovedEvent.post());
913 localPostNotification.remove(postRemovedEvent.post());
914 mentionNotification.remove(postRemovedEvent.post());
918 * Notifies the web interface that a {@link PostReply} was removed.
920 * @param postReplyRemovedEvent
924 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
925 PostReply reply = postReplyRemovedEvent.postReply();
926 newReplyNotification.remove(reply);
927 localReplyNotification.remove(reply);
928 if (!getMentionedSones(reply.getText()).isEmpty()) {
929 boolean isMentioned = false;
930 for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
931 isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
934 mentionNotification.remove(reply.getPost());
940 // CORELISTENER METHODS
947 public void soneLocked(final Sone sone) {
948 Object tickerObject = Ticker.getInstance().registerEvent(System.currentTimeMillis() + (5 * 60) * 1000, new Runnable() {
951 @SuppressWarnings("synthetic-access")
953 lockedSonesNotification.add(sone);
954 lockedSonesTickerObjects.remove(sone);
955 notificationManager.addNotification(lockedSonesNotification);
957 }, "Sone Locked Notification");
958 lockedSonesTickerObjects.put(sone, tickerObject);
965 public void soneUnlocked(Sone sone) {
966 lockedSonesNotification.remove(sone);
967 Ticker.getInstance().deregisterEvent(lockedSonesTickerObjects.remove(sone));
974 public void soneInserting(Sone sone) {
975 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
976 soneInsertNotification.set("soneStatus", "inserting");
977 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
978 notificationManager.addNotification(soneInsertNotification);
986 public void soneInserted(Sone sone, long insertDuration) {
987 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
988 soneInsertNotification.set("soneStatus", "inserted");
989 soneInsertNotification.set("insertDuration", insertDuration / 1000);
990 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
991 notificationManager.addNotification(soneInsertNotification);
999 public void soneInsertAborted(Sone sone, Throwable cause) {
1000 TemplateNotification soneInsertNotification = getSoneInsertNotification(sone);
1001 soneInsertNotification.set("soneStatus", "insert-aborted");
1002 soneInsertNotification.set("insert-error", cause);
1003 if (sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").get()) {
1004 notificationManager.addNotification(soneInsertNotification);
1012 public void updateFound(Version version, long releaseTime, long latestEdition) {
1013 newVersionNotification.getTemplateContext().set("latestVersion", version);
1014 newVersionNotification.getTemplateContext().set("latestEdition", latestEdition);
1015 newVersionNotification.getTemplateContext().set("releaseTime", releaseTime);
1016 notificationManager.addNotification(newVersionNotification);
1023 public void imageInsertStarted(Image image) {
1024 insertingImagesNotification.add(image);
1025 notificationManager.addNotification(insertingImagesNotification);
1032 public void imageInsertAborted(Image image) {
1033 insertingImagesNotification.remove(image);
1040 public void imageInsertFinished(Image image) {
1041 insertingImagesNotification.remove(image);
1042 insertedImagesNotification.add(image);
1043 notificationManager.addNotification(insertedImagesNotification);
1050 public void imageInsertFailed(Image image, Throwable cause) {
1051 insertingImagesNotification.remove(image);
1052 imageInsertFailedNotification.add(image);
1053 notificationManager.addNotification(imageInsertFailedNotification);