Skip to content

Commit

Permalink
TFJ-629 list manipulation with slug - ListMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke committed Jul 18, 2012
1 parent 45500f1 commit 3581773
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 4 deletions.
76 changes: 76 additions & 0 deletions twitter4j-async/src/main/java/twitter4j/AsyncTwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,25 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
});
}

/**
* {@inheritDoc}
*/
@Override
public void updateUserList(final long ownerId, final String slug, final String newListName, final boolean isPublicList, final String newDescription) {
getDispatcher().invokeLater(new AsyncTask(UPDATE_USER_LIST, listeners) {
@Override
public void invoke(List<TwitterListener> listeners) throws TwitterException {
UserList list = twitter.updateUserList(ownerId, slug, newListName, isPublicList, newDescription);
for (TwitterListener listener : listeners) {
try {
listener.updatedUserList(list);
} catch (Exception ignore) {
}
}
}
});
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -1082,6 +1101,25 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
});
}

/**
* {@inheritDoc}
*/
@Override
public void showUserList(final long ownerId, final String slug) {
getDispatcher().invokeLater(new AsyncTask(UPDATE_USER_LIST, listeners) {
@Override
public void invoke(List<TwitterListener> listeners) throws TwitterException {
UserList list = twitter.showUserList(ownerId, slug);
for (TwitterListener listener : listeners) {
try {
listener.gotShowUserList(list);
} catch (Exception ignore) {
}
}
}
});
}

/**
* {@inheritDoc}
*/
Expand All @@ -1101,6 +1139,25 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
});
}

/**
* {@inheritDoc}
*/
@Override
public void destroyUserList(final long ownerId, final String slug) {
getDispatcher().invokeLater(new AsyncTask(DESTROY_USER_LIST, listeners) {
@Override
public void invoke(List<TwitterListener> listeners) throws TwitterException {
UserList list = twitter.destroyUserList(ownerId, slug);
for (TwitterListener listener : listeners) {
try {
listener.destroyedUserList(list);
} catch (Exception ignore) {
}
}
}
});
}

/**
* {@inheritDoc}
*/
Expand All @@ -1120,6 +1177,25 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
});
}

/**
* {@inheritDoc}
*/
@Override
public void getUserListStatuses(final long ownerId, final String slug, final Paging paging) {
getDispatcher().invokeLater(new AsyncTask(USER_LIST_STATUSES, listeners) {
@Override
public void invoke(List<TwitterListener> listeners) throws TwitterException {
ResponseList<Status> statuses = twitter.getUserListStatuses(ownerId, slug, paging);
for (TwitterListener listener : listeners) {
try {
listener.gotUserListStatuses(statuses);
} catch (Exception ignore) {
}
}
}
});
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public interface ListMethodsAsync {
*/
void updateUserList(int listId, String newListName, boolean isPublicList, String newDescription);


/**
* Updates the specified list.
* <br>This method calls http://api.twitter.com/1/lists/update.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @param newListName What you'd like to change the list's name to.
* @param isPublicList Whether your list is public or private. Optional. Values can be public or private. Lists are public by default if no mode is specified.
* @param newDescription What you'd like to change the list description to.
* @see <a href="https://dev.twitter.com/docs/api/1/post/lists/update ">POST lists/update | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
void updateUserList(long ownerId, String slug, String newListName, boolean isPublicList, String newDescription);

/**
* List the lists of the specified user. Private lists will be included if the authenticated users is the same as the user whose lists are being returned.
* <br>This method calls http://api.twitter.com/1/lists.json
Expand Down Expand Up @@ -79,6 +94,18 @@ public interface ListMethodsAsync {
*/
void showUserList(int listId);


/**
* Show the specified list. Private lists will only be shown if the authenticated user owns the specified list.
* <br>This method calls http://api.twitter.com/1/lists/show.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @see <a href="https://dev.twitter.com/docs/api/1/get/lists/show">https://dev.twitter.com/docs/api/1/get/lists/show | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
void showUserList(long ownerId, String slug);

/**
* Deletes the specified list. Must be owned by the authenticated user.
* <br>This method calls http://api.twitter.com/1/lists/destroy.json
Expand All @@ -89,6 +116,18 @@ public interface ListMethodsAsync {
*/
void destroyUserList(int listId);


/**
* Deletes the specified list. Must be owned by the authenticated user.
* <br>This method calls http://api.twitter.com/1/lists/destroy.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @see <a href="https://dev.twitter.com/docs/api/1/post/lists/destroy">POST lists/destroy | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
void destroyUserList(long ownerId, String slug);

/**
* Show tweet timeline for members of the specified list.
* <br>http://api.twitter.com/1/user/lists/list_id/statuses.json
Expand All @@ -100,12 +139,23 @@ public interface ListMethodsAsync {
*/
void getUserListStatuses(int listId, Paging paging);

/**
* Show tweet timeline for members of the specified list.
* <br>http://api.twitter.com/1/user/lists/list_id/statuses.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @param paging controls pagination. Supports since_id, max_id, count and page parameters.
* @see <a href="https://dev.twitter.com/docs/api/1/get/lists/statuses">GET lists/statuses | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
void getUserListStatuses(long ownerId, String slug, Paging paging);

/**
* List the lists the authenticating user has been added to.
* <br>This method calls http://api.twitter.com/1/lists/memberships.json
*
* @param cursor Breaks the results into pages. A single page contains 20 lists. Provide a value of -1 to begin paging. Provide values as returned to in the response body's next_cursor and previous_cursor attributes to page back and forth in the list.
* @return the list of lists
* @see <a href="https://dev.twitter.com/docs/api/1/get/lists/memberships">GET lists/memberships | Twitter Developers</a>
* @since Twitter4J 2.2.4
*/
Expand Down
50 changes: 47 additions & 3 deletions twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -587,9 +588,22 @@ public UserList createUserList(String listName, boolean isPublicList, String des
*/
@Override
public UserList updateUserList(int listId, String newListName, boolean isPublicList, String newDescription) throws TwitterException {
return updateUserList(newListName, isPublicList, newDescription, new HttpParameter("list_id", listId));
}

/**
* {@inheritDoc}
*/
@Override
public UserList updateUserList(long ownerId, String slug, String newListName, boolean isPublicList, String newDescription) throws TwitterException {
return updateUserList(newListName, isPublicList, newDescription, new HttpParameter("owner_id", ownerId)
, new HttpParameter("slug", slug));
}

private UserList updateUserList(String newListName, boolean isPublicList, String newDescription, HttpParameter... params) throws TwitterException {
ensureAuthorizationEnabled();
List<HttpParameter> httpParams = new ArrayList<HttpParameter>();
httpParams.add(new HttpParameter("list_id", listId));
Collections.addAll(httpParams, params);
if (newListName != null) {
httpParams.add(new HttpParameter("name", newListName));
}
Expand Down Expand Up @@ -621,8 +635,16 @@ public PagableResponseList<UserList> getUserLists(long listOwnerUserId, long cur
*/
@Override
public UserList showUserList(int listId) throws TwitterException {
return factory.createAUserList(get(conf.getRestBaseURL() + "lists/show.json?list_id="
+ listId));
return factory.createAUserList(get(conf.getRestBaseURL() + "lists/show.json?list_id=" + listId));
}

/**
* {@inheritDoc}
*/
@Override
public UserList showUserList(long ownerId, String slug) throws TwitterException {
return factory.createAUserList(get(conf.getRestBaseURL() + "lists/show.json?owner_id=" + ownerId + "&slug="
+ slug));
}

/**
Expand All @@ -635,6 +657,17 @@ public UserList destroyUserList(int listId) throws TwitterException {
new HttpParameter[]{new HttpParameter("list_id", listId)}));
}

/**
* {@inheritDoc}
*/
@Override
public UserList destroyUserList(long ownerId, String slug) throws TwitterException {
ensureAuthorizationEnabled();
return factory.createAUserList(post(conf.getRestBaseURL() + "lists/destroy.json",
new HttpParameter[]{new HttpParameter("owner_id", ownerId)
, new HttpParameter("slug", slug)}));
}

/**
* {@inheritDoc}
*/
Expand All @@ -645,6 +678,17 @@ public ResponseList<Status> getUserListStatuses(int listId, Paging paging) throw
, new HttpParameter("list_id", listId))));
}

/**
* {@inheritDoc}
*/
@Override
public ResponseList<Status> getUserListStatuses(long ownerId, String slug, Paging paging) throws TwitterException {
return factory.createStatusList(get(conf.getRestBaseURL() + "lists/statuses.json"
, mergeParameters(paging.asPostParameterArray(Paging.SMCP, Paging.PER_PAGE)
, new HttpParameter[]{new HttpParameter("owner_id", ownerId)
, new HttpParameter("slug", slug)})));
}

/**
* {@inheritDoc}
*/
Expand Down
58 changes: 58 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/api/ListMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ UserList createUserList(String listName, boolean isPublicList, String descriptio
UserList updateUserList(int listId, String newListName, boolean isPublicList, String newDescription)
throws TwitterException;

/**
* Updates the specified list.
* <br>This method calls http://api.twitter.com/1/lists/update.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @param newListName What you'd like to change the list's name to.
* @param isPublicList Whether your list is public or private. Optional. Values can be public or private. Lists are public by default if no mode is specified.
* @param newDescription What you'd like to change the list description to.
* @return the updated list
* @throws TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1/post/lists/update ">POST lists/update | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
UserList updateUserList(long ownerId, String slug, String newListName, boolean isPublicList, String newDescription)
throws TwitterException;

/**
* List the lists of the specified user. Private lists will be included if the authenticated users is the same as the user whose lists are being returned.
* <br>This method calls http://api.twitter.com/1/lists.json
Expand Down Expand Up @@ -93,6 +110,19 @@ PagableResponseList<UserList> getUserLists(long listOwnerUserId, long cursor)
*/
UserList showUserList(int listId) throws TwitterException;

/**
* Show the specified list. Private lists will only be shown if the authenticated user owns the specified list.
* <br>This method calls http://api.twitter.com/1/lists/show.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @return the specified list
* @throws TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1/get/lists/show">https://dev.twitter.com/docs/api/1/get/lists/show | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
UserList showUserList(long ownerId, String slug) throws TwitterException;

/**
* Deletes the specified list. Must be owned by the authenticated user.
* <br>This method calls http://api.twitter.com/1/lists/destroy.json
Expand All @@ -105,6 +135,19 @@ PagableResponseList<UserList> getUserLists(long listOwnerUserId, long cursor)
*/
UserList destroyUserList(int listId) throws TwitterException;

/**
* Deletes the specified list. Must be owned by the authenticated user.
* <br>This method calls http://api.twitter.com/1/lists/destroy.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @return the deleted list
* @throws TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1/post/lists/destroy">POST lists/destroy | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
UserList destroyUserList(long ownerId, String slug) throws TwitterException;

/**
* Show tweet timeline for members of the specified list.
* <br>http://api.twitter.com/1/user/lists/list_id/statuses.json
Expand All @@ -119,6 +162,21 @@ PagableResponseList<UserList> getUserLists(long listOwnerUserId, long cursor)
ResponseList<Status> getUserListStatuses(int listId, Paging paging)
throws TwitterException;

/**
* Show tweet timeline for members of the specified list.
* <br>http://api.twitter.com/1/user/lists/list_id/statuses.json
*
* @param ownerId The user ID of the user who owns the list being requested by a slug.
* @param slug slug of the list
* @param paging controls pagination. Supports since_id, max_id, count and page parameters.
* @return list of statuses for members of the specified list
* @throws TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1/get/lists/statuses">GET lists/statuses | Twitter Developers</a>
* @since Twitter4J 3.0.0
*/
ResponseList<Status> getUserListStatuses(long ownerId, String slug, Paging paging)
throws TwitterException;

/**
* List the lists the authenticating user has been added to.
* <br>This method calls http://api.twitter.com/1/lists/memberships.json
Expand Down
16 changes: 16 additions & 0 deletions twitter4j-core/src/test/java/twitter4j/ListMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public void testListMethods() throws Exception {
assertNotNull(DataObjectFactory.getRawJSON(userList));
assertNotNull(userList);

userList = twitter1.showUserList(twitter1.getId(), userList.getSlug());
assertEquals(userList, DataObjectFactory.createUserList(DataObjectFactory.getRawJSON(userList)));
assertNotNull(DataObjectFactory.getRawJSON(userList));
assertNotNull(userList);

List<Status> statuses = twitter1.getUserListStatuses(userList.getId(), new Paging());
if (statuses.size() > 0) {
assertEquals(statuses.get(0), DataObjectFactory.createStatus(DataObjectFactory.getRawJSON(statuses.get(0))));
Expand Down Expand Up @@ -85,6 +90,17 @@ public void testListMethods() throws Exception {
assertNotNull(userList);
assertEquals("testpoint2", userList.getName());
assertEquals("description2", userList.getDescription());

userList = twitter1.updateUserList(twitter1.getId(), userList.getSlug(), "testpoint3", true, "description3");
assertEquals(userList, DataObjectFactory.createUserList(DataObjectFactory.getRawJSON(userList)));
// workarounding issue 2166
// http://code.google.com/p/twitter-api/issues/detail?id=2166
userList = twitter1.showUserList(userList.getId());
assertTrue(userList.isPublic());
assertNotNull(DataObjectFactory.getRawJSON(userList));
assertNotNull(userList);
assertEquals("testpoint3", userList.getName());
assertEquals("description3", userList.getDescription());
}

public void testListMemberMethods() throws Exception {
Expand Down

0 comments on commit 3581773

Please sign in to comment.