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

index f8f8948..841f560 100644 (file)
@@ -113,6 +113,13 @@ public class Core implements IdentityListener {
        /* synchronize access on {@link #newSones}. */
        private Set<String> knownSones = new HashSet<String>();
 
+       /** All new posts. */
+       private Set<String> newPosts = new HashSet<String>();
+
+       /** All known posts. */
+       /* synchronize access on {@link #newPosts}. */
+       private Set<String> knownPosts = new HashSet<String>();
+
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
 
@@ -405,6 +412,23 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the given post ID is new. After this method returns it is
+        * marked a known post ID.
+        *
+        * @param postId
+        *            The post ID
+        * @return {@code true} if the post is considered to be new, {@code false}
+        *         otherwise
+        */
+       public boolean isNewPost(String postId) {
+               synchronized (newPosts) {
+                       boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
+                       knownPosts.add(postId);
+                       return isNew;
+               }
+       }
+
+       /**
         * Returns the reply with the given ID.
         *
         * @param replyId
@@ -617,8 +641,13 @@ public class Core implements IdentityListener {
                                for (Post post : storedSone.getPosts()) {
                                        posts.remove(post.getId());
                                }
-                               for (Post post : sone.getPosts()) {
-                                       posts.put(post.getId(), post);
+                               synchronized (newPosts) {
+                                       for (Post post : sone.getPosts()) {
+                                               if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
+                                                       newPosts.add(post.getId());
+                                               }
+                                               posts.put(post.getId(), post);
+                                       }
                                }
                        }
                        synchronized (replies) {
@@ -784,6 +813,11 @@ public class Core implements IdentityListener {
                                knownSones.add(friend);
                        }
                }
+               synchronized (newPosts) {
+                       for (Post post : posts) {
+                               knownPosts.add(post.getId());
+                       }
+               }
        }
 
        /**
@@ -1043,6 +1077,19 @@ public class Core implements IdentityListener {
                                knownSones.add(knownSoneId);
                        }
                }
+
+               /* load known posts. */
+               int postCounter = 0;
+               while (true) {
+                       String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
+                       if (knownPostId == null) {
+                               break;
+                       }
+                       synchronized (newPosts) {
+                               knownPosts.add(knownPostId);
+                       }
+               }
+
        }
 
        /**
@@ -1064,6 +1111,15 @@ public class Core implements IdentityListener {
                                configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
                        }
 
+                       /* save known posts. */
+                       int postCounter = 0;
+                       synchronized (newPosts) {
+                               for (String knownPostId : knownPosts) {
+                                       configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
+                               }
+                               configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
+                       }
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
                }
index 2c0b59e..574eb1c 100644 (file)
@@ -60,6 +60,8 @@ public class PostAccessor extends ReflectionAccessor {
                } else if (member.equals("liked")) {
                        Sone currentSone = (Sone) dataProvider.getData("currentSone");
                        return (currentSone != null) && (currentSone.isLikedPostId(post.getId()));
+               } else if (member.equals("new")) {
+                       return core.isNewPost(post.getId());
                }
                return super.get(dataProvider, object, member);
        }
index c23047e..7682859 100644 (file)
@@ -127,6 +127,11 @@ textarea {
        clear: both;
 }
 
+#sone .post.new {
+       background-color: #ffffa0;
+       padding-left: 1ex;
+}
+
 #sone .post.last {
        padding: 1ex 0px;
        border-bottom: none;
index 58b0786..f3d4696 100644 (file)
@@ -1,4 +1,4 @@
-<div id="<% post.id|html>" class="post<%if loop.last> last<%/if>">
+<div id="<% post.id|html>" class="post<%if loop.last> last<%/if><%if post.new> new<%/if>">
        <div class="avatar">
                <img src="/WoT/GetIdenticon?identity=<% post.sone.id|html>&amp;width=48&height=48" width="48" height="48" alt="Avatar Image" />
        </div>