Move post liking from Sone to Post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2013 18:23:35 +0000 (19:23 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:53 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/database/PostDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java
src/main/java/net/pterodactylus/sone/web/LikePage.java
src/main/java/net/pterodactylus/sone/web/ajax/LikeAjaxPage.java

index 20bf518..63b4908 100644 (file)
@@ -124,6 +124,8 @@ public interface Post extends Identified {
         */
        public Post setKnown(boolean known);
 
+       public void like(Sone localSone);
+
        List<PostReply> getReplies();
 
 }
index dfeb019..cc32b7a 100644 (file)
@@ -418,15 +418,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        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
index abef0c4..2fe4308 100644 (file)
@@ -128,6 +128,11 @@ public class DefaultPost implements Post {
        }
 
        @Override
+       public void like(Sone localSone) {
+               database.likePost(this, localSone);
+       }
+
+       @Override
        public List<PostReply> getReplies() {
                return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
        }
index 92d7d5f..08a9ac1 100644 (file)
@@ -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;
index 9480071..dc56dab 100644 (file)
@@ -99,4 +99,6 @@ public interface PostDatabase {
         */
        void removePosts(Sone sone);
 
+       void likePost(Post post, Sone localSone);
+
 }
index 8343caf..7ede4fe 100644 (file)
@@ -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<String, Post> sonePosts = HashMultimap.create();
+       private final SetMultimap<String, String> likedPosts = HashMultimap.create();
 
        /** All posts by their recipient. */
        private final Multimap<String, Post> 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
        //
index ed1d42e..d2acb4a 100644 (file)
@@ -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());
        }
 
index d4dd401..970b4c5 100644 (file)
@@ -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> post = webInterface.getCore().getDatabase().getPost(id);
+                               if (post.isPresent()) {
+                                       post.get().like(currentSone);
+                               }
                        } else if ("reply".equals(type)) {
                                currentSone.addLikedReplyId(id);
                        }
index 7e7a6a6..ceeaf16 100644 (file)
@@ -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> post = webInterface.getCore().getDatabase().getPost(id);
+                       if (post.isPresent()) {
+                               post.get().like(currentSone);
+                       }
                        webInterface.getCore().touchConfiguration();
                } else if ("reply".equals(type)) {
                        currentSone.addLikedReplyId(id);