*/
public Post setKnown(boolean known);
+ public void like(Sone localSone);
+
List<PostReply> getReplies();
}
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
}
@Override
+ public void like(Sone localSone) {
+ database.likePost(this, localSone);
+ }
+
+ @Override
public List<PostReply> getReplies() {
return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
}
return likedPostIds.contains(postId);
}
- public Sone addLikedPostId(String postId) {
- likedPostIds.add(postId);
- return this;
- }
-
public Sone removeLikedPostId(String postId) {
likedPostIds.remove(postId);
return this;
*/
void removePosts(Sone sone);
+ void likePost(Post post, Sone localSone);
+
}
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;
/** 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();
}
}
+ @Override
+ public void likePost(Post post, Sone localSone) {
+ lock.writeLock().lock();
+ try {
+ likedPosts.put(localSone.getId(), post.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
//
// POSTSTORE METHODS
//
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());
}
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}.
*
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);
}
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}.
*
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);