From 16172b8d60c485ac52907ba45a354b754bce53e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 3 Oct 2014 23:09:29 +0200 Subject: [PATCH] Use bookmark database in core. --- .../java/net/pterodactylus/sone/core/Core.java | 69 +++------------------- .../net/pterodactylus/sone/database/Database.java | 2 +- .../sone/database/memory/MemoryDatabase.java | 28 +++++++++ .../net/pterodactylus/sone/web/BookmarkPage.java | 8 ++- .../net/pterodactylus/sone/web/UnbookmarkPage.java | 9 ++- .../sone/web/ajax/BookmarkAjaxPage.java | 8 ++- .../sone/web/ajax/UnbookmarkAjaxPage.java | 8 ++- .../sone/web/ajax/BookmarkAjaxPageTest.java | 11 +++- 8 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 2930283..9269ea6 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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 bookmarkedPosts = new HashSet(); - /** Trusted identities, sorted by own identities. */ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.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 getBookmarkedPosts() { - Set posts = new HashSet(); - synchronized (bookmarkedPosts) { - for (String bookmarkedPostId : bookmarkedPosts) { - Optional 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); } } diff --git a/src/main/java/net/pterodactylus/sone/database/Database.java b/src/main/java/net/pterodactylus/sone/database/Database.java index 1d871b9..2d80f7f 100644 --- a/src/main/java/net/pterodactylus/sone/database/Database.java +++ b/src/main/java/net/pterodactylus/sone/database/Database.java @@ -29,7 +29,7 @@ import com.google.inject.ImplementedBy; * @author David ‘Bombe’ Roden */ @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. 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 d129d87..5a8fb00 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -115,6 +115,9 @@ public class MemoryDatabase extends AbstractService implements Database { private final Map allImages = new HashMap(); private final Multimap 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 getBookmarkedPosts() { + return memoryBookmarkDatabase.getBookmarkedPosts(); + } + // // PACKAGE-PRIVATE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/web/BookmarkPage.java b/src/main/java/net/pterodactylus/sone/web/BookmarkPage.java index 042a683..c0a8907 100644 --- a/src/main/java/net/pterodactylus/sone/web/BookmarkPage.java +++ b/src/main/java/net/pterodactylus/sone/web/BookmarkPage.java @@ -17,11 +17,14 @@ 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 = webInterface.getCore().getPost(id); + if (post.isPresent()) { + webInterface.getCore().bookmarkPost(post.get()); + } throw new RedirectException(returnPage); } } diff --git a/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java b/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java index b568be1..7dfe301 100644 --- a/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java +++ b/src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java @@ -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 = 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 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"); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java index acee365..42719d3 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.java @@ -17,9 +17,12 @@ 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 = webInterface.getCore().getPost(id); + if (post.isPresent()) { + webInterface.getCore().bookmarkPost(post.get()); + } return createSuccessJsonObject(); } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java index 65bb14d..064bedc 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/UnbookmarkAjaxPage.java @@ -17,9 +17,12 @@ 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 = webInterface.getCore().getPost(id); + if (post.isPresent()) { + webInterface.getCore().unbookmarkPost(post.get()); + } return createSuccessJsonObject(); } diff --git a/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java index 1db89d0..5f2025f 100644 --- a/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/ajax/BookmarkAjaxPageTest.java @@ -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)); } } -- 2.7.4