/** 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());
* 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);
}
/**
* @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() {
}
}
- /**
- * 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);
}
/**
* @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);
}
/**
/* 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);
if (bookmarkedPostId == null) {
break;
}
- synchronized (bookmarkedPosts) {
- bookmarkedPosts.add(bookmarkedPostId);
- }
+ database.bookmarkPost(bookmarkedPostId);
}
}
* @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.
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.
*
}
}
+ @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
//
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.
*
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);
}
}
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.
*
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");
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");
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.
*
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();
}
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.
*
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();
}
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;
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;
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");
assertThat(jsonReturnObject.isSuccess(), is(true));
/* verify behaviour. */
- verify(core, times(1)).bookmarkPost(anyString());
- verify(core).bookmarkPost("abc");
+ verify(core).bookmarkPost(post);
}
@Test
assertThat(((JsonErrorReturnObject) jsonReturnObject).getError(), is("invalid-post-id"));
/* verify behaviour. */
- verify(core, never()).bookmarkPost(anyString());
+ verify(core, never()).bookmarkPost(any(Post.class));
}
}