Explicitely store a null if there is no recipient.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index e1dcb9c..52ebfab 100644 (file)
@@ -561,10 +561,9 @@ public class Core implements IdentityListener {
                synchronized (newPosts) {
                        boolean isNew = !knownPosts.contains(postId) && newPosts.contains(postId);
                        if (markAsKnown) {
-                               newPosts.remove(postId);
-                               knownPosts.add(postId);
-                               if (isNew) {
-                                       coreListenerManager.fireMarkPostKnown(getPost(postId));
+                               Post post = getPost(postId, false);
+                               if (post != null) {
+                                       markPostKnown(post);
                                }
                        }
                        return isNew;
@@ -654,10 +653,9 @@ public class Core implements IdentityListener {
                synchronized (newReplies) {
                        boolean isNew = !knownReplies.contains(replyId) && newReplies.contains(replyId);
                        if (markAsKnown) {
-                               newReplies.remove(replyId);
-                               knownReplies.add(replyId);
-                               if (isNew) {
-                                       coreListenerManager.fireMarkReplyKnown(getReply(replyId));
+                               Reply reply = getReply(replyId, false);
+                               if (reply != null) {
+                                       markReplyKnown(reply);
                                }
                        }
                        return isNew;
@@ -1145,9 +1143,7 @@ public class Core implements IdentityListener {
                        for (Post post : sone.getPosts()) {
                                String postPrefix = sonePrefix + "/Posts/" + postCounter++;
                                configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
-                               if (post.getRecipient() != null) {
-                                       configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipient().getId());
-                               }
+                               configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
                                configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
                                configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
                        }
@@ -1216,11 +1212,48 @@ public class Core implements IdentityListener {
         * @return The created post
         */
        public Post createPost(Sone sone, long time, String text) {
+               return createPost(sone, null, time, text);
+       }
+
+       /**
+        * Creates a new post.
+        *
+        * @param sone
+        *            The Sone that creates the post
+        * @param recipient
+        *            The recipient Sone, or {@code null} if this post does not have
+        *            a recipient
+        * @param text
+        *            The text of the post
+        * @return The created post
+        */
+       public Post createPost(Sone sone, Sone recipient, String text) {
+               return createPost(sone, recipient, System.currentTimeMillis(), text);
+       }
+
+       /**
+        * Creates a new post.
+        *
+        * @param sone
+        *            The Sone that creates the post
+        * @param recipient
+        *            The recipient Sone, or {@code null} if this post does not have
+        *            a recipient
+        * @param time
+        *            The time of the post
+        * @param text
+        *            The text of the post
+        * @return The created post
+        */
+       public Post createPost(Sone sone, Sone recipient, long time, String text) {
                if (!isLocalSone(sone)) {
                        logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
                        return null;
                }
                Post post = new Post(sone, time, text);
+               if (recipient != null) {
+                       post.setRecipient(recipient);
+               }
                synchronized (posts) {
                        posts.put(post.getId(), post);
                }