Mark new replies.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Nov 2010 20:41:18 +0000 (21:41 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Nov 2010 20:41:18 +0000 (21:41 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java
src/main/resources/static/css/sone.css
src/main/resources/templates/include/viewPost.html

index 37d1741..895f45e 100644 (file)
@@ -126,6 +126,12 @@ public class Core implements IdentityListener {
        /** All replies. */
        private Map<String, Reply> replies = new HashMap<String, Reply>();
 
+       /** All new replies. */
+       private Set<String> newReplies = new HashSet<String>();
+
+       /** All known replies. */
+       private Set<String> knownReplies = new HashSet<String>();
+
        /**
         * Creates a new core.
         *
@@ -468,6 +474,22 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the reply with the given ID is new.
+        *
+        * @param replyId
+        *            The ID of the reply to check
+        * @return {@code true} if the reply is considered to be new, {@code false}
+        *         otherwise
+        */
+       public boolean isNewReply(String replyId) {
+               synchronized (newReplies) {
+                       boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
+                       knownReplies.add(replyId);
+                       return isNew;
+               }
+       }
+
+       /**
         * Returns all Sones that have liked the given post.
         *
         * @param post
@@ -654,8 +676,13 @@ public class Core implements IdentityListener {
                                for (Reply reply : storedSone.getReplies()) {
                                        replies.remove(reply.getId());
                                }
-                               for (Reply reply : sone.getReplies()) {
-                                       replies.put(reply.getId(), reply);
+                               synchronized (newReplies) {
+                                       for (Reply reply : sone.getReplies()) {
+                                               if (!storedSone.getReplies().contains(reply) && !knownSones.contains(reply.getId())) {
+                                                       newReplies.add(reply.getId());
+                                               }
+                                               replies.put(reply.getId(), reply);
+                                       }
                                }
                        }
                        synchronized (storedSone) {
@@ -818,6 +845,11 @@ public class Core implements IdentityListener {
                                knownPosts.add(post.getId());
                        }
                }
+               synchronized (newReplies) {
+                       for (Reply reply : replies) {
+                               knownReplies.add(reply.getId());
+                       }
+               }
        }
 
        /**
@@ -993,6 +1025,9 @@ public class Core implements IdentityListener {
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
+               synchronized (newReplies) {
+                       knownReplies.add(reply.getId());
+               }
                sone.addReply(reply);
                saveSone(sone);
        }
@@ -1093,6 +1128,18 @@ public class Core implements IdentityListener {
                        }
                }
 
+               /* load known replies. */
+               int replyCounter = 0;
+               while (true) {
+                       String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
+                       if (knownReplyId == null) {
+                               break;
+                       }
+                       synchronized (newReplies) {
+                               knownReplies.add(knownReplyId);
+                       }
+               }
+
        }
 
        /**
@@ -1123,6 +1170,15 @@ public class Core implements IdentityListener {
                                configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
                        }
 
+                       /* save known replies. */
+                       int replyCounter = 0;
+                       synchronized (newReplies) {
+                               for (String knownReplyId : knownReplies) {
+                                       configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
+                               }
+                               configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
+                       }
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
                }
index 53c5448..08c91c7 100644 (file)
@@ -56,6 +56,8 @@ public class ReplyAccessor extends ReflectionAccessor {
                } else if (member.equals("liked")) {
                        Sone currentSone = (Sone) dataProvider.getData("currentSone");
                        return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId()));
+               } else if (member.equals("new")) {
+                       return core.isNewReply(reply.getId());
                }
                return super.get(dataProvider, object, member);
        }
index b05a1f1..9e33575 100644 (file)
@@ -234,6 +234,10 @@ textarea {
        padding: 1ex;
 }
 
+#sone .post .reply.new {
+       background-color: #ffffa0;
+}
+
 #sone .post .reply .time {
        float: none;
        display: inline;
index f3d4696..ddb3dbd 100644 (file)
@@ -37,7 +37,7 @@
                </div>
                <div class="replies">
                        <%foreach post.replies reply>
-                               <div id="<% reply.id|html>" class="reply">
+                               <div id="<% reply.id|html>" class="reply<%if reply.new> new<%/if>">
                                        <div class="avatar">
                                                <img src="/WoT/GetIdenticon?identity=<% reply.sone.id|html>&amp;width=36&height=36" width="36" height="36" alt="Avatar Image" />
                                        </div>