From 799741857d273338da0b3a3ba4c006330b8022f8 Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Tue, 6 Sep 2022 22:19:00 +0200 Subject: [PATCH] bug fix --- .../twidda/backend/api/impl/UserV1.java | 36 ++++------- .../twidda/backend/api/impl/UserV2.java | 61 +++++++++---------- .../twidda/backend/utils/StringTools.java | 19 ++++++ 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV1.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV1.java index 9ae1490c..4fb0336e 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV1.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV1.java @@ -57,18 +57,12 @@ public class UserV1 implements User { */ public UserV1(JSONObject json) throws JSONException { String idStr = json.getString("id_str"); - if (ID_PATTERN.matcher(idStr).matches()) { - id = Long.parseLong(idStr); - } else { - throw new JSONException("bad ID: " + idStr); - } + String profileImageUrl = json.optString("profile_image_url_https"); username = json.optString("name"); screenName = '@' + json.optString("screen_name"); isVerified = json.optBoolean("verified"); isLocked = json.optBoolean("protected"); - profileImageUrl = getProfileImage(json); profileBannerUrl = json.optString("profile_banner_url"); - description = getDescription(json); location = json.optString("location"); following = json.optInt("friends_count"); follower = json.optInt("followers_count"); @@ -77,7 +71,19 @@ public class UserV1 implements User { followReqSent = json.optBoolean("follow_request_sent"); defaultImage = json.optBoolean("default_profile_image"); created = StringTools.getTime1(json.optString("created_at")); + + description = getDescription(json); url = getUrl(json); + if (defaultImage) { + this.profileImageUrl = profileImageUrl; + } else { + this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl); + } + if (ID_PATTERN.matcher(idStr).matches()) { + id = Long.parseLong(idStr); + } else { + throw new JSONException("bad ID: " + idStr); + } } @Override @@ -232,20 +238,4 @@ public class UserV1 implements User { } return ""; } - - /** - * get original sized profile image url - * - * @param json root json object of user v1 - * @return profile image url - */ - private String getProfileImage(JSONObject json) { - String profileImage = json.optString("profile_image_url_https"); - // set profile image url - int start = profileImage.lastIndexOf('_'); - int end = profileImage.lastIndexOf('.'); - if (!defaultImage && start > 0 && end > 0) - return profileImage.substring(0, start) + profileImage.substring(end); - return profileImage; - } } \ No newline at end of file diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV2.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV2.java index 1f67ecb8..2eff139c 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV2.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/impl/UserV2.java @@ -40,11 +40,9 @@ public class UserV2 implements User { private int following; private int follower; private int tweetCount; - private int likeCount; private boolean isCurrentUser; private boolean isVerified; private boolean isProtected; - private boolean followReqSent; private boolean defaultImage; /** @@ -52,36 +50,37 @@ public class UserV2 implements User { * @param twitterId ID of the current user */ public UserV2(JSONObject json, long twitterId) throws JSONException { + JSONObject metrics = json.optJSONObject("public_metrics"); + String idStr = json.getString("id"); - if (ID_PATTERN.matcher(idStr).matches()) { - id = Long.parseLong(idStr); - } else { - throw new JSONException("bad ID: " + idStr); - } + String profileImageUrl = json.optString("profile_image_url"); username = json.optString("name"); screenName = '@' + json.optString("username"); isProtected = json.optBoolean("protected"); location = json.optString("location"); isVerified = json.optBoolean("verified"); - profileImageUrl = json.optString("profile_image_url"); profileBannerUrl = json.optString("profile_banner_url"); created = StringTools.getTime2(json.optString("created_at")); - description = getDescription(json); - url = getUrl(json); - isCurrentUser = id == twitterId; + defaultImage = profileImageUrl.contains("default_profile_images"); - JSONObject metrics = json.optJSONObject("public_metrics"); + url = getUrl(json); + description = getDescription(json); + isCurrentUser = id == twitterId; if (metrics != null) { following = metrics.optInt("following_count"); follower = metrics.optInt("followers_count"); tweetCount = metrics.optInt("tweet_count"); } - defaultImage = profileImageUrl.contains("default_profile_images"); - - // not yet implemented in API 2.0 - // todo check if Twitter added these values - likeCount = -1; - followReqSent = false; + if (defaultImage) { + this.profileImageUrl = profileImageUrl; + } else { + this.profileImageUrl = StringTools.createProfileImageLink(profileImageUrl); + } + if (ID_PATTERN.matcher(idStr).matches()) { + id = Long.parseLong(idStr); + } else { + throw new JSONException("bad ID: " + idStr); + } } @Override @@ -141,7 +140,8 @@ public class UserV2 implements User { @Override public boolean followRequested() { - return followReqSent; + // todo not yet implemented in API V2 + return false; } @Override @@ -161,7 +161,8 @@ public class UserV2 implements User { @Override public int getFavoriteCount() { - return likeCount; + // todo not yet implemented in API V2 + return -1; } @Override @@ -225,19 +226,15 @@ public class UserV2 implements User { * @return expanded url */ private String getUrl(JSONObject json) { - JSONObject entities = json.optJSONObject("entities"); - if (entities != null) { - JSONObject urlJson = entities.optJSONObject("url"); - if (urlJson != null) { - try { - JSONArray urls = urlJson.getJSONArray("urls"); - if (urls.length() > 0) { - return urls.getJSONObject(0).getString("display_url"); - } - } catch (JSONException e) { - // ignore - } + try { + JSONObject entities = json.getJSONObject("entities"); + JSONObject urlJson = entities.getJSONObject("url"); + JSONArray urls = urlJson.getJSONArray("urls"); + if (urls.length() > 0) { + return urls.getJSONObject(0).getString("display_url"); } + } catch (JSONException e) { + // ignore } return ""; } diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/utils/StringTools.java b/app/src/main/java/org/nuclearfog/twidda/backend/utils/StringTools.java index 98d004b3..a0beeffc 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/utils/StringTools.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/utils/StringTools.java @@ -340,4 +340,23 @@ public final class StringTools { throw new IOException("error generating signature!"); } } + + /** + * formate user profile image link. (remove suffix but keep the file extension if any) + * + * @param profileImage user profile image + * @return formatted link + */ + public static String createProfileImageLink(String profileImage) { + // set profile image url + int suffix = profileImage.lastIndexOf('_'); + int extension = profileImage.lastIndexOf('.'); + if (suffix > 0 && extension > 0) { + if (suffix > extension) + return profileImage.substring(0, suffix); + else + return profileImage.substring(0, suffix) + profileImage.substring(extension); + } + return profileImage; + } } \ No newline at end of file