public static boolean followBlogById( final long blogId, final boolean isAskingToFollow, final ActionListener actionListener) { if (blogId == 0) { if (actionListener != null) { actionListener.onActionResult(false); } return false; } ReaderBlogTable.setIsFollowedBlogId(blogId, isAskingToFollow); ReaderPostTable.setFollowStatusForPostsInBlog(blogId, isAskingToFollow); if (isAskingToFollow) { AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_BLOG_FOLLOWED, blogId); } else { AnalyticsUtils.trackWithBlogDetails(AnalyticsTracker.Stat.READER_BLOG_UNFOLLOWED, blogId); } final String actionName = (isAskingToFollow ? "follow" : "unfollow"); final String path = "sites/" + blogId + "/follows/" + (isAskingToFollow ? "new" : "mine/delete"); com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() { @Override public void onResponse(JSONObject jsonObject) { boolean success = isFollowActionSuccessful(jsonObject, isAskingToFollow); if (success) { AppLog.d(T.READER, "blog " + actionName + " succeeded"); } else { AppLog.w( T.READER, "blog " + actionName + " failed - " + jsonToString(jsonObject) + " - " + path); localRevertFollowBlogId(blogId, isAskingToFollow); } if (actionListener != null) { actionListener.onActionResult(success); } } }; RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { AppLog.w(T.READER, "blog " + actionName + " failed with error"); AppLog.e(T.READER, volleyError); localRevertFollowBlogId(blogId, isAskingToFollow); if (actionListener != null) { actionListener.onActionResult(false); } } }; WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener); return true; }
/* * block a blog - result includes the list of posts that were deleted by the block so they * can be restored if the user undoes the block */ public static BlockedBlogResult blockBlogFromReader( final long blogId, final ActionListener actionListener) { final BlockedBlogResult blockResult = new BlockedBlogResult(); blockResult.blogId = blogId; blockResult.deletedPosts = ReaderPostTable.getPostsInBlog(blogId, 0, false); blockResult.wasFollowing = ReaderBlogTable.isFollowedBlog(blogId); ReaderPostTable.deletePostsInBlog(blogId); ReaderBlogTable.setIsFollowedBlogId(blogId, false); com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() { @Override public void onResponse(JSONObject jsonObject) { if (actionListener != null) { actionListener.onActionResult(true); } } }; RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { AppLog.e(T.READER, volleyError); ReaderPostTable.addOrUpdatePosts(null, blockResult.deletedPosts); if (blockResult.wasFollowing) { ReaderBlogTable.setIsFollowedBlogId(blogId, true); } if (actionListener != null) { actionListener.onActionResult(false); } } }; AppLog.i(T.READER, "blocking blog " + blogId); String path = "me/block/sites/" + Long.toString(blogId) + "/new"; WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener); return blockResult; }
/* * called when a follow/unfollow fails, restores local data to previous state */ private static void localRevertFollowBlogId(long blogId, boolean isAskingToFollow) { ReaderBlogTable.setIsFollowedBlogId(blogId, !isAskingToFollow); ReaderPostTable.setFollowStatusForPostsInBlog(blogId, !isAskingToFollow); }