Use bookmark database in core.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 3 Oct 2014 21:09:29 +0000 (23:09 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 3 Oct 2014 21:09:29 +0000 (23:09 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/database/Database.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/web/BookmarkPage.java
src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java
src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java
src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java

index 2930283..9269ea6 100644 (file)
@@ -175,10 +175,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** The post database. */
        private final Database database;
 
-       /** All bookmarked posts. */
-       /* synchronize access on itself. */
-       private final Set<String> bookmarkedPosts = new HashSet<String>();
-
        /** Trusted identities, sorted by own identities. */
        private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
 
@@ -508,21 +504,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         otherwise
         */
        public boolean isBookmarked(Post post) {
-               return isPostBookmarked(post.getId());
-       }
-
-       /**
-        * Returns whether the post with the given ID is bookmarked.
-        *
-        * @param id
-        *            The ID of the post to check
-        * @return {@code true} if the post with the given ID is bookmarked,
-        *         {@code false} otherwise
-        */
-       public boolean isPostBookmarked(String id) {
-               synchronized (bookmarkedPosts) {
-                       return bookmarkedPosts.contains(id);
-               }
+               return database.isPostBookmarked(post);
        }
 
        /**
@@ -531,16 +513,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return All bookmarked posts
         */
        public Set<Post> getBookmarkedPosts() {
-               Set<Post> posts = new HashSet<Post>();
-               synchronized (bookmarkedPosts) {
-                       for (String bookmarkedPostId : bookmarkedPosts) {
-                               Optional<Post> post = getPost(bookmarkedPostId);
-                               if (post.isPresent()) {
-                                       posts.add(post.get());
-                               }
-                       }
-               }
-               return posts;
+               return database.getBookmarkedPosts();
        }
 
        public AlbumBuilder albumBuilder() {
@@ -1206,16 +1179,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                }
        }
 
-       /**
-        * Bookmarks the post with the given ID.
-        *
-        * @param id
-        *            The ID of the post to bookmark
-        */
-       public void bookmarkPost(String id) {
-               synchronized (bookmarkedPosts) {
-                       bookmarkedPosts.add(id);
-               }
+       public void bookmarkPost(Post post) {
+               database.bookmarkPost(post);
        }
 
        /**
@@ -1224,20 +1189,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @param post
         *            The post to unbookmark
         */
-       public void unbookmark(Post post) {
-               unbookmarkPost(post.getId());
-       }
-
-       /**
-        * Removes the post with the given ID from the bookmarks.
-        *
-        * @param id
-        *            The ID of the post to unbookmark
-        */
-       public void unbookmarkPost(String id) {
-               synchronized (bookmarkedPosts) {
-                       bookmarkedPosts.remove(id);
-               }
+       public void unbookmarkPost(Post post) {
+               database.unbookmarkPost(post);
        }
 
        /**
@@ -1663,10 +1616,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
 
                        /* save bookmarked posts. */
                        int bookmarkedPostCounter = 0;
-                       synchronized (bookmarkedPosts) {
-                               for (String bookmarkedPostId : bookmarkedPosts) {
-                                       configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPostId);
-                               }
+                       for (Post bookmarkedPost : getBookmarkedPosts()) {
+                               configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(bookmarkedPost.getId());
                        }
                        configuration.getStringValue("Bookmarks/Post/" + bookmarkedPostCounter++ + "/ID").setValue(null);
 
@@ -1723,9 +1674,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        if (bookmarkedPostId == null) {
                                break;
                        }
-                       synchronized (bookmarkedPosts) {
-                               bookmarkedPosts.add(bookmarkedPostId);
-                       }
+                       database.bookmarkPost(bookmarkedPostId);
                }
 
        }
index 1d871b9..2d80f7f 100644 (file)
@@ -29,7 +29,7 @@ import com.google.inject.ImplementedBy;
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 @ImplementedBy(MemoryDatabase.class)
-public interface Database extends Service, SoneDatabase, PostDatabase, PostReplyDatabase, AlbumDatabase, ImageDatabase {
+public interface Database extends Service, SoneDatabase, PostDatabase, PostReplyDatabase, AlbumDatabase, ImageDatabase, BookmarkDatabase {
 
        /**
         * Saves the database.
index d129d87..5a8fb00 100644 (file)
@@ -115,6 +115,9 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Map<String, Image> allImages = new HashMap<String, Image>();
        private final Multimap<String, Image> soneImages = HashMultimap.create();
 
+       private final MemoryBookmarkDatabase memoryBookmarkDatabase =
+                       new MemoryBookmarkDatabase(this);
+
        /**
         * Creates a new memory database.
         *
@@ -590,6 +593,31 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public void bookmarkPost(String postId) {
+               memoryBookmarkDatabase.bookmarkPost(postId);
+       }
+
+       @Override
+       public void bookmarkPost(Post post) {
+               memoryBookmarkDatabase.bookmarkPost(post);
+       }
+
+       @Override
+       public void unbookmarkPost(Post post) {
+               memoryBookmarkDatabase.unbookmarkPost(post);
+       }
+
+       @Override
+       public boolean isPostBookmarked(Post post) {
+               return memoryBookmarkDatabase.isPostBookmarked(post);
+       }
+
+       @Override
+       public Set<Post> getBookmarkedPosts() {
+               return memoryBookmarkDatabase.getBookmarkedPosts();
+       }
+
        //
        // PACKAGE-PRIVATE METHODS
        //
index 042a683..c0a8907 100644 (file)
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 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 bookmark a post.
  *
@@ -52,7 +55,10 @@ public class BookmarkPage extends SoneTemplatePage {
                if (request.getMethod() == Method.POST) {
                        String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
-                       webInterface.getCore().bookmarkPost(id);
+                       Optional<Post> post = webInterface.getCore().getPost(id);
+                       if (post.isPresent()) {
+                               webInterface.getCore().bookmarkPost(post.get());
+                       }
                        throw new RedirectException(returnPage);
                }
        }
index b568be1..7dfe301 100644 (file)
@@ -25,6 +25,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 unbookmark a post.
  *
@@ -55,7 +57,10 @@ public class UnbookmarkPage extends SoneTemplatePage {
                if (request.getMethod() == Method.POST) {
                        String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
-                       webInterface.getCore().unbookmarkPost(id);
+                       Optional<Post> post = webInterface.getCore().getPost(id);
+                       if (post.isPresent()) {
+                               webInterface.getCore().unbookmarkPost(post.get());
+                       }
                        throw new RedirectException(returnPage);
                }
                String id = request.getHttpRequest().getParam("post");
@@ -63,7 +68,7 @@ public class UnbookmarkPage extends SoneTemplatePage {
                        Set<Post> posts = webInterface.getCore().getBookmarkedPosts();
                        for (Post post : posts) {
                                if (post.getSone() == null) {
-                                       webInterface.getCore().unbookmark(post);
+                                       webInterface.getCore().unbookmarkPost(post);
                                }
                        }
                        throw new RedirectException("bookmarks.html");
index acee365..42719d3 100644 (file)
 
 package net.pterodactylus.sone.web.ajax;
 
+import net.pterodactylus.sone.data.Post;
 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 bookmark a post.
  *
@@ -50,7 +53,10 @@ public class BookmarkAjaxPage extends JsonPage {
                if ((id == null) || (id.length() == 0)) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               webInterface.getCore().bookmarkPost(id);
+               Optional<Post> post = webInterface.getCore().getPost(id);
+               if (post.isPresent()) {
+                       webInterface.getCore().bookmarkPost(post.get());
+               }
                return createSuccessJsonObject();
        }
 
index 65bb14d..064bedc 100644 (file)
 
 package net.pterodactylus.sone.web.ajax;
 
+import net.pterodactylus.sone.data.Post;
 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 unbookmark a post.
  *
@@ -50,7 +53,10 @@ public class UnbookmarkAjaxPage extends JsonPage {
                if ((id == null) || (id.length() == 0)) {
                        return createErrorJsonObject("invalid-post-id");
                }
-               webInterface.getCore().unbookmarkPost(id);
+               Optional<Post> post = webInterface.getCore().getPost(id);
+               if (post.isPresent()) {
+                       webInterface.getCore().unbookmarkPost(post.get());
+               }
                return createSuccessJsonObject();
        }
 
index 1db89d0..5f2025f 100644 (file)
@@ -4,10 +4,13 @@
 
 package net.pterodactylus.sone.web.ajax;
 
+import static com.google.common.base.Optional.of;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.times;
@@ -18,6 +21,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 
@@ -36,6 +40,8 @@ public class BookmarkAjaxPageTest {
        public void testBookmarkingExistingPost() throws URISyntaxException {
                /* create mocks. */
                Core core = mock(Core.class);
+               Post post = mock(Post.class);
+               when(core.getPost("abc")).thenReturn(of(post));
                WebInterface webInterface = mock(WebInterface.class);
                when(webInterface.getCore()).thenReturn(core);
                HTTPRequest httpRequest = new HTTPRequestImpl(new URI("/ajax/bookmark.ajax?post=abc"), "GET");
@@ -51,8 +57,7 @@ public class BookmarkAjaxPageTest {
                assertThat(jsonReturnObject.isSuccess(), is(true));
 
                /* verify behaviour. */
-               verify(core, times(1)).bookmarkPost(anyString());
-               verify(core).bookmarkPost("abc");
+               verify(core).bookmarkPost(post);
        }
 
        @Test
@@ -75,7 +80,7 @@ public class BookmarkAjaxPageTest {
                assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
 
                /* verify behaviour. */
-               verify(core, never()).bookmarkPost(anyString());
+               verify(core, never()).bookmarkPost(any(Post.class));
        }
 
 }