}
/**
- * Returns all Sones that have liked the given post.
- *
- * @param post
- * The post to get the liking Sones for
- * @return The Sones that like the given post
- */
- public Set<Sone> getLikes(Post post) {
- Set<Sone> sones = new HashSet<Sone>();
- for (Sone sone : getSones()) {
- if (sone.getLikedPostIds().contains(post.getId())) {
- sones.add(sone);
- }
- }
- return sones;
- }
-
- /**
* Returns all Sones that have liked the given reply.
*
* @param reply
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import java.util.Set;
import com.google.common.base.Function;
import com.google.common.base.Optional;
public void like(Sone localSone);
public void unlike(Sone localSone);
+ Set<Sone> getLikes();
+
List<PostReply> getReplies();
}
import static com.google.common.collect.FluentIterable.from;
import java.util.List;
+import java.util.Set;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
}
@Override
+ public Set<Sone> getLikes() {
+ return database.getLikes(this);
+ }
+
+ @Override
public List<PostReply> getReplies() {
return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
}
package net.pterodactylus.sone.database;
import java.util.Collection;
+import java.util.Set;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.Sone;
void likePost(Post post, Sone localSone);
void unlikePost(Post post, Sone localSone);
+ Set<Sone> getLikes(Post post);
}
/** All posts by their Sones. */
private final Multimap<String, Post> sonePosts = HashMultimap.create();
- private final SetMultimap<String, String> likedPosts = HashMultimap.create();
+ private final SetMultimap<String, String> likedPostsBySone = HashMultimap.create();
+ private final SetMultimap<String, String> postLikingSones = HashMultimap.create();
/** All posts by their recipient. */
private final Multimap<String, Post> recipientPosts = HashMultimap.create();
public void likePost(Post post, Sone localSone) {
lock.writeLock().lock();
try {
- likedPosts.put(localSone.getId(), post.getId());
+ likedPostsBySone.put(localSone.getId(), post.getId());
+ postLikingSones.put(post.getId(), localSone.getId());
} finally {
lock.writeLock().unlock();
}
public void unlikePost(Post post, Sone localSone) {
lock.writeLock().lock();
try {
- likedPosts.remove(localSone.getId(), post.getId());
+ likedPostsBySone.remove(localSone.getId(), post.getId());
+ postLikingSones.remove(post.getId(), localSone.getId());
} finally {
lock.writeLock().unlock();
}
}
+ @Override
+ public Set<Sone> getLikes(Post post) {
+ lock.readLock().lock();
+ try {
+ return from(postLikingSones.get(post.getId())).transform(getSone()).transformAndConcat(this.<Sone>unwrap()).toSet();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
//
// POSTSTORE METHODS
//
};
}
+ private static <T> Function<Optional<T>, Iterable<T>> unwrap() {
+ return new Function<Optional<T>, Iterable<T>>() {
+ @Override
+ public Iterable<T> apply(Optional<T> input) {
+ return (input == null) ? Collections.<T>emptyList() : input.asSet();
+ }
+ };
+ }
+
}
}
postBuilder.put(prefix + "Time", post.getTime());
postBuilder.put(prefix + "Text", encodeString(post.getText()));
- postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
+ postBuilder.put(encodeLikes(post.getLikes(), prefix + "Likes."));
return postBuilder;
}
Post post = getPost(parameters, "Post");
Sone sone = getMandatoryLocalSone(parameters, "Sone");
post.like(sone);
- return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(post).size()).get());
+ return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", post.getLikes().size()).get());
}
}
if ("replies".equals(member)) {
return from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER).toList();
} else if (member.equals("likes")) {
- return core.getLikes(post);
+ return post.getLikes();
} else if (member.equals("liked")) {
Sone currentSone = (Sone) templateContext.get("currentSone");
return (currentSone != null) && (currentSone.isLikedPostId(post.getId()));
if (!post.isPresent()) {
return createErrorJsonObject("invalid-post-id");
}
- Set<Sone> sones = webInterface.getCore().getLikes(post.get());
+ Set<Sone> sones = post.get().getLikes();
return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
} else if ("reply".equals(type)) {
Optional<PostReply> reply = webInterface.getCore().getDatabase().getPostReply(id);