From 0662173045f3ce7bb448ad1db132dddd4b3e62c3 Mon Sep 17 00:00:00 2001 From: tinsukE Date: Sat, 6 Jan 2024 20:18:32 +0000 Subject: [PATCH] Allow login with email (#239) While the call to obtain a JWT (`api/v3/user/login`) works for username or email, the app was then following that with a call to ` api/v3/user`, which only works with username (https://lemmy.readme.io/reference/get_user). This would cause the login attempt to fail with the error reported on #235. These changes skip the call to `api/v3/user`, and instead retrieves the user information from the call to `api/v3/site`, that was already being performed. Fixes #235. Question: should the label of the username field be updated to "Username / Email" or "Username or Email"? (screenshot) Reviewed-on: https://codeberg.org/Bazsalanszky/Eternity/pulls/239 Reviewed-by: Bazsalanszky Co-authored-by: tinsukE Co-committed-by: tinsukE --- .../toldi/infinityforlemmy/FetchMyInfo.java | 99 ----------------- .../activities/InstanceInfoActivity.java | 5 +- .../activities/LoginActivity.java | 105 ++++++++---------- .../activities/MainActivity.java | 5 +- .../asynctasks/SwitchAccount.java | 5 +- .../infinityforlemmy/site/FetchSiteInfo.java | 22 ++-- .../toldi/infinityforlemmy/site/SiteInfo.java | 63 +++++------ .../infinityforlemmy/user/MyUserInfo.java | 61 ++++++++++ app/src/main/res/values/strings.xml | 2 +- 9 files changed, 163 insertions(+), 204 deletions(-) delete mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/FetchMyInfo.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/user/MyUserInfo.java diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/FetchMyInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/FetchMyInfo.java deleted file mode 100644 index 4103b37a..00000000 --- a/app/src/main/java/eu/toldi/infinityforlemmy/FetchMyInfo.java +++ /dev/null @@ -1,99 +0,0 @@ -package eu.toldi.infinityforlemmy; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import eu.toldi.infinityforlemmy.apis.LemmyAPI; -import eu.toldi.infinityforlemmy.utils.LemmyUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Retrofit; - -public class FetchMyInfo { - - public static void fetchAccountInfo(final Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase, - String username,String accessToken, final FetchMyInfoListener fetchMyInfoListener) { - LemmyAPI api = retrofit.create(LemmyAPI.class); - - Call userInfo = api.userInfo(username,accessToken); - userInfo.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - if (response.isSuccessful()) { - new ParseAndSaveAccountInfoAsyncTask(response.body(), redditDataRoomDatabase, fetchMyInfoListener).execute(); - } else { - fetchMyInfoListener.onFetchMyInfoFailed(false); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchMyInfoListener.onFetchMyInfoFailed(false); - } - }); - } - - public interface FetchMyInfoListener { - void onFetchMyInfoSuccess(String name, String display_name,String profileImageUrl, String bannerImageUrl); - - void onFetchMyInfoFailed(boolean parseFailed); - } - - private static class ParseAndSaveAccountInfoAsyncTask extends AsyncTask { - private JSONObject jsonResponse; - private RedditDataRoomDatabase redditDataRoomDatabase; - private FetchMyInfoListener fetchMyInfoListener; - private boolean parseFailed; - - private String name; - private String profileImageUrl; - private String bannerImageUrl; - - private String display_name; - - ParseAndSaveAccountInfoAsyncTask(String response, RedditDataRoomDatabase redditDataRoomDatabase, - FetchMyInfoListener fetchMyInfoListener) { - try { - jsonResponse = new JSONObject(response); - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.fetchMyInfoListener = fetchMyInfoListener; - parseFailed = false; - } catch (JSONException e) { - fetchMyInfoListener.onFetchMyInfoFailed(true); - } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - JSONObject person = jsonResponse.getJSONObject("person_view").getJSONObject("person"); - - name = LemmyUtils.actorID2FullName(person.getString("actor_id")); - if (!person.isNull("avatar")) { - profileImageUrl = person.getString("avatar"); - } - if (!person.isNull("banner")) { - bannerImageUrl = person.getString("banner"); - } - display_name = (person.has("display_name")) ? person.getString("display_name") : person.getString("name"); - redditDataRoomDatabase.accountDao().updateAccountInfo(name, profileImageUrl, bannerImageUrl); - } catch (JSONException e) { - parseFailed = true; - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - if (!parseFailed) { - fetchMyInfoListener.onFetchMyInfoSuccess(name,display_name, profileImageUrl, bannerImageUrl); - } else { - fetchMyInfoListener.onFetchMyInfoFailed(true); - } - } - } -} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InstanceInfoActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InstanceInfoActivity.java index db510f95..e4158636 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InstanceInfoActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InstanceInfoActivity.java @@ -42,6 +42,7 @@ import eu.toldi.infinityforlemmy.site.FetchSiteInfo; import eu.toldi.infinityforlemmy.site.SiteInfo; import eu.toldi.infinityforlemmy.site.SiteStatistics; import eu.toldi.infinityforlemmy.user.BasicUserInfo; +import eu.toldi.infinityforlemmy.user.MyUserInfo; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import io.noties.markwon.AbstractMarkwonPlugin; import io.noties.markwon.Markwon; @@ -170,7 +171,7 @@ public class InstanceInfoActivity extends BaseActivity { private void fetchInstanceInfo() { FetchSiteInfo.fetchSiteInfo(mRetorifitHolder.getRetrofit(), null, new FetchSiteInfo.FetchSiteInfoListener() { @Override - public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { + public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) { mLoadingConstraintLayout.setVisibility(View.GONE); toolbar.setTitle(siteInfo.getName()); if (siteInfo.getSidebar() != null) { @@ -197,7 +198,7 @@ public class InstanceInfoActivity extends BaseActivity { } @Override - public void onFetchSiteInfoFailed() { + public void onFetchSiteInfoFailed(boolean parseFailed) { } }); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java index 550206ce..3614a12b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LoginActivity.java @@ -39,7 +39,6 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import eu.toldi.infinityforlemmy.FetchMyInfo; import eu.toldi.infinityforlemmy.Infinity; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; @@ -55,6 +54,7 @@ import eu.toldi.infinityforlemmy.lemmyverse.LemmyInstance; import eu.toldi.infinityforlemmy.lemmyverse.LemmyVerseFetchInstances; import eu.toldi.infinityforlemmy.site.FetchSiteInfo; import eu.toldi.infinityforlemmy.site.SiteInfo; +import eu.toldi.infinityforlemmy.user.MyUserInfo; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.Utils; import retrofit2.Call; @@ -217,67 +217,58 @@ public class LoginActivity extends BaseActivity { try { JSONObject responseJSON = new JSONObject(accountResponse); String accessToken = responseJSON.getString("jwt"); - mRetrofit.setAccessToken(null); + mRetrofit.setAccessToken(accessToken); - FetchMyInfo.fetchAccountInfo(mRetrofit.getRetrofit(), mRedditDataRoomDatabase, username, - accessToken, new FetchMyInfo.FetchMyInfoListener() { - @Override - public void onFetchMyInfoSuccess(String name, String display_name, String profileImageUrl, String bannerImageUrl) { - FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), accessToken, new FetchSiteInfo.FetchSiteInfoListener() { - @Override - public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { - boolean canDownvote = siteInfo.isEnable_downvotes(); - ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name, display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance, canDownvote, mRedditDataRoomDatabase.accountDao(), - () -> { - Intent resultIntent = new Intent(); - setResult(Activity.RESULT_OK, resultIntent); - finish(); - }); - mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply(); - String[] version = siteInfo.getVersion().split("\\."); - if (version.length > 0) { - Log.d("SwitchAccount", "Lemmy Version: " + version[0] + "." + version[1]); - int majorVersion = Integer.parseInt(version[0]); - int minorVersion = Integer.parseInt(version[1]); - if (majorVersion > 0 || (majorVersion == 0 && minorVersion >= 19)) { - mRetrofit.setAccessToken(accessToken); - mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true).apply(); - } else { - mRetrofit.setAccessToken(null); - mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, false).apply(); - } - } - } + FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), accessToken, new FetchSiteInfo.FetchSiteInfoListener() { + @Override + public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) { + if (myUserInfo == null) { + finish(); + Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show(); + return; + } - @Override - public void onFetchSiteInfoFailed() { - ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), name,display_name, accessToken, profileImageUrl, bannerImageUrl, authCode, finalInstance,true, mRedditDataRoomDatabase.accountDao(), - () -> { - Intent resultIntent = new Intent(); - setResult(Activity.RESULT_OK, resultIntent); - finish(); - }); - mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply(); - } + boolean canDownvote = siteInfo.isEnable_downvotes(); + ParseAndInsertNewAccount.parseAndInsertNewAccount(mExecutor, new Handler(), myUserInfo.getQualifiedName(), myUserInfo.getDisplayName(), accessToken, myUserInfo.getProfileImageUrl(), myUserInfo.getBannerImageUrl(), authCode, finalInstance, canDownvote, mRedditDataRoomDatabase.accountDao(), + () -> { + Intent resultIntent = new Intent(); + setResult(Activity.RESULT_OK, resultIntent); + finish(); }); - mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken) - .putString(SharedPreferencesUtils.ACCOUNT_NAME, display_name) - .putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, name) - .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,finalInstance) - .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, profileImageUrl).apply(); + mCurrentAccountSharedPreferences.edit() + .putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken) + .putString(SharedPreferencesUtils.ACCOUNT_NAME, myUserInfo.getDisplayName()) + .putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, myUserInfo.getQualifiedName()) + .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,finalInstance) + .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, myUserInfo.getProfileImageUrl()) + .putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply(); + String[] version = siteInfo.getVersion().split("\\."); + if (version.length > 0) { + Log.d("SwitchAccount", "Lemmy Version: " + version[0] + "." + version[1]); + int majorVersion = Integer.parseInt(version[0]); + int minorVersion = Integer.parseInt(version[1]); + if (majorVersion > 0 || (majorVersion == 0 && minorVersion >= 19)) { + mRetrofit.setAccessToken(accessToken); + mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true).apply(); + } else { + mRetrofit.setAccessToken(null); + mCurrentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, false).apply(); } + } + } - @Override - public void onFetchMyInfoFailed(boolean parseFailed) { - if (parseFailed) { - Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); - } - - finish(); - } - }); + @Override + public void onFetchSiteInfoFailed(boolean parseFailed) { + if (parseFailed) { + finish(); + Toast.makeText(LoginActivity.this, R.string.parse_user_info_error, Toast.LENGTH_SHORT).show(); + } else { + progressBar.setVisibility(ProgressBar.GONE); + loginButton.setEnabled(true); + Toast.makeText(LoginActivity.this, R.string.cannot_fetch_user_info, Toast.LENGTH_SHORT).show(); + } + } + }); } catch (JSONException e) { throw new RuntimeException(e); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java index 166603d7..f651e5c1 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java @@ -128,6 +128,7 @@ import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditData; import eu.toldi.infinityforlemmy.subscribedsubreddit.SubscribedSubredditViewModel; import eu.toldi.infinityforlemmy.subscribeduser.SubscribedUserData; import eu.toldi.infinityforlemmy.user.FetchUserData; +import eu.toldi.infinityforlemmy.user.MyUserInfo; import eu.toldi.infinityforlemmy.user.UserData; import eu.toldi.infinityforlemmy.utils.APIUtils; import eu.toldi.infinityforlemmy.utils.CustomThemeSharedPreferencesUtils; @@ -1147,7 +1148,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb FetchSiteInfo.fetchSiteInfo(mRetrofit.getRetrofit(), mAccessToken, new FetchSiteInfo.FetchSiteInfoListener() { @Override - public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { + public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) { String[] version = siteInfo.getVersion().split("\\."); if (version.length > 0) { Log.d("MainActvity", "Lemmy Version: " + version[0] + "." + version[1]); @@ -1165,7 +1166,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } @Override - public void onFetchSiteInfoFailed() { + public void onFetchSiteInfoFailed(boolean parseFailed) { } }); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java index efe8f65c..1b06dcde 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java @@ -11,6 +11,7 @@ import eu.toldi.infinityforlemmy.RetrofitHolder; import eu.toldi.infinityforlemmy.account.Account; import eu.toldi.infinityforlemmy.site.FetchSiteInfo; import eu.toldi.infinityforlemmy.site.SiteInfo; +import eu.toldi.infinityforlemmy.user.MyUserInfo; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; public class SwitchAccount { @@ -33,7 +34,7 @@ public class SwitchAccount { retrofitHolder.setAccessToken(null); FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() { @Override - public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { + public void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo) { boolean canDownvote = siteInfo.isEnable_downvotes(); currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply(); String[] version = siteInfo.getVersion().split("\\."); @@ -49,7 +50,7 @@ public class SwitchAccount { } @Override - public void onFetchSiteInfoFailed() { + public void onFetchSiteInfoFailed(boolean parseFailed) { Log.e("SwitchAccount", "Failed to fetch site info"); currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply(); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/site/FetchSiteInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/site/FetchSiteInfo.java index b33517b3..8ba2a1e7 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/site/FetchSiteInfo.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/site/FetchSiteInfo.java @@ -1,6 +1,9 @@ package eu.toldi.infinityforlemmy.site; +import org.json.JSONException; + import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.user.MyUserInfo; import retrofit2.Retrofit; public class FetchSiteInfo { @@ -11,24 +14,29 @@ public class FetchSiteInfo { @Override public void onResponse(retrofit2.Call call, retrofit2.Response response) { if (response.isSuccessful()) { - String siteInfoJson = response.body(); - SiteInfo siteInfo = SiteInfo.parseSiteInfo(siteInfoJson); - fetchSiteInfoListener.onFetchSiteInfoSuccess(siteInfo); + try { + String siteInfoJson = response.body(); + SiteInfo siteInfo = SiteInfo.parseSiteInfo(siteInfoJson); + MyUserInfo myUserInfo = MyUserInfo.parseFromSiteInfo(siteInfoJson); + fetchSiteInfoListener.onFetchSiteInfoSuccess(siteInfo, myUserInfo); + } catch (JSONException e) { + fetchSiteInfoListener.onFetchSiteInfoFailed(true); + } } else { - fetchSiteInfoListener.onFetchSiteInfoFailed(); + fetchSiteInfoListener.onFetchSiteInfoFailed(false); } } @Override public void onFailure(retrofit2.Call call, Throwable t) { - fetchSiteInfoListener.onFetchSiteInfoFailed(); + fetchSiteInfoListener.onFetchSiteInfoFailed(false); } } ); } public interface FetchSiteInfoListener { - void onFetchSiteInfoSuccess(SiteInfo siteInfo); - void onFetchSiteInfoFailed(); + void onFetchSiteInfoSuccess(SiteInfo siteInfo, MyUserInfo myUserInfo); + void onFetchSiteInfoFailed(boolean parseFailed); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java index e14d98db..2ab732bc 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java @@ -79,45 +79,40 @@ public class SiteInfo { return version; } - public static SiteInfo parseSiteInfo(String siteInfoJson) { - try { - JSONObject siteInfo = new JSONObject(siteInfoJson); - JSONObject siteView = siteInfo.getJSONObject("site_view"); - JSONObject site = siteView.getJSONObject("site"); - JSONObject localSite = siteView.getJSONObject("local_site"); + public static SiteInfo parseSiteInfo(String siteInfoJson) throws JSONException { + JSONObject siteInfo = new JSONObject(siteInfoJson); + JSONObject siteView = siteInfo.getJSONObject("site_view"); + JSONObject site = siteView.getJSONObject("site"); + JSONObject localSite = siteView.getJSONObject("local_site"); - int id = site.getInt("id"); - String name = site.getString("name"); - String sidebar = null; - if (site.has("sidebar")) - sidebar = site.getString("sidebar"); - String version = siteInfo.getString("version"); + int id = site.getInt("id"); + String name = site.getString("name"); + String sidebar = null; + if (site.has("sidebar")) + sidebar = site.getString("sidebar"); + String version = siteInfo.getString("version"); - String description = null; - if (site.has("description")) - description = site.getString("description"); + String description = null; + if (site.has("description")) + description = site.getString("description"); - boolean enable_downvotes = localSite.getBoolean("enable_downvotes"); - boolean enable_nsfw = localSite.getBoolean("enable_nsfw"); - boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only"); + boolean enable_downvotes = localSite.getBoolean("enable_downvotes"); + boolean enable_nsfw = localSite.getBoolean("enable_nsfw"); + boolean community_creation_admin_only = localSite.getBoolean("community_creation_admin_only"); - JSONObject counts = siteView.getJSONObject("counts"); - List admins = new ArrayList<>(); - if (siteInfo.has("admins")) { - JSONArray adminsJson = siteInfo.getJSONArray("admins"); - for (int i = 0; i < adminsJson.length(); i++) { - JSONObject adminJson = adminsJson.getJSONObject(i).getJSONObject("person"); - admins.add(new BasicUserInfo(adminJson.getInt("id"), adminJson.getString("name"), - LemmyUtils.actorID2FullName(adminJson.getString("actor_id")), adminJson.optString("avatar ", ""), - adminJson.optString("display_name", adminJson.getString("name"))) - ); - } + JSONObject counts = siteView.getJSONObject("counts"); + List admins = new ArrayList<>(); + if (siteInfo.has("admins")) { + JSONArray adminsJson = siteInfo.getJSONArray("admins"); + for (int i = 0; i < adminsJson.length(); i++) { + JSONObject adminJson = adminsJson.getJSONObject(i).getJSONObject("person"); + admins.add(new BasicUserInfo(adminJson.getInt("id"), adminJson.getString("name"), + LemmyUtils.actorID2FullName(adminJson.getString("actor_id")), adminJson.optString("avatar ", ""), + adminJson.optString("display_name", adminJson.getString("name"))) + ); } - - return new SiteInfo(id, name, version, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts)); - } catch (JSONException e) { - e.printStackTrace(); - return null; } + + return new SiteInfo(id, name, version, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts)); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/user/MyUserInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/user/MyUserInfo.java new file mode 100644 index 00000000..26eb8c7a --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/user/MyUserInfo.java @@ -0,0 +1,61 @@ +package eu.toldi.infinityforlemmy.user; + +import org.json.JSONException; +import org.json.JSONObject; + +import eu.toldi.infinityforlemmy.utils.LemmyUtils; + +public class MyUserInfo { + + private final String qualifiedName; + private final String name; + private final String displayName; + private final String profileImageUrl; + private final String bannerImageUrl; + + private MyUserInfo(String qualifiedName, String name, String displayName, String profileImageUrl, String bannerImageUrl) { + this.qualifiedName = qualifiedName; + this.name = name; + this.displayName = displayName; + this.profileImageUrl = profileImageUrl; + this.bannerImageUrl = bannerImageUrl; + } + + public String getQualifiedName() { + return qualifiedName; + } + + public String getName() { + return name; + } + + public String getDisplayName() { + return displayName; + } + + public String getProfileImageUrl() { + return profileImageUrl; + } + + public String getBannerImageUrl() { + return bannerImageUrl; + } + + public static MyUserInfo parseFromSiteInfo(String siteInfoJson) throws JSONException { + JSONObject siteInfo = new JSONObject(siteInfoJson); + JSONObject myUser = siteInfo.optJSONObject("my_user"); + if (myUser == null) { + return null; + } + JSONObject localUserView = myUser.getJSONObject("local_user_view"); + JSONObject person = localUserView.getJSONObject("person"); + + String qualifiedName = LemmyUtils.actorID2FullName(person.getString("actor_id")); + String name = person.getString("name"); + String displayName = person.has("display_name") ? person.getString("display_name") : name; + String avatar = person.optString("avatar"); + String banner = person.optString("banner"); + + return new MyUserInfo(qualifiedName, name, displayName, avatar, banner); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 299690b6..98526244 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1265,7 +1265,7 @@ I understand Instance URL The URL of you preferred Lemmy instance with or without the https:// prefix - Username + Username or email Password 2FA token (if needed) Login