Skip to content

Commit

Permalink
TFJ-336 support friendships/incoming and friendships/outgoing
Browse files Browse the repository at this point in the history
added Twitter#getIncomingFriendships()/getOutgoingFriendships()
  • Loading branch information
yusuke committed Apr 17, 2010
1 parent 45881c8 commit 0d170a0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 38 deletions.
16 changes: 16 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/Twitter.java
Expand Up @@ -1022,6 +1022,22 @@ public Relationship showFriendship(int sourceId, int targetId) throws TwitterExc
getParameterArray("source_id", sourceId, "target_id", targetId), auth));
}

/**
* {@inheritDoc}
*/
public IDs getIncomingFriendships(long cursor) throws TwitterException {
return IDsJSONImpl.getFriendsIDs(http.get(conf.getRestBaseURL() + "friendships/incoming.json?cursor=" + cursor, auth));
}

/**
* {@inheritDoc}
*/
public IDs getOutgoingFriendships(long cursor) throws TwitterException {
return IDsJSONImpl.getFriendsIDs(http.get(conf.getRestBaseURL() + "friendships/outgoing.json?cursor=" + cursor, auth));
}

/* Social Graph Methods */

/**
* {@inheritDoc}
*/
Expand Down
21 changes: 21 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/api/FriendshipMethods.java
Expand Up @@ -26,6 +26,7 @@
*/
package twitter4j.api;

import twitter4j.IDs;
import twitter4j.Relationship;
import twitter4j.TwitterException;
import twitter4j.User;
Expand Down Expand Up @@ -154,4 +155,24 @@ Relationship showFriendship(String sourceScreenName, String targetScreenName)
*/
Relationship showFriendship(int sourceId, int targetId)
throws TwitterException;

/**
* Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user.
* <br>This method calls http://api.twitter.com/1/friendships/incoming.json
*
* @param cursor Breaks the results into pages. A single page contains 5000 identifiers. Provide a value of -1 to begin paging.
* @return an array of numeric IDs for every user who has a pending request to follow the authenticating user.
* @throws TwitterException when Twitter service or network is unavailable
*/
IDs getIncomingFriendships(long cursor) throws TwitterException;

/**
* Returns an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request.
* <br>This method calls http://api.twitter.com/1/friendships/outgoing.json
*
* @param cursor Breaks the results into pages. A single page contains 5000 identifiers. Provide a value of -1 to begin paging.
* @return an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request.
* @throws TwitterException when Twitter service or network is unavailable
*/
IDs getOutgoingFriendships(long cursor) throws TwitterException;
}
86 changes: 48 additions & 38 deletions twitter4j-core/src/test/java/twitter4j/TwitterTestUnit.java
Expand Up @@ -398,6 +398,54 @@ public void testGetFriendsStatuses() throws Exception {
assertNotNull("friendsStatuses", users);
}

public void testRelationship() throws Exception {
// TESTING PRECONDITIONS:
// 1) id1 is followed by "followsOneWay", but not following "followsOneWay"
Relationship rel1 = twitterAPI1.showFriendship(id1.screenName, followsOneWay);

// test second precondition
assertNotNull(rel1);
assertTrue(rel1.isSourceFollowedByTarget());
assertFalse(rel1.isSourceFollowingTarget());
assertTrue(rel1.isTargetFollowingSource());
assertFalse(rel1.isTargetFollowedBySource());

// 2) best_friend1 is following and followed by best_friend2
Relationship rel2 = twitterAPI1.showFriendship(bestFriend1.screenName, bestFriend2.screenName);

// test second precondition
assertNotNull(rel2);
assertTrue(rel2.isSourceFollowedByTarget());
assertTrue(rel2.isSourceFollowingTarget());
assertTrue(rel2.isTargetFollowingSource());
assertTrue(rel2.isTargetFollowedBySource());

// test equality
Relationship rel3 = twitterAPI1.showFriendship(id1.screenName, followsOneWay);
assertEquals(rel1, rel3);
assertFalse(rel1.equals(rel2));
}

private void assertIDExsits(String assertion, IDs ids, int idToFind) {
boolean found = false;
for (int id : ids.getIDs()) {
if (id == idToFind) {
found = true;
break;
}
}
assertTrue(assertion, found);
}


public void testFriendships() throws Exception {
IDs ids;
ids = twitterAPI4.getIncomingFriendships(-1);
assertTrue(ids.getIDs().length > 0);
ids = twitterAPI2.getOutgoingFriendships(-1);
assertTrue(ids.getIDs().length > 0);
}

public void testSocialGraphMethods() throws Exception {
IDs ids;
ids = twitterAPI1.getFriendsIDs();
Expand Down Expand Up @@ -452,44 +500,6 @@ public void testSocialGraphMethods() throws Exception {
}


public void testRelationship() throws Exception {
// TESTING PRECONDITIONS:
// 1) id1 is followed by "followsOneWay", but not following "followsOneWay"
Relationship rel1 = twitterAPI1.showFriendship(id1.screenName, followsOneWay);

// test second precondition
assertNotNull(rel1);
assertTrue(rel1.isSourceFollowedByTarget());
assertFalse(rel1.isSourceFollowingTarget());
assertTrue(rel1.isTargetFollowingSource());
assertFalse(rel1.isTargetFollowedBySource());

// 2) best_friend1 is following and followed by best_friend2
Relationship rel2 = twitterAPI1.showFriendship(bestFriend1.screenName, bestFriend2.screenName);

// test second precondition
assertNotNull(rel2);
assertTrue(rel2.isSourceFollowedByTarget());
assertTrue(rel2.isSourceFollowingTarget());
assertTrue(rel2.isTargetFollowingSource());
assertTrue(rel2.isTargetFollowedBySource());

// test equality
Relationship rel3 = twitterAPI1.showFriendship(id1.screenName, followsOneWay);
assertEquals(rel1, rel3);
assertFalse(rel1.equals(rel2));
}

private void assertIDExsits(String assertion, IDs ids, int idToFind) {
boolean found = false;
for (int id : ids.getIDs()) {
if (id == idToFind) {
found = true;
break;
}
}
assertTrue(assertion, found);
}

public void testAccountMethods() throws Exception {
User original = twitterAPI1.verifyCredentials();
Expand Down

0 comments on commit 0d170a0

Please sign in to comment.