Move unliking a post from Sone to Post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2013 18:26:38 +0000 (19:26 +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/database/PostDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/web/UnlikePage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.java

index 63b4908..269c2e4 100644 (file)
@@ -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<PostReply> getReplies();
 
index cc32b7a..e340bdf 100644 (file)
@@ -418,15 +418,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        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
index 2fe4308..fa21e7a 100644 (file)
@@ -133,6 +133,11 @@ public class DefaultPost implements Post {
        }
 
        @Override
+       public void unlike(Sone localSone) {
+               database.unlikePost(this, localSone);
+       }
+
+       @Override
        public List<PostReply> getReplies() {
                return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
        }
index dc56dab..8bdca6c 100644 (file)
@@ -100,5 +100,6 @@ public interface PostDatabase {
        void removePosts(Sone sone);
 
        void likePost(Post post, Sone localSone);
+       void unlikePost(Post post, Sone localSone);
 
 }
index 7ede4fe..c73513d 100644 (file)
@@ -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
        //
index 0670a18..48e5ab7 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 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> post = webInterface.getCore().getDatabase().getPost(id);
+                               if (post.isPresent()) {
+                                       post.get().unlike(currentSone);
+                               }
                        } else if ("reply".equals(type)) {
                                currentSone.removeLikedReplyId(id);
                        }
index bd3c387..5ac0ca0 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 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> post = webInterface.getCore().getDatabase().getPost(id);
+                       if (post.isPresent()) {
+                               post.get().unlike(currentSone);
+                       }
                        webInterface.getCore().touchConfiguration();
                } else if ("reply".equals(type)) {
                        currentSone.removeLikedReplyId(id);