Skip to content

Commit

Permalink
Merge pull request #20430 from wordpress-mobile/issue/13989-Fix-Follo…
Browse files Browse the repository at this point in the history
…wing-Sites-list-is-incomplete

[Reader] Fix for Incomplete subscribed sites list
  • Loading branch information
daniloercoli authored Mar 11, 2024
2 parents 2bf30f7 + 3bdb704 commit 618f509
Showing 1 changed file with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void performTasks(EnumSet<UpdateTask> tasks, Object companion) {
fetchInterestTags();
}
if (tasks.contains(UpdateTask.FOLLOWED_BLOGS)) {
updateFollowedBlogs();
updateFollowedBlogs(1, new ReaderBlogList());
}
}

Expand Down Expand Up @@ -297,56 +297,69 @@ public void run() {
/***
* request the list of blogs the current user is following
*/
private void updateFollowedBlogs() {
private void updateFollowedBlogs(final int page, final ReaderBlogList serverBlogs) {
RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
handleFollowedBlogsResponse(jsonObject);
handleFollowedBlogsResponse(serverBlogs, jsonObject);
}
};
RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(AppLog.T.READER, volleyError);
serverBlogs.clear();
taskCompleted(UpdateTask.FOLLOWED_BLOGS);
}
};

AppLog.d(AppLog.T.READER, "reader service > updating followed blogs");
AppLog.d(AppLog.T.READER, "reader service > updating followed blogs. Page requested: " + page);
// request using ?meta=site,feed to get extra info
WordPress.getRestClientUtilsV1_2().get("read/following/mine?meta=site%2Cfeed", listener, errorListener);
WordPress.getRestClientUtilsV1_2()
.get("read/following/mine?number=100&page=" + page + "&meta=site%2Cfeed", listener, errorListener);
}

private void handleFollowedBlogsResponse(final JSONObject jsonObject) {
private void handleFollowedBlogsResponse(final ReaderBlogList serverBlogs, final JSONObject jsonObject) {
new Thread() {
@Override
public void run() {
ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject);
ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs();
ReaderBlogList currentPageServerResponse = ReaderBlogList.fromJson(jsonObject);

// This is required because under rare circumstances the server can return duplicates.
// We could have modified the function isSameList to eliminate the length check,
// but it's better to keep it separate since we aim to remove this check as soon as possible.
removeDuplicateBlogs(serverBlogs);
removeDuplicateBlogs(currentPageServerResponse);

boolean sitesSubscribedChanged = false;
final int totalSites = jsonObject == null ? 0 : jsonObject.optInt("total_subscriptions", 0);

if (!localBlogs.isSameList(serverBlogs)) {
// always update the list of followed blogs if there are *any* changes between
// server and local (including subscription count, description, etc.)
ReaderBlogTable.setFollowedBlogs(serverBlogs);
// ...but only update the follow status and alert that followed blogs have
// changed if the server list doesn't have the same blogs as the local list
// (ie: a blog has been followed/unfollowed since local was last updated)
if (!localBlogs.hasSameBlogs(serverBlogs)) {
ReaderPostTable.updateFollowedStatus();
AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed");
sitesSubscribedChanged = true;
final int page = jsonObject == null ? 1 : jsonObject.optInt("page", 1);
final int numberOfSitesReturned = jsonObject == null ? 0 : jsonObject.optInt("number", 0);
serverBlogs.addAll(currentPageServerResponse);
if (numberOfSitesReturned > 90) {
// 90 appears to be a magic number here, and in a way, it is.
// The server doesn't always return the exact number of requested sites, likely due to deleted or
// suspended sites. In the worst-case scenario, we might make an additional request that returns 0.
updateFollowedBlogs(page + 1, serverBlogs);
} else {
ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs();
if (!localBlogs.isSameList(serverBlogs)) {
// always update the list of followed blogs if there are *any* changes between
// server and local (including subscription count, description, etc.)
ReaderBlogTable.setFollowedBlogs(serverBlogs);
// ...but only update the follow status and alert that followed blogs have
// changed if the server list doesn't have the same blogs as the local list
// (ie: a blog has been followed/unfollowed since local was last updated)
if (!localBlogs.hasSameBlogs(serverBlogs)) {
ReaderPostTable.updateFollowedStatus();
AppLog.i(AppLog.T.READER, "reader blogs service > followed blogs changed: "
+ totalSites);
sitesSubscribedChanged = true;
}
}
EventBus.getDefault().post(new FollowedBlogsFetched(totalSites, sitesSubscribedChanged));
serverBlogs.clear();
taskCompleted(UpdateTask.FOLLOWED_BLOGS);
}
EventBus.getDefault().post(new FollowedBlogsFetched(totalSites, sitesSubscribedChanged));
taskCompleted(UpdateTask.FOLLOWED_BLOGS);
}
}.start();
}
Expand Down

0 comments on commit 618f509

Please sign in to comment.