コード例 #1
0
  /*
   * get the latest version of this post
   */
  private void updatePost() {
    if (!hasPost() || !mPost.isWP()) {
      return;
    }

    final int numLikesBefore = ReaderLikeTable.getNumLikesForPost(mPost);

    ReaderActions.UpdateResultListener resultListener =
        new ReaderActions.UpdateResultListener() {
          @Override
          public void onUpdateResult(ReaderActions.UpdateResult result) {
            if (!isAdded()) {
              return;
            }
            // if the post has changed, reload it from the db and update the like/comment counts
            if (result.isNewOrChanged()) {
              mPost = ReaderPostTable.getPost(mBlogId, mPostId, false);
              refreshIconCounts();
            }
            // refresh likes if necessary - done regardless of whether the post has changed
            // since it's possible likes weren't stored until the post was updated
            if (result != ReaderActions.UpdateResult.FAILED
                && numLikesBefore != ReaderLikeTable.getNumLikesForPost(mPost)) {
              refreshLikes();
            }
          }
        };
    ReaderPostActions.updatePost(mPost, resultListener);
  }
コード例 #2
0
  /** like/unlike the passed post */
  public static boolean performLikeAction(final ReaderPost post, final boolean isAskingToLike) {
    // do nothing if post's like state is same as passed
    boolean isCurrentlyLiked = ReaderPostTable.isPostLikedByCurrentUser(post);
    if (isCurrentlyLiked == isAskingToLike) {
      AppLog.w(T.READER, "post like unchanged");
      return false;
    }

    // update like status and like count in local db
    int numCurrentLikes = ReaderPostTable.getNumLikesForPost(post.blogId, post.postId);
    int newNumLikes = (isAskingToLike ? numCurrentLikes + 1 : numCurrentLikes - 1);
    if (newNumLikes < 0) {
      newNumLikes = 0;
    }
    ReaderPostTable.setLikesForPost(post, newNumLikes, isAskingToLike);
    ReaderLikeTable.setCurrentUserLikesPost(post, isAskingToLike);

    final String actionName = isAskingToLike ? "like" : "unlike";
    String path = "sites/" + post.blogId + "/posts/" + post.postId + "/likes/";
    if (isAskingToLike) {
      path += "new";
    } else {
      path += "mine/delete";
    }

    com.wordpress.rest.RestRequest.Listener listener =
        new RestRequest.Listener() {
          @Override
          public void onResponse(JSONObject jsonObject) {
            AppLog.d(T.READER, String.format("post %s succeeded", actionName));
          }
        };

    RestRequest.ErrorListener errorListener =
        new RestRequest.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            String error = VolleyUtils.errStringFromVolleyError(volleyError);
            if (TextUtils.isEmpty(error)) {
              AppLog.w(T.READER, String.format("post %s failed", actionName));
            } else {
              AppLog.w(T.READER, String.format("post %s failed (%s)", actionName, error));
            }
            AppLog.e(T.READER, volleyError);
            ReaderPostTable.setLikesForPost(post, post.numLikes, post.isLikedByCurrentUser);
            ReaderLikeTable.setCurrentUserLikesPost(post, post.isLikedByCurrentUser);
          }
        };

    WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);
    return true;
  }
コード例 #3
0
  /*
   * updates local liking users based on the "likes" meta section of the post's json - requires
   * using the /sites/ endpoint with ?meta=likes - returns true if likes have changed
   */
  private static boolean handlePostLikes(final ReaderPost post, JSONObject jsonPost) {
    if (post == null || jsonPost == null) {
      return false;
    }

    JSONObject jsonLikes = JSONUtils.getJSONChild(jsonPost, "meta/data/likes");
    if (jsonLikes == null) {
      return false;
    }

    ReaderUserList likingUsers = ReaderUserList.fromJsonLikes(jsonLikes);
    ReaderUserIdList likingUserIds = likingUsers.getUserIds();

    ReaderUserIdList existingIds = ReaderLikeTable.getLikesForPost(post);
    if (likingUserIds.isSameList(existingIds)) {
      return false;
    }

    ReaderUserTable.addOrUpdateUsers(likingUsers);
    ReaderLikeTable.setLikesForPost(post, likingUserIds);
    return true;
  }