From: David ‘Bombe’ Roden Date: Tue, 5 Nov 2013 18:26:38 +0000 (+0100) Subject: Move unliking a post from Sone to Post. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=403b51bcf5b736808e3c554b8589759e7d3d5d47 Move unliking a post from Sone to Post. --- diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 63b4908..269c2e4 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -125,6 +125,7 @@ public interface Post extends Identified { public Post setKnown(boolean known); public void like(Sone localSone); + public void unlike(Sone localSone); List getReplies(); diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index cc32b7a..e340bdf 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -418,15 +418,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { boolean isLikedPostId(String postId); /** - * Removes the given post ID from the list of posts this Sone likes. - * - * @param postId - * The ID of the post - * @return This Sone (for method chaining) - */ - Sone removeLikedPostId(String postId); - - /** * Returns the IDs of all liked replies. * * @return All liked replies’ IDs 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 2fe4308..fa21e7a 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -133,6 +133,11 @@ public class DefaultPost implements Post { } @Override + public void unlike(Sone localSone) { + database.unlikePost(this, localSone); + } + + @Override public List getReplies() { return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR); } diff --git a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java index dc56dab..8bdca6c 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -100,5 +100,6 @@ public interface PostDatabase { void removePosts(Sone sone); void likePost(Post post, Sone localSone); + void unlikePost(Post post, Sone localSone); } 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 7ede4fe..c73513d 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -296,6 +296,16 @@ public class MemoryDatabase extends AbstractService implements Database { } } + @Override + public void unlikePost(Post post, Sone localSone) { + lock.writeLock().lock(); + try { + likedPosts.remove(localSone.getId(), post.getId()); + } finally { + lock.writeLock().unlock(); + } + } + // // POSTSTORE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/web/UnlikePage.java b/src/main/java/net/pterodactylus/sone/web/UnlikePage.java index 0670a18..48e5ab7 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnlikePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnlikePage.java @@ -24,6 +24,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user unlike a {@link Post}. * @@ -56,7 +58,10 @@ public class UnlikePage extends SoneTemplatePage { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); Sone currentSone = getCurrentSone(request.getToadletContext()); if ("post".equals(type)) { - currentSone.removeLikedPostId(id); + Optional post = webInterface.getCore().getDatabase().getPost(id); + if (post.isPresent()) { + post.get().unlike(currentSone); + } } else if ("reply".equals(type)) { currentSone.removeLikedReplyId(id); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java index bd3c387..5ac0ca0 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java @@ -22,6 +22,8 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; +import com.google.common.base.Optional; + /** * AJAX page that lets the user unlike a {@link Post}. * @@ -51,7 +53,10 @@ public class UnlikeAjaxPage extends JsonPage { return createErrorJsonObject("auth-required"); } if ("post".equals(type)) { - currentSone.removeLikedPostId(id); + Optional post = webInterface.getCore().getDatabase().getPost(id); + if (post.isPresent()) { + post.get().unlike(currentSone); + } webInterface.getCore().touchConfiguration(); } else if ("reply".equals(type)) { currentSone.removeLikedReplyId(id);