user bug fix

This commit is contained in:
nuclearfog 2023-03-24 20:24:26 +01:00
parent 9c6cc588de
commit 962a77d3e1
No known key found for this signature in database
GPG Key ID: 03488A185C476379
4 changed files with 40 additions and 15 deletions

View File

@ -946,8 +946,14 @@ public class Mastodon implements Connection {
*/
private User getCredentials(String host, @NonNull String bearer) throws MastodonException {
try {
return createUser(get(host, ENDPOINT_VERIFY_CREDENTIALS, bearer, new ArrayList<>()));
} catch (IOException e) {
Response response = get(host, ENDPOINT_VERIFY_CREDENTIALS, bearer, new ArrayList<>());
ResponseBody body = response.body();
if (response.code() == 200 && body != null) {
JSONObject json = new JSONObject(body.string());
return new MastodonUser(json);
}
throw new MastodonException(response);
} catch (IOException|JSONException e) {
throw new MastodonException(e);
}
}

View File

@ -39,6 +39,18 @@ public class MastodonUser implements User {
private Emoji[] emojis = {};
/**
* constructor used to create an user instance of the current user
*
* @param json json object used by Mastodon API
*/
public MastodonUser(JSONObject json) throws JSONException {
this(json, 0L);
isCurrentUser = true;
}
/**
* default constructor for all user instances
*
* @param json json object used by Mastodon API
* @param currentUserId current user ID
*/
@ -57,7 +69,6 @@ public class MastodonUser implements User {
follower = json.optInt("followers_count");
statusCount = json.optInt("statuses_count");
locked = json.optBoolean("locked");
isCurrentUser = currentUserId == id;
if (!description.isEmpty()) {
this.description = Jsoup.parse(description).text();
}
@ -76,6 +87,7 @@ public class MastodonUser implements User {
}
try {
id = Long.parseLong(idStr);
isCurrentUser = currentUserId == id;
} catch (NumberFormatException e) {
throw new JSONException("bad user ID:" + idStr);
}

View File

@ -6,7 +6,6 @@ import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.nuclearfog.twidda.backend.api.twitter.v1.impl.MediaV1;
import org.nuclearfog.twidda.model.Media;
import org.nuclearfog.twidda.model.Message;
import org.nuclearfog.twidda.model.User;

View File

@ -21,11 +21,16 @@ public class UserV1 implements User {
public static final long serialVersionUID = 7893496988800499358L;
/**
* API query to prevent disable statuses in the user object.
*/
public static final String PARAM_SKIP_STATUS = "skip_status=true";
/**
* API query parameter to get all required json fields
*/
public static final String PARAM_INCLUDE_ENTITIES = "include_entities=true";
private long id;
private long timestamp;
private String username;
@ -43,23 +48,25 @@ public class UserV1 implements User {
private boolean isLocked;
private boolean followReqSent;
private boolean defaultImage;
private boolean isCurrentUser = true;
private boolean isCurrentUser;
/**
* @param json JSON object containing user information
* @param twitterId ID of the current user
* @throws JSONException if values are missing
* user constructor used to create instance of the current user
*
* @param json JSON object containing user information
*/
public UserV1(JSONObject json, long twitterId) throws JSONException {
this(json);
isCurrentUser = twitterId == id;
public UserV1(JSONObject json) throws JSONException {
this(json, 0L);
isCurrentUser = true;
}
/**
* @param json JSON object containing user information
* @throws JSONException if values are missing
* default user constructor
*
* @param json JSON object containing user information
* @param twitterId ID of the current user
*/
public UserV1(JSONObject json) throws JSONException {
public UserV1(JSONObject json, long twitterId) throws JSONException {
String idStr = json.getString("id_str");
String profileImageUrl = json.optString("profile_image_url_https", "");
String profileBannerUrl = json.optString("profile_banner_url", "");
@ -90,6 +97,7 @@ public class UserV1 implements User {
}
try {
id = Long.parseLong(idStr);
isCurrentUser = twitterId == id;
} catch (NumberFormatException e) {
throw new JSONException("bad user ID: " + idStr);
}