Skip to content
Browse files

TFJ-629 list manipulation with slug - ListMethods

  • Loading branch information...
1 parent 45500f1 commit 3581773086699c6606c87c674c39db76f1e7b5a6 @yusuke committed Jul 18, 2012
View
76 twitter4j-async/src/main/java/twitter4j/AsyncTwitterImpl.java
@@ -1029,6 +1029,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}
+ */
+ @Override
public void getUserLists(final String listOwnerScreenName, final long cursor) {
getDispatcher().invokeLater(new AsyncTask(USER_LISTS, listeners) {
@Override
@@ -1086,6 +1105,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}
+ */
+ @Override
public void destroyUserList(final int listId) {
getDispatcher().invokeLater(new AsyncTask(DESTROY_USER_LIST, listeners) {
@Override
@@ -1105,6 +1143,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}
+ */
+ @Override
public void getUserListStatuses(final int listId, final Paging paging) {
getDispatcher().invokeLater(new AsyncTask(USER_LIST_STATUSES, listeners) {
@Override
@@ -1124,6 +1181,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}
+ */
+ @Override
public void getUserListMemberships(final long cursor) {
getDispatcher().invokeLater(new AsyncTask(USER_LIST_MEMBERSHIPS, listeners) {
@Override
View
52 twitter4j-async/src/main/java/twitter4j/api/ListMethodsAsync.java
@@ -47,6 +47,21 @@
*/
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
@@ -79,6 +94,18 @@
*/
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
@@ -89,6 +116,18 @@
*/
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
@@ -101,11 +140,22 @@
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
*/
View
50 twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
@@ -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;
@@ -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));
}
@@ -621,8 +635,16 @@ public UserList updateUserList(int listId, String newListName, boolean isPublicL
*/
@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));
}
/**
@@ -639,6 +661,17 @@ public UserList destroyUserList(int listId) throws TwitterException {
* {@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}
+ */
+ @Override
public ResponseList<Status> getUserListStatuses(int listId, Paging paging) throws TwitterException {
return factory.createStatusList(get(conf.getRestBaseURL() + "lists/statuses.json"
, mergeParameters(paging.asPostParameterArray(Paging.SMCP, Paging.PER_PAGE)
@@ -649,6 +682,17 @@ public UserList destroyUserList(int listId) throws TwitterException {
* {@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}
+ */
+ @Override
public PagableResponseList<UserList> getUserListMemberships(long cursor) throws TwitterException {
ensureAuthorizationEnabled();
return factory.createPagableUserListList(get(conf.getRestBaseURL() + "lists/memberships.json?cursor=" + cursor));
View
58 twitter4j-core/src/main/java/twitter4j/api/ListMethods.java
@@ -54,6 +54,23 @@ UserList updateUserList(int listId, String newListName, boolean isPublicList, St
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
*
@@ -94,6 +111,19 @@ UserList updateUserList(int listId, String newListName, boolean isPublicList, St
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
*
@@ -106,6 +136,19 @@ UserList updateUserList(int listId, String newListName, boolean isPublicList, St
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
*
@@ -120,6 +163,21 @@ UserList updateUserList(int listId, String newListName, boolean isPublicList, St
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
*
View
16 twitter4j-core/src/test/java/twitter4j/ListMethodsTest.java
@@ -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))));
@@ -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 {

0 comments on commit 3581773

Please sign in to comment.
Something went wrong with that request. Please try again.