Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
Add create comment feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Feb 29, 2012
1 parent 520d69f commit 0166246
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

public interface CommentOperations {

Comment createComment(long id, String comment);

Comment createComment(long id, String comment,
boolean commentFromExternalSource);

CursoredList<Comment> getCommentsByMe();

CursoredList<Comment> getCommentsByMe(int pageSize, int pageNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.social.weibo.api.CursoredList;
import org.springframework.social.weibo.api.SourceFilterType;
import org.springframework.social.weibo.util.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

class CommentTemplate extends AbstractWeiboOperations implements
Expand All @@ -36,6 +38,25 @@ protected CommentTemplate(ObjectMapper objectMapper,
super(objectMapper, restTemplate, isAuthorized);
}

@Override
public Comment createComment(long id, String comment) {
return createComment(id, comment, false);
}

@Override
public Comment createComment(long id, String comment,
boolean commentFromExternalSource) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
3);
request.add("id", String.valueOf(id));
request.add("comment", comment);
request.add("comment_ori",
StringUtils.booleanToString(commentFromExternalSource));
return restTemplate.postForObject(buildUri("comments/create.json"),
request, Comment.class);
}

@Override
public CursoredList<Comment> getCommentsByMe() {
requireAuthorization();
Expand Down Expand Up @@ -116,8 +137,7 @@ public List<Comment> getCommentsOnStatuses(List<Long> ids) {
requireAuthorization();
URI uri = buildUri("comments/show_batch.json", "cids",
StringUtils.join(ids));
JsonNode dataNode = restTemplate.getForObject(
uri, JsonNode.class);
JsonNode dataNode = restTemplate.getForObject(uri, JsonNode.class);
return deserializeDataList(dataNode, Comment.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@
import org.springframework.social.weibo.api.Status;
import org.springframework.social.weibo.api.StatusContentType;
import org.springframework.social.weibo.api.TimelineOperations;
import org.springframework.social.weibo.util.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class TimelineTemplate extends AbstractWeiboOperations implements
TimelineOperations {

private static String booleanToString(boolean value) {
return value ? "1" : "0";
}

protected TimelineTemplate(ObjectMapper objectMapper,
RestTemplate restTemplate, boolean isAuthorized) {
super(objectMapper, restTemplate, isAuthorized);
Expand All @@ -56,17 +53,22 @@ private CursoredList<Status> fetchStatusList(String url, long sinceId,
long maxId, int pageSize, int pageNumber,
boolean onlyApplicationStatus, StatusContentType statusContentType) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
uriBuilder(url)
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam("feature",
String.valueOf(statusContentType.ordinal()))
.build(), JsonNode.class);
JsonNode dataNode = restTemplate
.getForObject(
uriBuilder(url)
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.queryParam(
"feature",
String.valueOf(statusContentType
.ordinal())).build(),
JsonNode.class);
return deserializeCursoredList(dataNode, Status.class, "statuses");
}

Expand Down Expand Up @@ -116,8 +118,10 @@ public List<Status> getDailyHotComments(int pageSize,
.getForObject(
uriBuilder("statuses/hot/comments_daily.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.build(), JsonNode.class);
return deserializeDataList(dataNode, Status.class);
}
Expand All @@ -138,8 +142,10 @@ public List<Status> getDailyHotRepost(int pageSize,
.getForObject(
uriBuilder("statuses/hot/repost_daily.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.build(), JsonNode.class);
return deserializeDataList(dataNode, Status.class);
}
Expand Down Expand Up @@ -231,8 +237,8 @@ public CursoredList<Status> getMentions(long sinceId, long maxId,
.queryParam("filter_by_source",
String.valueOf(sourceFilterType.ordinal()))
.queryParam("filter_by_type",
booleanToString(createdInWeibo)).build(),
JsonNode.class);
StringUtils.booleanToString(createdInWeibo))
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, Status.class, "statuses");
}

Expand All @@ -257,8 +263,10 @@ public CursoredList<Status> getPublicTimeline(int pageSize, int pageNumber,
uriBuilder("statuses/public_timeline.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, Status.class, "statuses");
}
Expand Down Expand Up @@ -359,18 +367,23 @@ public CursoredList<Status> getUserTimeline(long uid, long sinceId,
long maxId, int pageSize, int pageNumber,
boolean onlyApplicationStatus, StatusContentType statusContentType) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
uriBuilder("statuses/user_timeline.json")
.queryParam("uid", String.valueOf(uid))
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam("feature",
String.valueOf(statusContentType.ordinal()))
.build(), JsonNode.class);
JsonNode dataNode = restTemplate
.getForObject(
uriBuilder("statuses/user_timeline.json")
.queryParam("uid", String.valueOf(uid))
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.queryParam(
"feature",
String.valueOf(statusContentType
.ordinal())).build(),
JsonNode.class);
return deserializeCursoredList(dataNode, Status.class, "statuses");
}

Expand All @@ -390,8 +403,10 @@ public List<Status> getWeeklyHotComments(int pageSize,
.getForObject(
uriBuilder("statuses/hot/comments_weekly.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.build(), JsonNode.class);
return deserializeDataList(dataNode, Status.class);
}
Expand All @@ -412,8 +427,10 @@ public List<Status> getWeeklyHotRepost(int pageSize,
.getForObject(
uriBuilder("statuses/hot/repost_weekly.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("base_app",
booleanToString(onlyApplicationStatus))
.queryParam(
"base_app",
StringUtils
.booleanToString(onlyApplicationStatus))
.build(), JsonNode.class);
return deserializeDataList(dataNode, Status.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public static <T> String join(Iterable<T> iterable) {
return stringBuilder.toString();
}

public static String booleanToString(boolean value) {
return value ? "1" : "0";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.social.test.client.RequestMatchers.body;
import static org.springframework.social.test.client.RequestMatchers.method;
import static org.springframework.social.test.client.RequestMatchers.requestTo;
import static org.springframework.social.test.client.ResponseCreators.withResponse;
Expand Down Expand Up @@ -260,4 +262,17 @@ public void testGetCommentsOnStatuses() {
verifyComment(comments.iterator().next());
assertEquals(2, comments.size());
}

@Test
public void testCreateComment() {
mockServer
.expect(requestTo("https://api.weibo.com/2/comments/create.json"))
.andExpect(method(POST))
.andExpect(
body("id=1&comment=%E6%88%91%E5%96%9C%E6%AC%A2%E4%BD%A0%E5%81%9A%E7%9A%84&comment_ori=0"))
.andRespond(
withResponse(jsonResource("comment"), responseHeaders));
Comment comment = commentTemplate.createComment(1, "我喜欢你做的");
verifyComment(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public void testJoinArray() {
assertEquals("1,2,3", StringUtils.join(Arrays.asList(1L, 2L, 3L)));
}

@Test
public void testBooleanToString() {
assertEquals("1", StringUtils.booleanToString(true));
assertEquals("0", StringUtils.booleanToString(false));
}

}
81 changes: 81 additions & 0 deletions src/test/resources/json/comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"created_at" : "Wed Jun 01 00:50:25 +0800 2011",
"id" : 12438492184,
"text" : "我喜欢你做的",
"source" : "<a href=\"http : //weibo.com\" rel=\"\">新浪微博</a>",
"mid" : "202110601896455629",
"user" : {
"id" : 1404376560,
"screen_name" : "zaku",
"name" : "zaku",
"province" : "11",
"city" : "5",
"location" : "北京 朝阳区",
"description" : "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
"url" : "http:https://blog.sina.com.cn/zaku",
"profile_image_url" : "http:https://tp1.sinaimg.cn/1404376560/50/0/1",
"domain" : "zaku",
"gender" : "m",
"followers_count" : 1204,
"friends_count" : 447,
"statuses_count" : 2908,
"favourites_count" : 0,
"created_at" : "Fri Aug 28 00:00:00 +0800 2009",
"following" : false,
"allow_all_act_msg" : false,
"remark" : "",
"geo_enabled" : true,
"verified" : false,
"allow_all_comment" : true,
"avatar_large" : "http:https://tp1.sinaimg.cn/1404376560/180/0/1",
"verified_reason" : "",
"follow_me" : false,
"online_status" : 0,
"bi_followers_count" : 215
},
"status" : {
"created_at" : "Tue May 31 17:46:55 +0800 2011",
"id" : 11488058246,
"text" : "求关注。",
"source" : "<a href=\"http:https://weibo.com\" rel=\"\">新浪微博</a>",
"favorited" : false,
"truncated" : false,
"in_reply_to_status_id" : "",
"in_reply_to_user_id" : "",
"in_reply_to_screen_name" : "",
"geo" : null,
"mid" : "5612814510546515491",
"reposts_count" : 8,
"comments_count" : 9,
"annotations" : [],
"user" : {
"id" : 1404376560,
"screen_name" : "zaku",
"name" : "zaku",
"province" : "11",
"city" : "5",
"location" : "北京 朝阳区",
"description" : "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
"url" : "http:https://blog.sina.com.cn/zaku",
"profile_image_url" : "http:https://tp1.sinaimg.cn/1404376560/50/0/1",
"domain" : "zaku",
"gender" : "m",
"followers_count" : 1204,
"friends_count" : 447,
"statuses_count" : 2908,
"favourites_count" : 0,
"created_at" : "Fri Aug 28 00:00:00 +0800 2009",
"following" : false,
"allow_all_act_msg" : false,
"remark" : "",
"geo_enabled" : true,
"verified" : false,
"allow_all_comment" : true,
"avatar_large" : "http:https://tp1.sinaimg.cn/1404376560/180/0/1",
"verified_reason" : "",
"follow_me" : false,
"online_status" : 0,
"bi_followers_count" : 215
}
}
}

0 comments on commit 0166246

Please sign in to comment.