X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FWebInterface.java;h=6807b35c4a93aab0621b8b20b7fa7324af487ee1;hp=b4fffd928c81d7d6e681154edb209f59be9ef851;hb=bb2c8cb2d47a77bcef2d299ad4bf8e16f22a6198;hpb=217baee0543781a682227212118396296d96ee90 diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index b4fffd9..6807b35 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -58,6 +59,9 @@ import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.template.SubstringFilter; import net.pterodactylus.sone.template.TrustAccessor; import net.pterodactylus.sone.template.UnknownDateFilter; +import net.pterodactylus.sone.text.Part; +import net.pterodactylus.sone.text.SonePart; +import net.pterodactylus.sone.text.SoneTextParser; import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage; import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage; import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage; @@ -96,6 +100,8 @@ import net.pterodactylus.util.cache.CacheItem; import net.pterodactylus.util.cache.DefaultCacheItem; import net.pterodactylus.util.cache.MemoryCache; import net.pterodactylus.util.cache.ValueRetriever; +import net.pterodactylus.util.collection.SetBuilder; +import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.NotificationManager; @@ -151,6 +157,9 @@ public class WebInterface implements CoreListener { /** The template context factory. */ private final TemplateContextFactory templateContextFactory; + /** The Sone text parser. */ + private final SoneTextParser soneTextParser; + /** The “new Sone” notification. */ private final ListNotification newSoneNotification; @@ -160,6 +169,15 @@ public class WebInterface implements CoreListener { /** The “new reply” notification. */ private final ListNotification newReplyNotification; + /** The invisible “local post” notification. */ + private final ListNotification localPostNotification; + + /** The invisible “local reply” notification. */ + private final ListNotification localReplyNotification; + + /** The “you have been mentioned” notification. */ + private final ListNotification mentionNotification; + /** The “rescuing Sone” notification. */ private final ListNotification rescuingSonesNotification; @@ -185,6 +203,7 @@ public class WebInterface implements CoreListener { public WebInterface(SonePlugin sonePlugin) { this.sonePlugin = sonePlugin; formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); + soneTextParser = new SoneTextParser(getCore(), getCore()); templateContextFactory = new TemplateContextFactory(); templateContextFactory.addAccessor(Object.class, new ReflectionAccessor()); @@ -206,7 +225,7 @@ public class WebInterface implements CoreListener { templateContextFactory.addFilter("match", new MatchFilter()); templateContextFactory.addFilter("css", new CssClassNameFilter()); templateContextFactory.addFilter("js", new JavascriptFilter()); - templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory)); + templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser)); templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate")); templateContextFactory.addFilter("format", new FormatFilter()); templateContextFactory.addFilter("sort", new CollectionSortFilter()); @@ -224,9 +243,18 @@ public class WebInterface implements CoreListener { Template newPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html")); newPostNotification = new ListNotification("new-post-notification", "posts", newPostNotificationTemplate, false); + Template localPostNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newPostNotification.html")); + localPostNotification = new ListNotification("local-post-notification", "posts", localPostNotificationTemplate, false); + Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html")); newReplyNotification = new ListNotification("new-reply-notification", "replies", newReplyNotificationTemplate, false); + Template localReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html")); + localReplyNotification = new ListNotification("local-reply-notification", "replies", localReplyNotificationTemplate, false); + + Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html")); + mentionNotification = new ListNotification("mention-notification", "posts", mentionNotificationTemplate, false); + Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html")); rescuingSonesNotification = new ListNotification("sones-being-rescued-notification", "sones", rescuingSonesTemplate); @@ -405,7 +433,7 @@ public class WebInterface implements CoreListener { * @return The new posts */ public Set getNewPosts() { - return new HashSet(newPostNotification.getElements()); + return new SetBuilder().addAll(newPostNotification.getElements()).addAll(localPostNotification.getElements()).get(); } /** @@ -415,7 +443,7 @@ public class WebInterface implements CoreListener { * @return The new replies */ public Set getNewReplies() { - return new HashSet(newReplyNotification.getElements()); + return new SetBuilder().addAll(newReplyNotification.getElements()).addAll(localReplyNotification.getElements()).get(); } /** @@ -540,6 +568,7 @@ public class WebInterface implements CoreListener { Template deleteSoneTemplate = TemplateParser.parse(createReader("/templates/deleteSone.html")); Template noPermissionTemplate = TemplateParser.parse(createReader("/templates/noPermission.html")); Template optionsTemplate = TemplateParser.parse(createReader("/templates/options.html")); + Template rescueTemplate = TemplateParser.parse(createReader("/templates/rescue.html")); Template aboutTemplate = TemplateParser.parse(createReader("/templates/about.html")); Template invalidTemplate = TemplateParser.parse(createReader("/templates/invalid.html")); Template postTemplate = TemplateParser.parse(createReader("/templates/include/viewPost.html")); @@ -578,6 +607,7 @@ public class WebInterface implements CoreListener { pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login")); pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(emptyTemplate, this), "Logout")); pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options")); + pageToadlets.add(pageToadletFactory.createPageToadlet(new RescuePage(rescueTemplate, this), "Rescue")); pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About")); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this))); @@ -649,11 +679,34 @@ public class WebInterface implements CoreListener { try { return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8"); } catch (UnsupportedEncodingException uee1) { - System.out.println(" fail."); return null; } } + /** + * Returns all {@link Core#isLocalSone(Sone) local Sone}s that are + * referenced by {@link SonePart}s in the given text (after parsing it using + * {@link SoneTextParser}). + * + * @param text + * The text to parse + * @return All mentioned local Sones + */ + private Set getMentionedSones(String text) { + /* we need no context to find mentioned Sones. */ + Set mentionedSones = new HashSet(); + try { + for (Part part : soneTextParser.parse(null, new StringReader(text))) { + if (part instanceof SonePart) { + mentionedSones.add(((SonePart) part).getSone()); + } + } + } catch (IOException ioe1) { + logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1); + } + return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER); + } + // // CORELISTENER METHODS // @@ -693,9 +746,18 @@ public class WebInterface implements CoreListener { */ @Override public void newPostFound(Post post) { - newPostNotification.add(post); + boolean isLocal = getCore().isLocalSone(post.getSone()); + if (isLocal) { + localPostNotification.add(post); + } else { + newPostNotification.add(post); + } if (!hasFirstStartNotification()) { - notificationManager.addNotification(newPostNotification); + notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification); + if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) { + mentionNotification.add(post); + notificationManager.addNotification(mentionNotification); + } } else { getCore().markPostKnown(post); } @@ -706,12 +768,18 @@ public class WebInterface implements CoreListener { */ @Override public void newReplyFound(Reply reply) { - if (reply.getPost().getSone() == null) { - return; + boolean isLocal = getCore().isLocalSone(reply.getSone()); + if (isLocal) { + localReplyNotification.add(reply); + } else { + newReplyNotification.add(reply); } - newReplyNotification.add(reply); if (!hasFirstStartNotification()) { - notificationManager.addNotification(newReplyNotification); + notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification); + if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal) { + mentionNotification.add(reply.getPost()); + notificationManager.addNotification(mentionNotification); + } } else { getCore().markReplyKnown(reply); } @@ -731,6 +799,8 @@ public class WebInterface implements CoreListener { @Override public void markPostKnown(Post post) { newPostNotification.remove(post); + localPostNotification.remove(post); + mentionNotification.remove(post); } /** @@ -739,6 +809,16 @@ public class WebInterface implements CoreListener { @Override public void markReplyKnown(Reply reply) { newReplyNotification.remove(reply); + localReplyNotification.remove(reply); + mentionNotification.remove(reply.getPost()); + } + + /** + * {@inheritDoc} + */ + @Override + public void soneRemoved(Sone sone) { + newSoneNotification.remove(sone); } /** @@ -747,6 +827,7 @@ public class WebInterface implements CoreListener { @Override public void postRemoved(Post post) { newPostNotification.remove(post); + localPostNotification.remove(post); } /** @@ -755,6 +836,7 @@ public class WebInterface implements CoreListener { @Override public void replyRemoved(Reply reply) { newReplyNotification.remove(reply); + localReplyNotification.remove(reply); } /**