Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SOCIALFB-41: Capture likes count (where available) for a Post

  • Loading branch information...
commit 6a07ea52c4545173490ea15a88d38b814e56cf45 1 parent f185186
Craig Walls authored
View
8 spring-social-facebook/src/main/java/org/springframework/social/facebook/api/Checkin.java
@@ -37,9 +37,9 @@
private String message;
- private Comments comments;
+ private ListAndCount<Comment> comments;
- private List<Reference> likes;
+ private ListAndCount<Reference> likes;
private List<Reference> tags;
@@ -76,11 +76,11 @@ public String getMessage() {
}
public List<Comment> getComments() {
- return comments.getComments();
+ return comments.getList();
}
public List<Reference> getLikes() {
- return likes;
+ return likes.getList();
}
public List<Reference> getTags() {
View
20 ...springframework/social/facebook/api/Comments.java → ...ngframework/social/facebook/api/ListAndCount.java
@@ -23,23 +23,23 @@
* Instead, the comments list and count are available through Comment.getComments(), Post.getComments(), and Post.getCommentCount().
* @author habuma
*/
-public class Comments {
+public class ListAndCount<T> {
- private final List<Comment> comments;
+ private final List<T> list;
- private final int commentCount;
+ private final int count;
- public Comments(List<Comment> comments, int commentCount) {
- this.comments = comments;
- this.commentCount = commentCount;
+ public ListAndCount(List<T> list, int count) {
+ this.list = list;
+ this.count = count;
}
- public int getCommentCount() {
- return commentCount;
+ public int getCount() {
+ return count;
}
- public List<Comment> getComments() {
- return comments;
+ public List<T> getList() {
+ return list;
}
}
View
24 spring-social-facebook/src/main/java/org/springframework/social/facebook/api/Post.java
@@ -54,11 +54,9 @@
private PostType type;
- private int likeCount;
-
- private Comments comments;
+ private ListAndCount<Reference> likes;
- private int commentCount;
+ private ListAndCount<Comment> comments;
private int sharesCount;
@@ -125,8 +123,20 @@ public PostType getType() {
return type;
}
+ /**
+ * Reference for users who have liked this Post.
+ * May not be a complete list and the size may be different than the value returned from getLikeCount().
+ * For a complete list of likes, use {@link LikeOperations#getLikes(String)}.
+ */
+ public List<Reference> getLikes() {
+ return likes.getList();
+ }
+
+ /**
+ * The number of likes for this Post. May be different than the size of the list returned from getLikes().
+ */
public int getLikeCount() {
- return likeCount;
+ return likes.getCount();
}
public int getSharesCount() {
@@ -137,11 +147,11 @@ public int getSharesCount() {
* The most recent comments for the post.
*/
public List<Comment> getComments() {
- return comments.getComments();
+ return comments.getList();
}
public int getCommentCount() {
- return comments.getCommentCount();
+ return comments.getCount();
}
public static enum PostType { POST, CHECKIN, LINK, NOTE, PHOTO, STATUS, VIDEO, SWF, MUSIC }
View
11 spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/json/CheckinMixin.java
@@ -22,7 +22,8 @@
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
-import org.springframework.social.facebook.api.Comments;
+import org.springframework.social.facebook.api.Comment;
+import org.springframework.social.facebook.api.ListAndCount;
import org.springframework.social.facebook.api.Page;
import org.springframework.social.facebook.api.Reference;
@@ -45,12 +46,12 @@
String message;
@JsonProperty("comments")
- @JsonDeserialize(using = CommentListDeserializer.class)
- Comments comments;
+ @JsonDeserialize(using = CommentListAndCountDeserializer.class)
+ ListAndCount<Comment> comments;
@JsonProperty("likes")
- @JsonDeserialize(using = ReferenceListDeserializer.class)
- List<Reference> likes;
+ @JsonDeserialize(using = ReferenceListAndCountDeserializer.class)
+ ListAndCount<Reference> likes;
@JsonProperty("tags")
@JsonDeserialize(using = ReferenceListDeserializer.class)
View
8 ...cebook/api/impl/json/CommentListDeserializer.java → ...pi/impl/json/CommentListAndCountDeserializer.java
@@ -27,13 +27,13 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.social.facebook.api.Comment;
-import org.springframework.social.facebook.api.Comments;
+import org.springframework.social.facebook.api.ListAndCount;
-class CommentListDeserializer extends JsonDeserializer<Comments> {
+class CommentListAndCountDeserializer extends JsonDeserializer<ListAndCount<Comment>> {
@SuppressWarnings("unchecked")
@Override
- public Comments deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+ public ListAndCount<Comment> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setDeserializationConfig(ctxt.getConfig());
jp.setCodec(mapper);
@@ -45,7 +45,7 @@ public Comments deserialize(JsonParser jp, DeserializationContext ctxt) throws I
Collections.<Comment>emptyList();
JsonNode countNode = commentsNode.get("count");
int commentCount = countNode != null ? countNode.getIntValue() : 0;
- return new Comments(commentsList, commentCount);
+ return new ListAndCount<Comment>(commentsList, commentCount);
}
return null;
View
11 spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/json/PostMixin.java
@@ -34,8 +34,9 @@
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.springframework.social.facebook.api.CheckinPost;
-import org.springframework.social.facebook.api.Comments;
+import org.springframework.social.facebook.api.Comment;
import org.springframework.social.facebook.api.LinkPost;
+import org.springframework.social.facebook.api.ListAndCount;
import org.springframework.social.facebook.api.MusicPost;
import org.springframework.social.facebook.api.NotePost;
import org.springframework.social.facebook.api.PhotoPost;
@@ -113,12 +114,12 @@
int sharesCount;
@JsonProperty("likes")
- @JsonDeserialize(using = CountDeserializer.class)
- int likeCount;
+ @JsonDeserialize(using = ReferenceListAndCountDeserializer.class)
+ ListAndCount<Reference> likes;
@JsonProperty("comments")
- @JsonDeserialize(using = CommentListDeserializer.class)
- Comments comments;
+ @JsonDeserialize(using = CommentListAndCountDeserializer.class)
+ ListAndCount<Comment> comments;
private static class TypeDeserializer extends JsonDeserializer<PostType> {
View
53 ...k/src/main/java/org/springframework/social/facebook/api/impl/json/ReferenceListAndCountDeserializer.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.facebook.api.impl.json;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.JsonProcessingException;
+import org.codehaus.jackson.map.DeserializationContext;
+import org.codehaus.jackson.map.JsonDeserializer;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.type.TypeReference;
+import org.springframework.social.facebook.api.ListAndCount;
+import org.springframework.social.facebook.api.Reference;
+
+class ReferenceListAndCountDeserializer extends JsonDeserializer<ListAndCount<Reference>> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public ListAndCount<Reference> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.setDeserializationConfig(ctxt.getConfig());
+ jp.setCodec(mapper);
+ if(jp.hasCurrentToken()) {
+ JsonNode node = jp.readValueAsTree();
+ JsonNode dataNode = node.get("data");
+ List<Reference> commentsList = dataNode != null ?
+ (List<Reference>) mapper.readValue(dataNode, new TypeReference<List<Reference>>() {}) :
+ Collections.<Reference>emptyList();
+ JsonNode countNode = node.get("count");
+ int referenceCount = countNode != null ? countNode.getIntValue() : 0;
+ return new ListAndCount<Reference>(commentsList, referenceCount);
+ }
+
+ return null;
+ }
+}
View
5 spring-social-facebook/src/test/java/org/springframework/social/facebook/api/FeedTemplateTest.java
@@ -350,6 +350,11 @@ public void getFeedEntry() {
assertEquals("100001387295207", feedEntry.getFrom().getId());
assertEquals("Art Names", feedEntry.getFrom().getName());
assertEquals(2, feedEntry.getLikeCount());
+ List<Reference> likes = feedEntry.getLikes();
+ assertEquals("1533260333", likes.get(0).getId());
+ assertEquals("Roy Clarkson", likes.get(0).getName());
+ assertEquals("1322692345", likes.get(1).getId());
+ assertEquals("Jim Smith", likes.get(1).getName());
assertEquals(2, feedEntry.getComments().size());
assertEquals(22, feedEntry.getCommentCount());
assertNull(feedEntry.getComments().get(1).getLikes());
Please sign in to comment.
Something went wrong with that request. Please try again.