Don’t show “web of trust is missing” notification the old way.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - WebInterface.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import net.pterodactylus.sone.core.Core;
33 import net.pterodactylus.sone.core.CoreListener;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Reply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.freenet.L10nFilter;
38 import net.pterodactylus.sone.freenet.wot.Identity;
39 import net.pterodactylus.sone.main.SonePlugin;
40 import net.pterodactylus.sone.notify.NewPostNotification;
41 import net.pterodactylus.sone.notify.NewReplyNotification;
42 import net.pterodactylus.sone.notify.NewSoneNotification;
43 import net.pterodactylus.sone.template.CollectionAccessor;
44 import net.pterodactylus.sone.template.CssClassNameFilter;
45 import net.pterodactylus.sone.template.GetPagePlugin;
46 import net.pterodactylus.sone.template.IdentityAccessor;
47 import net.pterodactylus.sone.template.NotificationManagerAccessor;
48 import net.pterodactylus.sone.template.PostAccessor;
49 import net.pterodactylus.sone.template.ReplyAccessor;
50 import net.pterodactylus.sone.template.RequestChangeFilter;
51 import net.pterodactylus.sone.template.SoneAccessor;
52 import net.pterodactylus.sone.template.SubstringFilter;
53 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
54 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
55 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
56 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
57 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
58 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetNotificationsAjaxPage;
60 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
61 import net.pterodactylus.sone.web.ajax.GetSoneStatusPage;
62 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
63 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
64 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
65 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
66 import net.pterodactylus.sone.web.page.PageToadlet;
67 import net.pterodactylus.sone.web.page.PageToadletFactory;
68 import net.pterodactylus.sone.web.page.StaticPage;
69 import net.pterodactylus.util.logging.Logging;
70 import net.pterodactylus.util.notify.NotificationManager;
71 import net.pterodactylus.util.notify.TemplateNotification;
72 import net.pterodactylus.util.template.DateFilter;
73 import net.pterodactylus.util.template.DefaultTemplateFactory;
74 import net.pterodactylus.util.template.MatchFilter;
75 import net.pterodactylus.util.template.PaginationPlugin;
76 import net.pterodactylus.util.template.ReflectionAccessor;
77 import net.pterodactylus.util.template.Template;
78 import net.pterodactylus.util.template.TemplateException;
79 import net.pterodactylus.util.template.TemplateFactory;
80 import net.pterodactylus.util.template.TemplateProvider;
81 import net.pterodactylus.util.template.XmlFilter;
82 import net.pterodactylus.util.thread.Ticker;
83 import freenet.clients.http.SessionManager;
84 import freenet.clients.http.ToadletContainer;
85 import freenet.l10n.BaseL10n;
86
87 /**
88  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
89  * references to l10n helpers.
90  *
91  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
92  */
93 public class WebInterface implements CoreListener {
94
95         /** The logger. */
96         private static final Logger logger = Logging.getLogger(WebInterface.class);
97
98         /** The notification manager. */
99         private final NotificationManager notificationManager = new NotificationManager();
100
101         /** The Sone plugin. */
102         private final SonePlugin sonePlugin;
103
104         /** The registered toadlets. */
105         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
106
107         /** The form password. */
108         private final String formPassword;
109
110         /** The template factory. */
111         private DefaultTemplateFactory templateFactory;
112
113         /** The “new Sone” notification. */
114         private final NewSoneNotification newSoneNotification;
115
116         /** The “new post” notification. */
117         private final NewPostNotification newPostNotification;
118
119         /** The “new reply” notification. */
120         private final NewReplyNotification newReplyNotification;
121
122         /**
123          * Creates a new web interface.
124          *
125          * @param sonePlugin
126          *            The Sone plugin
127          */
128         public WebInterface(SonePlugin sonePlugin) {
129                 this.sonePlugin = sonePlugin;
130                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
131
132                 templateFactory = new DefaultTemplateFactory();
133                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
134                 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
135                 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
136                 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
137                 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
138                 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
139                 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
140                 templateFactory.addFilter("date", new DateFilter());
141                 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
142                 templateFactory.addFilter("substring", new SubstringFilter());
143                 templateFactory.addFilter("xml", new XmlFilter());
144                 templateFactory.addFilter("change", new RequestChangeFilter());
145                 templateFactory.addFilter("match", new MatchFilter());
146                 templateFactory.addFilter("css", new CssClassNameFilter());
147                 templateFactory.addPlugin("getpage", new GetPagePlugin());
148                 templateFactory.addPlugin("paginate", new PaginationPlugin());
149                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
150                 templateFactory.addTemplateObject("formPassword", formPassword);
151
152                 /* create notifications. */
153                 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
154                 newSoneNotification = new NewSoneNotification(newSoneNotificationTemplate);
155
156                 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
157                 newPostNotification = new NewPostNotification(newPostNotificationTemplate);
158
159                 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
160                 newReplyNotification = new NewReplyNotification(newReplyNotificationTemplate);
161         }
162
163         //
164         // ACCESSORS
165         //
166
167         /**
168          * Returns the Sone core used by the Sone plugin.
169          *
170          * @return The Sone core
171          */
172         public Core getCore() {
173                 return sonePlugin.core();
174         }
175
176         /**
177          * Returns the notification manager.
178          *
179          * @return The notification manager
180          */
181         public NotificationManager getNotifications() {
182                 return notificationManager;
183         }
184
185         /**
186          * Returns the l10n helper of the node.
187          *
188          * @return The node’s l10n helper
189          */
190         public BaseL10n getL10n() {
191                 return sonePlugin.l10n().getBase();
192         }
193
194         /**
195          * Returns the session manager of the node.
196          *
197          * @return The node’s session manager
198          */
199         public SessionManager getSessionManager() {
200                 try {
201                         return sonePlugin.pluginRespirator().getSessionManager(new URI("/"));
202                 } catch (URISyntaxException use1) {
203                         logger.log(Level.SEVERE, "Could not get Session Manager!", use1);
204                         return null;
205                 }
206         }
207
208         /**
209          * Returns the node’s form password.
210          *
211          * @return The form password
212          */
213         public String getFormPassword() {
214                 return formPassword;
215         }
216
217         //
218         // ACTIONS
219         //
220
221         /**
222          * Starts the web interface and registers all toadlets.
223          */
224         public void start() {
225                 registerToadlets();
226
227                 /* notification templates. */
228                 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
229
230                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
231                 notificationManager.addNotification(startupNotification);
232
233                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
234
235                         @Override
236                         public void run() {
237                                 startupNotification.dismiss();
238                         }
239                 }, "Sone Startup Notification Remover");
240         }
241
242         /**
243          * Stops the web interface and unregisters all toadlets.
244          */
245         public void stop() {
246                 unregisterToadlets();
247                 Ticker.getInstance().stop();
248         }
249
250         //
251         // PRIVATE METHODS
252         //
253
254         /**
255          * Register all toadlets.
256          */
257         private void registerToadlets() {
258                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
259                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
260                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
261                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
262                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
263                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
264                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
265                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
266                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
267                 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
268                 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
269                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
270                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
271                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
272                 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
273                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
274                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
275                 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
276                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
277                 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
278                 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
279                 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
280
281                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
282                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
283                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
284                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
285                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
286                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
287                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
288                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
289                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
290                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
291                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
292                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
293                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
294                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
295                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
296                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
297                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
298                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
299                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
300                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
301                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
302                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
303                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
304                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
305                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
306                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
307                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetNotificationsAjaxPage(this)));
308                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
309                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetSoneStatusPage(this)));
310                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
311                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
312                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
313                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
314                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
315                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
316                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
317                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
318                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
319
320                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
321                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
322                 for (PageToadlet toadlet : pageToadlets) {
323                         String menuName = toadlet.getMenuName();
324                         if (menuName != null) {
325                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
326                         } else {
327                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
328                         }
329                 }
330         }
331
332         /**
333          * Unregisters all toadlets.
334          */
335         private void unregisterToadlets() {
336                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
337                 for (PageToadlet pageToadlet : pageToadlets) {
338                         toadletContainer.unregister(pageToadlet);
339                 }
340                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
341         }
342
343         /**
344          * Creates a {@link Reader} from the {@link InputStream} for the resource
345          * with the given name.
346          *
347          * @param resourceName
348          *            The name of the resource
349          * @return A {@link Reader} for the resource
350          */
351         private Reader createReader(String resourceName) {
352                 try {
353                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
354                 } catch (UnsupportedEncodingException uee1) {
355                         return null;
356                 }
357         }
358
359         //
360         // CORELISTENER METHODS
361         //
362
363         /**
364          * {@inheritDoc}
365          */
366         @Override
367         public void newSoneFound(Sone sone) {
368                 newSoneNotification.addSone(sone);
369                 notificationManager.addNotification(newSoneNotification);
370         }
371
372         /**
373          * {@inheritDoc}
374          */
375         @Override
376         public void newPostFound(Post post) {
377                 newPostNotification.addPost(post);
378                 notificationManager.addNotification(newPostNotification);
379         }
380
381         /**
382          * {@inheritDoc}
383          */
384         @Override
385         public void newReplyFound(Reply reply) {
386                 if (reply.getPost().getSone() == null) {
387                         return;
388                 }
389                 newReplyNotification.addReply(reply);
390                 notificationManager.addNotification(newReplyNotification);
391         }
392
393         /**
394          * {@inheritDoc}
395          */
396         @Override
397         public void markSoneKnown(Sone sone) {
398                 newSoneNotification.removeSone(sone);
399                 if (newSoneNotification.isEmpty()) {
400                         newSoneNotification.dismiss();
401                 }
402         }
403
404         /**
405          * {@inheritDoc}
406          */
407         @Override
408         public void markPostKnown(Post post) {
409                 newPostNotification.removePost(post);
410                 if (newPostNotification.isEmpty()) {
411                         newPostNotification.dismiss();
412                 }
413         }
414
415         /**
416          * {@inheritDoc}
417          */
418         @Override
419         public void markReplyKnown(Reply reply) {
420                 newReplyNotification.removeReply(reply);
421                 if (newReplyNotification.isEmpty()) {
422                         newReplyNotification.dismiss();
423                 }
424         }
425
426         /**
427          * Template provider implementation that uses
428          * {@link WebInterface#createReader(String)} to load templates for
429          * inclusion.
430          *
431          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
432          */
433         private class ClassPathTemplateProvider implements TemplateProvider {
434
435                 /** The template factory. */
436                 @SuppressWarnings("hiding")
437                 private final TemplateFactory templateFactory;
438
439                 /**
440                  * Creates a new template provider that locates templates on the
441                  * classpath.
442                  *
443                  * @param templateFactory
444                  *            The template factory to create the templates
445                  */
446                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
447                         this.templateFactory = templateFactory;
448                 }
449
450                 /**
451                  * {@inheritDoc}
452                  */
453                 @Override
454                 @SuppressWarnings("synthetic-access")
455                 public Template getTemplate(String templateName) {
456                         Reader templateReader = createReader("/templates/" + templateName);
457                         if (templateReader == null) {
458                                 return null;
459                         }
460                         Template template = templateFactory.createTemplate(templateReader);
461                         try {
462                                 template.parse();
463                         } catch (TemplateException te1) {
464                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
465                         }
466                         return template;
467                 }
468
469         }
470
471 }