+ * Returns all {@link Sone#isLocal() 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 Collection<Sone> getMentionedSones(String text) {
+ /* we need no context to find mentioned Sones. */
+ Set<Sone> mentionedSones = new HashSet<Sone>();
+ 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, String.format("Could not parse post text: %s", text), ioe1);
+ }
+ return Collections2.filter(mentionedSones, Sone.LOCAL_SONE_FILTER);
+ }
+
+ /**
+ * Returns the Sone insert notification for the given Sone. If no
+ * notification for the given Sone exists, a new notification is created and
+ * cached.
+ *
+ * @param sone
+ * The Sone to get the insert notification for
+ * @return The Sone insert notification
+ */
+ private TemplateNotification getSoneInsertNotification(Sone sone) {
+ synchronized (soneInsertNotifications) {
+ TemplateNotification templateNotification = soneInsertNotifications.get(sone);
+ if (templateNotification == null) {
+ templateNotification = new TemplateNotification(TemplateParser.parse(createReader("/templates/notify/soneInsertNotification.html")));
+ templateNotification.set("insertSone", sone);
+ soneInsertNotifications.put(sone, templateNotification);
+ }
+ return templateNotification;
+ }
+ }
+
+ //
+ // EVENT HANDLERS
+ //
+
+ /**
+ * Notifies the web interface that a new {@link Sone} was found.
+ *
+ * @param newSoneFoundEvent
+ * The event
+ */
+ @Subscribe
+ public void newSoneFound(NewSoneFoundEvent newSoneFoundEvent) {
+ newSoneNotification.add(newSoneFoundEvent.sone());
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(newSoneNotification);
+ }
+ }
+
+ /**
+ * Notifies the web interface that a new {@link Post} was found.
+ *
+ * @param newPostFoundEvent
+ * The event
+ */
+ @Subscribe
+ public void newPostFound(NewPostFoundEvent newPostFoundEvent) {
+ Post post = newPostFoundEvent.post();
+ boolean isLocal = post.getSone().isLocal();
+ if (isLocal) {
+ localPostNotification.add(post);
+ } else {
+ newPostNotification.add(post);
+ }
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(isLocal ? localPostNotification : newPostNotification);
+ if (!getMentionedSones(post.getText()).isEmpty() && !isLocal) {
+ mentionNotification.add(post);
+ notificationManager.addNotification(mentionNotification);
+ }
+ } else {
+ getCore().markPostKnown(post);
+ }
+ }
+
+ /**
+ * Notifies the web interface that a new {@link PostReply} was found.
+ *
+ * @param newPostReplyFoundEvent
+ * The event
+ */
+ @Subscribe
+ public void newReplyFound(NewPostReplyFoundEvent newPostReplyFoundEvent) {
+ PostReply reply = newPostReplyFoundEvent.postReply();
+ boolean isLocal = reply.getSone().isLocal();
+ if (isLocal) {
+ localReplyNotification.add(reply);
+ } else {
+ newReplyNotification.add(reply);
+ }
+ if (!hasFirstStartNotification()) {
+ notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
+ if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
+ mentionNotification.add(reply.getPost());
+ notificationManager.addNotification(mentionNotification);
+ }
+ } else {
+ getCore().markReplyKnown(reply);
+ }
+ }
+
+ /**
+ * Notifies the web interface that a {@link Sone} was marked as known.
+ *
+ * @param markSoneKnownEvent
+ * The event
+ */
+ @Subscribe
+ public void markSoneKnown(MarkSoneKnownEvent markSoneKnownEvent) {
+ newSoneNotification.remove(markSoneKnownEvent.sone());
+ }
+
+ /**
+ * Notifies the web interface that a {@link Post} was marked as known.
+ *
+ * @param markPostKnownEvent
+ * The event
+ */
+ @Subscribe
+ public void markPostKnown(MarkPostKnownEvent markPostKnownEvent) {
+ newPostNotification.remove(markPostKnownEvent.post());
+ localPostNotification.remove(markPostKnownEvent.post());
+ mentionNotification.remove(markPostKnownEvent.post());
+ }
+
+ /**
+ * Notifies the web interface that a {@link PostReply} was marked as known.
+ *
+ * @param markPostReplyKnownEvent
+ * The event
+ */
+ @Subscribe
+ public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
+ newReplyNotification.remove(markPostReplyKnownEvent.postReply());
+ localReplyNotification.remove(markPostReplyKnownEvent.postReply());
+ mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
+ }
+
+ /**
+ * Notifies the web interface that a {@link Sone} was removed.
+ *
+ * @param soneRemovedEvent
+ * The event
+ */
+ @Subscribe
+ public void soneRemoved(SoneRemovedEvent soneRemovedEvent) {
+ newSoneNotification.remove(soneRemovedEvent.sone());
+ }
+
+ /**
+ * Notifies the web interface that a {@link Post} was removed.
+ *
+ * @param postRemovedEvent
+ * The event
+ */
+ @Subscribe
+ public void postRemoved(PostRemovedEvent postRemovedEvent) {
+ newPostNotification.remove(postRemovedEvent.post());
+ localPostNotification.remove(postRemovedEvent.post());
+ mentionNotification.remove(postRemovedEvent.post());
+ }
+
+ /**
+ * Notifies the web interface that a {@link PostReply} was removed.
+ *
+ * @param postReplyRemovedEvent
+ * The event
+ */
+ @Subscribe
+ public void replyRemoved(PostReplyRemovedEvent postReplyRemovedEvent) {
+ PostReply reply = postReplyRemovedEvent.postReply();
+ newReplyNotification.remove(reply);
+ localReplyNotification.remove(reply);
+ if (!getMentionedSones(reply.getText()).isEmpty()) {
+ boolean isMentioned = false;
+ for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
+ isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
+ }
+ if (!isMentioned) {
+ mentionNotification.remove(reply.getPost());
+ }
+ }
+ }
+
+ /**
+ * Notifies the web interface that a Sone was locked.