From 60d42fc47435de0b37710af49ad76ceb31dcfb9d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 Nov 2013 19:23:35 +0100 Subject: [PATCH] Move post liking from Sone to Post. --- src/main/java/net/pterodactylus/sone/data/Post.java | 2 ++ src/main/java/net/pterodactylus/sone/data/Sone.java | 9 --------- .../java/net/pterodactylus/sone/data/impl/DefaultPost.java | 5 +++++ .../java/net/pterodactylus/sone/data/impl/DefaultSone.java | 5 ----- .../java/net/pterodactylus/sone/database/PostDatabase.java | 2 ++ .../pterodactylus/sone/database/memory/MemoryDatabase.java | 12 ++++++++++++ .../java/net/pterodactylus/sone/fcp/LikePostCommand.java | 2 +- src/main/java/net/pterodactylus/sone/web/LikePage.java | 7 ++++++- .../java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java | 7 ++++++- 9 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 20bf518..63b4908 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -124,6 +124,8 @@ public interface Post extends Identified { */ public Post setKnown(boolean known); + public void like(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 dfeb019..cc32b7a 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); /** - * Adds the given post ID to the list of posts this Sone likes. - * - * @param postId - * The ID of the post - * @return This Sone (for method chaining) - */ - Sone addLikedPostId(String postId); - - /** * Removes the given post ID from the list of posts this Sone likes. * * @param postId 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 abef0c4..2fe4308 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -128,6 +128,11 @@ public class DefaultPost implements Post { } @Override + public void like(Sone localSone) { + database.likePost(this, localSone); + } + + @Override public List getReplies() { return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR); } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java index 92d7d5f..08a9ac1 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java @@ -319,11 +319,6 @@ public class DefaultSone implements Sone { return likedPostIds.contains(postId); } - public Sone addLikedPostId(String postId) { - likedPostIds.add(postId); - return this; - } - public Sone removeLikedPostId(String postId) { likedPostIds.remove(postId); 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 9480071..dc56dab 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -99,4 +99,6 @@ public interface PostDatabase { */ void removePosts(Sone sone); + void likePost(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 8343caf..7ede4fe 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -57,6 +57,7 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; +import com.google.common.collect.SetMultimap; import com.google.common.collect.SortedSetMultimap; import com.google.common.collect.TreeMultimap; import com.google.common.util.concurrent.AbstractService; @@ -83,6 +84,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** All posts by their Sones. */ private final Multimap sonePosts = HashMultimap.create(); + private final SetMultimap likedPosts = HashMultimap.create(); /** All posts by their recipient. */ private final Multimap recipientPosts = HashMultimap.create(); @@ -284,6 +286,16 @@ public class MemoryDatabase extends AbstractService implements Database { } } + @Override + public void likePost(Post post, Sone localSone) { + lock.writeLock().lock(); + try { + likedPosts.put(localSone.getId(), post.getId()); + } finally { + lock.writeLock().unlock(); + } + } + // // POSTSTORE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java index ed1d42e..d2acb4a 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java @@ -46,7 +46,7 @@ public class LikePostCommand extends AbstractSoneCommand { public Response execute(SimpleFieldSet parameters, Bucket data, AccessType accessType) throws FcpException { Post post = getPost(parameters, "Post"); Sone sone = getMandatoryLocalSone(parameters, "Sone"); - sone.addLikedPostId(post.getId()); + post.like(sone); return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(post).size()).get()); } diff --git a/src/main/java/net/pterodactylus/sone/web/LikePage.java b/src/main/java/net/pterodactylus/sone/web/LikePage.java index d4dd401..970b4c5 100644 --- a/src/main/java/net/pterodactylus/sone/web/LikePage.java +++ b/src/main/java/net/pterodactylus/sone/web/LikePage.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 like a {@link Post}. * @@ -56,7 +58,10 @@ public class LikePage extends SoneTemplatePage { String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256); Sone currentSone = getCurrentSone(request.getToadletContext()); if ("post".equals(type)) { - currentSone.addLikedPostId(id); + Optional post = webInterface.getCore().getDatabase().getPost(id); + if (post.isPresent()) { + post.get().like(currentSone); + } } else if ("reply".equals(type)) { currentSone.addLikedReplyId(id); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java index 7e7a6a6..ceeaf16 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.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 like a {@link Post}. * @@ -51,7 +53,10 @@ public class LikeAjaxPage extends JsonPage { return createErrorJsonObject("auth-required"); } if ("post".equals(type)) { - currentSone.addLikedPostId(id); + Optional post = webInterface.getCore().getDatabase().getPost(id); + if (post.isPresent()) { + post.get().like(currentSone); + } webInterface.getCore().touchConfiguration(); } else if ("reply".equals(type)) { currentSone.addLikedReplyId(id); -- 2.7.4