From: David ‘Bombe’ Roden Date: Wed, 5 Mar 2014 06:14:47 +0000 (+0100) Subject: Store the “known” status of a post in the database. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=72407829d504a0444aadd09cc937bae10b6cb866;p=Sone.git Store the “known” status of a post in the database. Also, don’t allow a post to be set unknown from the Post interface because there’s no use for it. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 0107a22..cb791dc 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -759,7 +759,7 @@ public class Core extends AbstractService implements SoneProvider { continue; } if (newPost.getTime() < getSoneFollowingTime(sone)) { - newPost.setKnown(true); + newPost.setKnown(); } else if (!newPost.isKnown()) { eventBus.post(new NewPostFoundEvent(newPost)); } @@ -1071,7 +1071,7 @@ public class Core extends AbstractService implements SoneProvider { } database.storePosts(sone, posts); for (Post post : posts) { - post.setKnown(true); + post.setKnown(); } database.storePostReplies(sone, replies); for (PostReply reply : replies) { @@ -1106,7 +1106,7 @@ public class Core extends AbstractService implements SoneProvider { * The post to mark as known */ public void markPostKnown(Post post) { - post.setKnown(true); + post.setKnown(); eventBus.post(new MarkPostKnownEvent(post)); touchConfiguration(); for (PostReply reply : post.getReplies()) { diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 1fe2fa7..bd3986f 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -119,11 +119,9 @@ public interface Post extends Identified { /** * Sets whether this post is known. * - * @param known - * {@code true} if this post is known, {@code false} otherwise * @return This post */ - public Post setKnown(boolean known); + public Post setKnown(); void like(Sone localSone); void unlike(Sone localSone); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java index a1204f9..a560185 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -55,9 +55,6 @@ public class DefaultPost implements Post { /** The text of the post. */ private final String text; - /** Whether the post is known. */ - private volatile boolean known; - /** * Creates a new post. * @@ -119,12 +116,12 @@ public class DefaultPost implements Post { @Override public boolean isKnown() { - return known; + return database.isPostKnown(this); } @Override - public DefaultPost setKnown(boolean known) { - this.known = known; + public DefaultPost setKnown() { + database.setPostKnown(this); return this; } diff --git a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java index d8956db..8fa4000 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -100,6 +100,8 @@ public interface PostDatabase { */ void removePosts(Sone sone); + void setPostKnown(Post post); + boolean isPostKnown(Post post); void likePost(Post post, Sone localSone); void unlikePost(Post post, Sone localSone); boolean isLiked(Post post, Sone sone); diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index edadf61..4bf6643 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -309,6 +309,39 @@ public class MemoryDatabase extends AbstractService implements Database { } } + /** + * Returns whether the given post is known. + * + * @param post + * The post + * @return {@code true} if the post is known, {@code false} otherwise + */ + @Override + public boolean isPostKnown(Post post) { + lock.readLock().lock(); + try { + return knownPosts.contains(post.getId()); + } finally { + lock.readLock().unlock(); + } + } + + /** + * Sets whether the given post is known. + * + * @param post + * The post + */ + @Override + public void setPostKnown(Post post) { + lock.writeLock().lock(); + try { + knownPosts.add(post.getId()); + } finally { + lock.writeLock().unlock(); + } + } + @Override public void likePost(Post post, Sone localSone) { lock.writeLock().lock(); @@ -766,47 +799,6 @@ public class MemoryDatabase extends AbstractService implements Database { } // - // PACKAGE-PRIVATE METHODS - // - - /** - * Returns whether the given post is known. - * - * @param post - * The post - * @return {@code true} if the post is known, {@code false} otherwise - */ - boolean isPostKnown(Post post) { - lock.readLock().lock(); - try { - return knownPosts.contains(post.getId()); - } finally { - lock.readLock().unlock(); - } - } - - /** - * Sets whether the given post is known. - * - * @param post - * The post - * @param known - * {@code true} if the post is known, {@code false} otherwise - */ - void setPostKnown(Post post, boolean known) { - lock.writeLock().lock(); - try { - if (known) { - knownPosts.add(post.getId()); - } else { - knownPosts.remove(post.getId()); - } - } finally { - lock.writeLock().unlock(); - } - } - - // // PRIVATE METHODS //