2 * Sone - WebInterface.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import static java.util.logging.Logger.getLogger;
21 import static net.pterodactylus.util.template.TemplateParser.parse;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.io.StringReader;
28 import java.io.UnsupportedEncodingException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.UUID;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.ScheduledExecutorService;
40 import java.util.concurrent.ScheduledFuture;
41 import java.util.concurrent.TimeUnit;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
45 import net.pterodactylus.sone.core.Core;
46 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
47 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
48 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
49 import net.pterodactylus.sone.core.event.ImageInsertStartedEvent;
50 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
51 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
52 import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
53 import net.pterodactylus.sone.core.event.NewPostFoundEvent;
54 import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
55 import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
56 import net.pterodactylus.sone.core.event.PostRemovedEvent;
57 import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
58 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
59 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
60 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
61 import net.pterodactylus.sone.core.event.SoneLockedEvent;
62 import net.pterodactylus.sone.core.event.SoneRemovedEvent;
63 import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
64 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
65 import net.pterodactylus.sone.data.Album;
66 import net.pterodactylus.sone.data.Image;
67 import net.pterodactylus.sone.data.LocalSone;
68 import net.pterodactylus.sone.data.Post;
69 import net.pterodactylus.sone.data.PostReply;
70 import net.pterodactylus.sone.data.Profile;
71 import net.pterodactylus.sone.data.Reply;
72 import net.pterodactylus.sone.data.Sone;
73 import net.pterodactylus.sone.freenet.L10nFilter;
74 import net.pterodactylus.sone.freenet.wot.Identity;
75 import net.pterodactylus.sone.freenet.wot.Trust;
76 import net.pterodactylus.sone.main.SonePlugin;
77 import net.pterodactylus.sone.notify.ListNotification;
78 import net.pterodactylus.sone.template.AlbumAccessor;
79 import net.pterodactylus.sone.template.CollectionAccessor;
80 import net.pterodactylus.sone.template.CssClassNameFilter;
81 import net.pterodactylus.sone.template.HttpRequestAccessor;
82 import net.pterodactylus.sone.template.IdentityAccessor;
83 import net.pterodactylus.sone.template.ImageAccessor;
84 import net.pterodactylus.sone.template.ImageLinkFilter;
85 import net.pterodactylus.sone.template.JavascriptFilter;
86 import net.pterodactylus.sone.template.ParserFilter;
87 import net.pterodactylus.sone.template.PostAccessor;
88 import net.pterodactylus.sone.template.ProfileAccessor;
89 import net.pterodactylus.sone.template.ReplyAccessor;
90 import net.pterodactylus.sone.template.ReplyGroupFilter;
91 import net.pterodactylus.sone.template.RequestChangeFilter;
92 import net.pterodactylus.sone.template.SoneAccessor;
93 import net.pterodactylus.sone.template.SubstringFilter;
94 import net.pterodactylus.sone.template.TrustAccessor;
95 import net.pterodactylus.sone.template.UniqueElementFilter;
96 import net.pterodactylus.sone.template.UnknownDateFilter;
97 import net.pterodactylus.sone.text.Part;
98 import net.pterodactylus.sone.text.SonePart;
99 import net.pterodactylus.sone.text.SoneTextParser;
100 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
101 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
102 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
103 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
104 import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
105 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
106 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
107 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
108 import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
109 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
110 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
111 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
112 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
113 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
114 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
115 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
116 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
117 import net.pterodactylus.sone.web.ajax.GetTimesAjaxPage;
118 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
119 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
120 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
121 import net.pterodactylus.sone.web.ajax.MarkAsKnownAjaxPage;
122 import net.pterodactylus.sone.web.ajax.MoveProfileFieldAjaxPage;
123 import net.pterodactylus.sone.web.ajax.TrustAjaxPage;
124 import net.pterodactylus.sone.web.ajax.UnbookmarkAjaxPage;
125 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
126 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
127 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
128 import net.pterodactylus.sone.web.ajax.UntrustAjaxPage;
129 import net.pterodactylus.sone.web.page.FreenetRequest;
130 import net.pterodactylus.sone.web.page.PageToadlet;
131 import net.pterodactylus.sone.web.page.PageToadletFactory;
132 import net.pterodactylus.util.io.Closer;
133 import net.pterodactylus.util.notify.Notification;
134 import net.pterodactylus.util.notify.NotificationManager;
135 import net.pterodactylus.util.notify.TemplateNotification;
136 import net.pterodactylus.util.template.ClassPathTemplateProvider;
137 import net.pterodactylus.util.template.CollectionSortFilter;
138 import net.pterodactylus.util.template.ContainsFilter;
139 import net.pterodactylus.util.template.DateFilter;
140 import net.pterodactylus.util.template.FormatFilter;
141 import net.pterodactylus.util.template.HtmlFilter;
142 import net.pterodactylus.util.template.MatchFilter;
143 import net.pterodactylus.util.template.ModFilter;
144 import net.pterodactylus.util.template.PaginationFilter;
145 import net.pterodactylus.util.template.ReflectionAccessor;
146 import net.pterodactylus.util.template.ReplaceFilter;
147 import net.pterodactylus.util.template.StoreFilter;
148 import net.pterodactylus.util.template.Template;
149 import net.pterodactylus.util.template.TemplateContextFactory;
150 import net.pterodactylus.util.template.TemplateProvider;
151 import net.pterodactylus.util.template.XmlFilter;
152 import net.pterodactylus.util.web.RedirectPage;
153 import net.pterodactylus.util.web.StaticPage;
154 import net.pterodactylus.util.web.TemplatePage;
156 import com.google.common.base.Optional;
157 import com.google.common.collect.Collections2;
158 import com.google.common.collect.ImmutableSet;
159 import com.google.common.eventbus.Subscribe;
160 import com.google.inject.Inject;
162 import freenet.clients.http.SessionManager;
163 import freenet.clients.http.SessionManager.Session;
164 import freenet.clients.http.ToadletContainer;
165 import freenet.clients.http.ToadletContext;
166 import freenet.l10n.BaseL10n;
167 import freenet.support.api.HTTPRequest;
170 * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
171 * references to l10n helpers.
173 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
175 public class WebInterface {
178 private static final Logger logger = getLogger("Sone.Web.Main");
180 /** The notification manager. */
181 private final NotificationManager notificationManager = new NotificationManager();
183 /** The Sone plugin. */
184 private final SonePlugin sonePlugin;
186 /** The registered toadlets. */
187 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
189 /** The form password. */
190 private final String formPassword;
192 /** The template context factory. */
193 private final TemplateContextFactory templateContextFactory;
195 /** The Sone text parser. */
196 private final SoneTextParser soneTextParser;
198 /** The parser filter. */
199 private final ParserFilter parserFilter;
201 /** The “new Sone” notification. */
202 private final ListNotification<Sone> newSoneNotification;
204 /** The “new post” notification. */
205 private final ListNotification<Post> newPostNotification;
207 /** The “new reply” notification. */
208 private final ListNotification<PostReply> newReplyNotification;
210 /** The invisible “local post” notification. */
211 private final ListNotification<Post> localPostNotification;
213 /** The invisible “local reply” notification. */
214 private final ListNotification<PostReply> localReplyNotification;
216 /** The “you have been mentioned” notification. */
217 private final ListNotification<Post> mentionNotification;
219 /** Notifications for sone inserts. */
220 private final Map<Sone, TemplateNotification> soneInsertNotifications = new HashMap<Sone, TemplateNotification>();
222 /** Sone locked notification ticker objects. */
223 private final Map<Sone, ScheduledFuture<?>> lockedSonesTickerObjects = Collections.synchronizedMap(new HashMap<Sone, ScheduledFuture<?>>());
225 /** The “Sone locked” notification. */
226 private final ListNotification<Sone> lockedSonesNotification;
228 /** The “new version” notification. */
229 private final TemplateNotification newVersionNotification;
231 /** The “inserting images” notification. */
232 private final ListNotification<Image> insertingImagesNotification;
234 /** The “inserted images” notification. */
235 private final ListNotification<Image> insertedImagesNotification;
237 /** The “image insert failed” notification. */
238 private final ListNotification<Image> imageInsertFailedNotification;
240 /** Scheduled executor for time-based notifications. */
241 private final ScheduledExecutorService ticker = Executors.newScheduledThreadPool(1);
244 * Creates a new web interface.
250 public WebInterface(SonePlugin sonePlugin) {
251 this.sonePlugin = sonePlugin;
252 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
253 soneTextParser = new SoneTextParser(getCore(), getCore());
255 templateContextFactory = new TemplateContextFactory();
256 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
257 templateContextFactory.addAccessor(Collection.class, new CollectionAccessor());
258 templateContextFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
259 templateContextFactory.addAccessor(Post.class, new PostAccessor(getCore()));
260 templateContextFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
261 templateContextFactory.addAccessor(Album.class, new AlbumAccessor());
262 templateContextFactory.addAccessor(Image.class, new ImageAccessor());
263 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
264 templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
265 templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
266 templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
267 templateContextFactory.addFilter("date", new DateFilter());
268 templateContextFactory.addFilter("html", new HtmlFilter());
269 templateContextFactory.addFilter("replace", new ReplaceFilter());
270 templateContextFactory.addFilter("store", new StoreFilter());
271 templateContextFactory.addFilter("l10n", new L10nFilter(this));
272 templateContextFactory.addFilter("substring", new SubstringFilter());
273 templateContextFactory.addFilter("xml", new XmlFilter());
274 templateContextFactory.addFilter("change", new RequestChangeFilter());
275 templateContextFactory.addFilter("match", new MatchFilter());
276 templateContextFactory.addFilter("css", new CssClassNameFilter());
277 templateContextFactory.addFilter("js", new JavascriptFilter());
278 templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
279 templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
280 templateContextFactory.addFilter("format", new FormatFilter());
281 templateContextFactory.addFilter("sort", new CollectionSortFilter());
282 templateContextFactory.addFilter("image-link", new ImageLinkFilter(getCore(), templateContextFactory));
283 templateContextFactory.addFilter("replyGroup", new ReplyGroupFilter());
284 templateContextFactory.addFilter("in", new ContainsFilter());
285 templateContextFactory.addFilter("unique", new UniqueElementFilter());
286 templateContextFactory.addFilter("mod", new ModFilter());
287 templateContextFactory.addFilter("paginate", new PaginationFilter());
288 templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
289 templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/"));
290 templateContextFactory.addTemplateObject("webInterface", this);
291 templateContextFactory.addTemplateObject("formPassword", formPassword);
293 /* create notifications. */
294 Template newSoneNotificationTemplate = parseTemplate("/templates/notify/newSoneNotification.html");
295 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate, false);
297 Template newPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html");
298 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate, false);
300 Template localPostNotificationTemplate = parseTemplate("/templates/notify/newPostNotification.html");
301 localPostNotification = new ListNotification<Post>("local-post-notification", "posts", localPostNotificationTemplate, false);
303 Template newReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html");
304 newReplyNotification = new ListNotification<PostReply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
306 Template localReplyNotificationTemplate = parseTemplate("/templates/notify/newReplyNotification.html");
307 localReplyNotification = new ListNotification<PostReply>("local-reply-notification", "replies", localReplyNotificationTemplate, false);
309 Template mentionNotificationTemplate = parseTemplate("/templates/notify/mentionNotification.html");
310 mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
312 Template lockedSonesTemplate = parseTemplate("/templates/notify/lockedSonesNotification.html");
313 lockedSonesNotification = new ListNotification<Sone>("sones-locked-notification", "sones", lockedSonesTemplate);
315 Template newVersionTemplate = parseTemplate("/templates/notify/newVersionNotification.html");
316 newVersionNotification = new TemplateNotification("new-version-notification", newVersionTemplate);
318 Template insertingImagesTemplate = parseTemplate("/templates/notify/inserting-images-notification.html");
319 insertingImagesNotification = new ListNotification<Image>("inserting-images-notification", "images", insertingImagesTemplate);
321 Template insertedImagesTemplate = parseTemplate("/templates/notify/inserted-images-notification.html");
322 insertedImagesNotification = new ListNotification<Image>("inserted-images-notification", "images", insertedImagesTemplate);
324 Template imageInsertFailedTemplate = parseTemplate("/templates/notify/image-insert-failed-notification.html");
325 imageInsertFailedNotification = new ListNotification<Image>("image-insert-failed-notification", "images", imageInsertFailedTemplate);
328 private Template parseTemplate(String resourceName) {
329 InputStream templateInputStream = null;
330 Reader reader = null;
332 templateInputStream = getClass().getResourceAsStream(resourceName);
333 reader = new InputStreamReader(templateInputStream, "UTF-8");
334 return parse(reader);
335 } catch (UnsupportedEncodingException uee1) {
336 throw new RuntimeException("UTF-8 not supported.");
338 Closer.close(reader);
339 Closer.close(templateInputStream);
348 * Returns the Sone core used by the Sone plugin.
350 * @return The Sone core
352 public Core getCore() {
353 return sonePlugin.core();
357 * Returns the template context factory of the web interface.
359 * @return The template context factory
361 public TemplateContextFactory getTemplateContextFactory() {
362 return templateContextFactory;
365 public Session getCurrentSession(ToadletContext toadletContenxt) {
366 return getCurrentSession(toadletContenxt, true).get();
369 public Optional<Session> getCurrentSession(ToadletContext toadletContenxt, boolean createSession) {
370 Session session = getSessionManager().useSession(toadletContenxt);
371 if (createSession && (session == null)) {
372 session = getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt);
374 return Optional.fromNullable(session);
378 * Returns the currently logged in Sone.
380 * @param toadletContext
381 * The toadlet context
382 * @return The currently logged in Sone, or {@code null} if no Sone is
383 * currently logged in
385 public Optional<LocalSone> getCurrentSone(ToadletContext toadletContext) {
386 return getCurrentSone(toadletContext, true);
390 * Returns the currently logged in Sone.
392 * @param toadletContext
393 * The toadlet context
394 * @param createSession
395 * {@code true} to create a new session if no session exists,
396 * {@code false} to not create a new session
397 * @return The currently logged in Sone, or {@code null} if no Sone is
398 * currently logged in
400 public Optional<LocalSone> getCurrentSone(ToadletContext toadletContext, boolean createSession) {
401 Collection<LocalSone> localSones = getCore().getLocalSones();
402 if (localSones.size() == 1) {
403 return Optional.of(localSones.iterator().next());
405 return getCurrentSone(getCurrentSession(toadletContext, createSession));
409 * Returns the currently logged in Sone.
413 * @return The currently logged in Sone, or {@code null} if no Sone is
414 * currently logged in
416 public Optional<LocalSone> getCurrentSone(Optional<Session> session) {
417 if (!session.isPresent()) {
418 return Optional.absent();
420 String soneId = (String) session.get().getAttribute("Sone.CurrentSone");
421 if (soneId == null) {
422 return Optional.absent();
424 return getCore().getLocalSone(soneId);
428 * Sets the currently logged in Sone.
430 * @param toadletContext
431 * The toadlet context
433 * The Sone to set as currently logged in
435 public void setCurrentSone(ToadletContext toadletContext, Sone sone) {
436 Session session = getCurrentSession(toadletContext);
438 session.removeAttribute("Sone.CurrentSone");
440 session.setAttribute("Sone.CurrentSone", sone.getId());
445 * Returns the notification manager.
447 * @return The notification manager
449 public NotificationManager getNotifications() {
450 return notificationManager;
454 * Returns the l10n helper of the node.
456 * @return The node’s l10n helper
458 public BaseL10n getL10n() {
459 return sonePlugin.l10n().getBase();
463 * Returns the session manager of the node.
465 * @return The node’s session manager
467 public SessionManager getSessionManager() {
468 return sonePlugin.pluginRespirator().getSessionManager("Sone");
472 * Returns the node’s form password.
474 * @return The form password
476 public String getFormPassword() {
481 * Returns the posts that have been announced as new in the
482 * {@link #newPostNotification}.
484 * @return The new posts
486 public Set<Post> getNewPosts() {
487 return ImmutableSet.<Post> builder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).build();
491 * Returns the replies that have been announced as new in the
492 * {@link #newReplyNotification}.
494 * @return The new replies
496 public Set<PostReply> getNewReplies() {
497 return ImmutableSet.<PostReply> builder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).build();
501 * Sets whether the current start of the plugin is the first start. It is
502 * considered a first start if the configuration file does not exist.
505 * {@code true} if no configuration file existed when Sone was
506 * loaded, {@code false} otherwise
508 public void setFirstStart(boolean firstStart) {
510 Template firstStartNotificationTemplate = parseTemplate("/templates/notify/firstStartNotification.html");
511 Notification firstStartNotification = new TemplateNotification("first-start-notification", firstStartNotificationTemplate);
512 notificationManager.addNotification(firstStartNotification);
517 * Sets whether Sone was started with a fresh configuration file.
520 * {@code true} if Sone was started with a fresh configuration,
521 * {@code false} if the existing configuration could be read
523 public void setNewConfig(boolean newConfig) {
524 if (newConfig && !hasFirstStartNotification()) {
525 Template configNotReadNotificationTemplate = parseTemplate("/templates/notify/configNotReadNotification.html");
526 Notification configNotReadNotification = new TemplateNotification("config-not-read-notification", configNotReadNotificationTemplate);
527 notificationManager.addNotification(configNotReadNotification);
536 * Returns whether the first start notification is currently displayed.
538 * @return {@code true} if the first-start notification is currently
539 * displayed, {@code false} otherwise
541 private boolean hasFirstStartNotification() {
542 return notificationManager.getNotification("first-start-notification") != null;
550 * Starts the web interface and registers all toadlets.
552 public void start() {
555 /* notification templates. */
556 Template startupNotificationTemplate = parseTemplate("/templates/notify/startupNotification.html");
558 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
559 notificationManager.addNotification(startupNotification);
561 ticker.schedule(new Runnable() {
565 startupNotification.dismiss();
567 }, 2, TimeUnit.MINUTES);
569 Template wotMissingNotificationTemplate = parseTemplate("/templates/notify/wotMissingNotification.html");
570 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
571 ticker.scheduleAtFixedRate(new Runnable() {
574 @SuppressWarnings("synthetic-access")
576 if (getCore().getIdentityManager().isConnected()) {
577 wotMissingNotification.dismiss();
579 notificationManager.addNotification(wotMissingNotification);
583 }, 15, 15, TimeUnit.SECONDS);
587 * Stops the web interface and unregisters all toadlets.
590 unregisterToadlets();
591 ticker.shutdownNow();
599 * Register all toadlets.
601 private void registerToadlets() {
602 Template emptyTemplate = parse(new StringReader(""));
603 Template loginTemplate = parseTemplate("/templates/login.html");
604 Template indexTemplate = parseTemplate("/templates/index.html");
605 Template newTemplate = parseTemplate("/templates/new.html");
606 Template knownSonesTemplate = parseTemplate("/templates/knownSones.html");
607 Template createSoneTemplate = parseTemplate("/templates/createSone.html");
608 Template createPostTemplate = parseTemplate("/templates/createPost.html");
609 Template createReplyTemplate = parseTemplate("/templates/createReply.html");
610 Template bookmarksTemplate = parseTemplate("/templates/bookmarks.html");
611 Template searchTemplate = parseTemplate("/templates/search.html");
612 Template editProfileTemplate = parseTemplate("/templates/editProfile.html");
613 Template editProfileFieldTemplate = parseTemplate("/templates/editProfileField.html");
614 Template deleteProfileFieldTemplate = parseTemplate("/templates/deleteProfileField.html");
615 Template viewSoneTemplate = parseTemplate("/templates/viewSone.html");
616 Template viewPostTemplate = parseTemplate("/templates/viewPost.html");
617 Template deletePostTemplate = parseTemplate("/templates/deletePost.html");
618 Template deleteReplyTemplate = parseTemplate("/templates/deleteReply.html");
619 Template deleteSoneTemplate = parseTemplate("/templates/deleteSone.html");
620 Template imageBrowserTemplate = parseTemplate("/templates/imageBrowser.html");
621 Template createAlbumTemplate = parseTemplate("/templates/createAlbum.html");
622 Template deleteAlbumTemplate = parseTemplate("/templates/deleteAlbum.html");
623 Template deleteImageTemplate = parseTemplate("/templates/deleteImage.html");
624 Template noPermissionTemplate = parseTemplate("/templates/noPermission.html");
625 Template emptyImageTitleTemplate = parseTemplate("/templates/emptyImageTitle.html");
626 Template emptyAlbumTitleTemplate = parseTemplate("/templates/emptyAlbumTitle.html");
627 Template optionsTemplate = parseTemplate("/templates/options.html");
628 Template rescueTemplate = parseTemplate("/templates/rescue.html");
629 Template aboutTemplate = parseTemplate("/templates/about.html");
630 Template invalidTemplate = parseTemplate("/templates/invalid.html");
631 Template postTemplate = parseTemplate("/templates/include/viewPost.html");
632 Template replyTemplate = parseTemplate("/templates/include/viewReply.html");
633 Template openSearchTemplate = parseTemplate("/templates/xml/OpenSearch.xml");
635 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
636 pageToadlets.add(pageToadletFactory.createPageToadlet(new RedirectPage<FreenetRequest>("", "index.html")));
637 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
638 pageToadlets.add(pageToadletFactory.createPageToadlet(new NewPage(newTemplate, this), "New"));
639 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
640 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
641 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
642 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldPage(editProfileFieldTemplate, this)));
643 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldPage(deleteProfileFieldTemplate, this)));
644 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
645 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
646 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
647 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
648 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(emptyTemplate, this)));
649 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(emptyTemplate, this)));
650 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
651 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
652 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(emptyTemplate, this)));
653 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(emptyTemplate, this)));
654 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(emptyTemplate, this)));
655 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(emptyTemplate, this)));
656 pageToadlets.add(pageToadletFactory.createPageToadlet(new ImageBrowserPage(imageBrowserTemplate, this), "ImageBrowser"));
657 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateAlbumPage(createAlbumTemplate, this)));
658 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumPage(emptyTemplate, this)));
659 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteAlbumPage(deleteAlbumTemplate, this)));
660 pageToadlets.add(pageToadletFactory.createPageToadlet(new UploadImagePage(invalidTemplate, this)));
661 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImagePage(emptyTemplate, this)));
662 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteImagePage(deleteImageTemplate, this)));
663 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustPage(emptyTemplate, this)));
664 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustPage(emptyTemplate, this)));
665 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustPage(emptyTemplate, this)));
666 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownPage(emptyTemplate, this)));
667 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkPage(emptyTemplate, this)));
668 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkPage(emptyTemplate, this)));
669 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarksPage(bookmarksTemplate, this), "Bookmarks"));
670 pageToadlets.add(pageToadletFactory.createPageToadlet(new SearchPage(searchTemplate, this)));
671 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
672 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
673 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout"));
674 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
675 pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue"));
676 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
677 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
678 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this)));
679 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this)));
680 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this)));
681 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this)));
682 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
683 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("javascript/", "/static/javascript/", "text/javascript")));
684 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("images/", "/static/images/", "image/png")));
685 pageToadlets.add(pageToadletFactory.createPageToadlet(new TemplatePage<FreenetRequest>("OpenSearch.xml", "application/opensearchdescription+xml", templateContextFactory, openSearchTemplate)));
686 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetImagePage(this)));
687 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
688 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
689 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
690 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
691 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
692 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
693 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
694 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
695 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTimesAjaxPage(this)));
696 pageToadlets.add(pageToadletFactory.createPageToadlet(new MarkAsKnownAjaxPage(this)));
697 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
698 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
699 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
700 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
701 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
702 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
703 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
704 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter)));
705 pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
706 pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
707 pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
708 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
709 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
710 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
711 pageToadlets.add(pageToadletFactory.createPageToadlet(new BookmarkAjaxPage(this)));
712 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnbookmarkAjaxPage(this)));
713 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfileFieldAjaxPage(this)));
714 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteProfileFieldAjaxPage(this)));
715 pageToadlets.add(pageToadletFactory.createPageToadlet(new MoveProfileFieldAjaxPage(this)));
717 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
718 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Sone.Name", "Navigation.Menu.Sone.Tooltip", sonePlugin);
719 for (PageToadlet toadlet : pageToadlets) {
720 String menuName = toadlet.getMenuName();
721 if (menuName != null) {
722 toadletContainer.register(toadlet, "Navigation.Menu.Sone.Name", toadlet.path(), true, "Navigation.Menu.Sone.Item." + menuName + ".Name", "Navigation.Menu.Sone.Item." + menuName + ".Tooltip", false, toadlet);
724 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
730 * Unregisters all toadlets.
732 private void unregisterToadlets() {
733 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
734 for (PageToadlet pageToadlet : pageToadlets) {
735 toadletContainer.unregister(pageToadlet);
737 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Sone.Name");
741 * Returns all {@link Sone#isLocal() local Sone}s that are referenced by
742 * {@link SonePart}s in the given text (after parsing it using
743 * {@link SoneTextParser}).
747 * @return All mentioned local Sones
749 private Collection<Sone> getMentionedSones(String text) {
750 /* we need no context to find mentioned Sones. */
751 Set<Sone> mentionedSones = new HashSet<Sone>();
753 for (Part part : soneTextParser.parse(null, new StringReader(text))) {
754 if (part instanceof SonePart) {
755 mentionedSones.add(((SonePart) part).getSone());
758 } catch (IOException ioe1) {
759 logger.log(Level.WARNING, String.format("Could not parse post text: %s", text), ioe1);
761 return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
765 * Returns the Sone insert notification for the given Sone. If no
766 * notification for the given Sone exists, a new notification is created and
770 * The Sone to get the insert notification for
771 * @return The Sone insert notification
773 private TemplateNotification getSoneInsertNotification(Sone sone) {
774 synchronized (soneInsertNotifications) {
775 TemplateNotification templateNotification = soneInsertNotifications.get(sone);
776 if (templateNotification == null) {
777 templateNotification = new TemplateNotification(parseTemplate("/templates/notify/soneInsertNotification.html"));
778 templateNotification.set("insertSone", sone);
779 soneInsertNotifications.put(sone, templateNotification);
781 return templateNotification;
785 private boolean localSoneMentionedInNewPostOrReply(Post post) {
786 if (!post.getSone().isLocal()) {
787 if (!getMentionedSones(post.getText()).isEmpty() && !post.isKnown()) {
791 for (PostReply postReply : getCore().getReplies(post.getId())) {
792 if (postReply.getSone().isLocal()) {
795 if (!getMentionedSones(postReply.getText()).isEmpty() && !postReply.isKnown()) {
807 * Notifies the web interface that a new {@link Sone} was found.
809 * @param newSoneFoundEvent
813 public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
814 newSoneNotification.add(newSoneFoundEvent.sone());
815 if (!hasFirstStartNotification()) {
816 notificationManager.addNotification(newSoneNotification);
821 * Notifies the web interface that a new {@link Post} was found.
823 * @param newPostFoundEvent
827 public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
828 Post post = newPostFoundEvent.post();
829 boolean isLocal = post.getSone().isLocal();
831 localPostNotification.add(post);
833 newPostNotification.add(post);
835 if (!hasFirstStartNotification()) {
836 notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
837 if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
838 mentionNotification.add(post);
839 notificationManager.addNotification(mentionNotification);
842 getCore().markPostKnown(post);
847 * Notifies the web interface that a new {@link PostReply} was found.
849 * @param newPostReplyFoundEvent
853 public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
854 PostReply reply = newPostReplyFoundEvent.postReply();
855 boolean isLocal = reply.getSone().isLocal();
857 localReplyNotification.add(reply);
859 newReplyNotification.add(reply);
861 if (!hasFirstStartNotification()) {
862 notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
863 if (reply.getPost().isPresent() && localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
864 mentionNotification.add(reply.getPost().get());
865 notificationManager.addNotification(mentionNotification);
868 getCore().markReplyKnown(reply);
873 * Notifies the web interface that a {@link Sone} was marked as known.
875 * @param markSoneKnownEvent
879 public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
880 newSoneNotification.remove(markSoneKnownEvent.sone());
884 * Notifies the web interface that a {@link Post} was marked as known.
886 * @param markPostKnownEvent
890 public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
891 newPostNotification.remove(markPostKnownEvent.post());
892 localPostNotification.remove(markPostKnownEvent.post());
893 if (!localSoneMentionedInNewPostOrReply(markPostKnownEvent.post())) {
894 mentionNotification.remove(markPostKnownEvent.post());
899 * Notifies the web interface that a {@link PostReply} was marked as known.
901 * @param markPostReplyKnownEvent
905 public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
906 PostReply postReply = markPostReplyKnownEvent.postReply();
907 newReplyNotification.remove(postReply);
908 localReplyNotification.remove(postReply);
909 if (postReply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(postReply.getPost().get())) {
910 mentionNotification.remove(postReply.getPost().get());
915 * Notifies the web interface that a {@link Sone} was removed.
917 * @param soneRemovedEvent
921 public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
922 newSoneNotification.remove(soneRemovedEvent.sone());
926 * Notifies the web interface that a {@link Post} was removed.
928 * @param postRemovedEvent
932 public void postRemoved(PostRemovedEvent postRemovedEvent) {
933 newPostNotification.remove(postRemovedEvent.post());
934 localPostNotification.remove(postRemovedEvent.post());
935 if (!localSoneMentionedInNewPostOrReply(postRemovedEvent.post())) {
936 mentionNotification.remove(postRemovedEvent.post());
941 * Notifies the web interface that a {@link PostReply} was removed.
943 * @param postReplyRemovedEvent
947 public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
948 PostReply reply = postReplyRemovedEvent.postReply();
949 newReplyNotification.remove(reply);
950 localReplyNotification.remove(reply);
951 if (reply.getPost().isPresent() && !localSoneMentionedInNewPostOrReply(reply.getPost().get())) {
952 mentionNotification.remove(reply.getPost().get());
957 * Notifies the web interface that a Sone was locked.
959 * @param soneLockedEvent
963 public void soneLocked(SoneLockedEvent soneLockedEvent) {
964 final Sone sone = soneLockedEvent.sone();
965 ScheduledFuture<?> tickerObject = ticker.schedule(new Runnable() {
968 @SuppressWarnings("synthetic-access")
970 lockedSonesNotification.add(sone);
971 notificationManager.addNotification(lockedSonesNotification);
973 }, 5, TimeUnit.MINUTES);
974 lockedSonesTickerObjects.put(sone, tickerObject);
978 * Notifies the web interface that a Sone was unlocked.
980 * @param soneUnlockedEvent
984 public void soneUnlocked(SoneUnlockedEvent soneUnlockedEvent) {
985 lockedSonesNotification.remove(soneUnlockedEvent.sone());
986 lockedSonesTickerObjects.remove(soneUnlockedEvent.sone()).cancel(false);
990 * Notifies the web interface that a {@link Sone} is being inserted.
992 * @param soneInsertingEvent
996 public void soneInserting(SoneInsertingEvent soneInsertingEvent) {
997 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertingEvent.sone());
998 soneInsertNotification.set("soneStatus", "inserting");
999 if (soneInsertingEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1000 notificationManager.addNotification(soneInsertNotification);
1005 * Notifies the web interface that a {@link Sone} was inserted.
1007 * @param soneInsertedEvent
1011 public void soneInserted(SoneInsertedEvent soneInsertedEvent) {
1012 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertedEvent.sone());
1013 soneInsertNotification.set("soneStatus", "inserted");
1014 soneInsertNotification.set("insertDuration", soneInsertedEvent.insertDuration() / 1000);
1015 if (soneInsertedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1016 notificationManager.addNotification(soneInsertNotification);
1021 * Notifies the web interface that a {@link Sone} insert was aborted.
1023 * @param soneInsertAbortedEvent
1027 public void soneInsertAborted(SoneInsertAbortedEvent soneInsertAbortedEvent) {
1028 TemplateNotification soneInsertNotification = getSoneInsertNotification(soneInsertAbortedEvent.sone());
1029 soneInsertNotification.set("soneStatus", "insert-aborted");
1030 soneInsertNotification.set("insert-error", soneInsertAbortedEvent.cause());
1031 if (soneInsertAbortedEvent.sone().getOptions().isSoneInsertNotificationEnabled()) {
1032 notificationManager.addNotification(soneInsertNotification);
1037 * Notifies the web interface that a new Sone version was found.
1039 * @param updateFoundEvent
1043 public void updateFound(UpdateFoundEvent updateFoundEvent) {
1044 newVersionNotification.getTemplateContext().set("latestVersion", updateFoundEvent.version());
1045 newVersionNotification.getTemplateContext().set("latestEdition", updateFoundEvent.latestEdition());
1046 newVersionNotification.getTemplateContext().set("releaseTime", updateFoundEvent.releaseTime());
1047 notificationManager.addNotification(newVersionNotification);
1051 * Notifies the web interface that an image insert was started
1053 * @param imageInsertStartedEvent
1057 public void imageInsertStarted(ImageInsertStartedEvent imageInsertStartedEvent) {
1058 insertingImagesNotification.add(imageInsertStartedEvent.image());
1059 notificationManager.addNotification(insertingImagesNotification);
1063 * Notifies the web interface that an {@link Image} insert was aborted.
1065 * @param imageInsertAbortedEvent
1069 public void imageInsertAborted(ImageInsertAbortedEvent imageInsertAbortedEvent) {
1070 insertingImagesNotification.remove(imageInsertAbortedEvent.image());
1074 * Notifies the web interface that an {@link Image} insert is finished.
1076 * @param imageInsertFinishedEvent
1080 public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) {
1081 insertingImagesNotification.remove(imageInsertFinishedEvent.image());
1082 insertedImagesNotification.add(imageInsertFinishedEvent.image());
1083 notificationManager.addNotification(insertedImagesNotification);
1087 * Notifies the web interface that an {@link Image} insert has failed.
1089 * @param imageInsertFailedEvent
1093 public void imageInsertFailed(ImageInsertFailedEvent imageInsertFailedEvent) {
1094 insertingImagesNotification.remove(imageInsertFailedEvent.image());
1095 imageInsertFailedNotification.add(imageInsertFailedEvent.image());
1096 notificationManager.addNotification(imageInsertFailedNotification);