From a8eb33f3dd8ab0bf551248db6aba287e92054658 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 10 Dec 2022 10:38:01 +0100 Subject: [PATCH] Allow to check remote conversations --- .../android/activities/ContextActivity.java | 37 ++++++++++--------- .../android/helper/CrossActionHelper.java | 5 --- .../app/fedilab/android/helper/Helper.java | 2 + .../android/ui/drawer/StatusAdapter.java | 28 +++++++------- .../main/res/layout/activity_conversation.xml | 2 + 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/ContextActivity.java b/app/src/main/java/app/fedilab/android/activities/ContextActivity.java index e486e14d..0d2ae793 100644 --- a/app/src/main/java/app/fedilab/android/activities/ContextActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ContextActivity.java @@ -24,7 +24,6 @@ import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.util.Log; import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; @@ -35,14 +34,15 @@ import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; import androidx.preference.PreferenceManager; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; -import app.fedilab.android.client.entities.api.Account; import app.fedilab.android.client.entities.api.Status; import app.fedilab.android.client.entities.app.StatusCache; import app.fedilab.android.databinding.ActivityConversationBinding; import app.fedilab.android.exception.DBException; -import app.fedilab.android.helper.CrossActionHelper; import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.ui.fragment.timeline.FragmentMastodonContext; @@ -143,6 +143,8 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon MenuItem action_remote = menu.findItem(R.id.action_remote); if (remote_instance != null) { action_remote.setVisible(false); + } else { + action_remote.setVisible(firstMessage != null && !firstMessage.visibility.equalsIgnoreCase("direct") && !firstMessage.visibility.equalsIgnoreCase("private")); } return true; } @@ -184,28 +186,28 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon Toasty.info(ContextActivity.this, getString(R.string.toast_on_your_instance), Toasty.LENGTH_SHORT).show(); return true; } - Log.v(Helper.TAG, "firstMessage.uri: " + firstMessage.uri); - Log.v(Helper.TAG, "instance: " + instance); - CrossActionHelper.fetchStatusInRemoteInstance(ContextActivity.this, firstMessage.uri, instance, new CrossActionHelper.Callback() { - @Override - public void federatedStatus(Status status) { - Log.v(Helper.TAG, ">status: " + status); + Pattern pattern = Helper.statusIdInUrl; + Matcher matcher = pattern.matcher(firstMessage.uri); + String remoteId = null; + if (matcher.find()) { + remoteId = matcher.group(1); + } + if (remoteId != null) { + StatusesVM statusesVM = new ViewModelProvider(ContextActivity.this).get(StatusesVM.class); + statusesVM.getStatus(instance, null, remoteId).observe(ContextActivity.this, status -> { if (status != null) { Intent intentContext = new Intent(ContextActivity.this, ContextActivity.class); intentContext.putExtra(Helper.ARG_STATUS, status); - intentContext.putExtra(Helper.ARG_REMOTE_INSTANCE, true); + intentContext.putExtra(Helper.ARG_REMOTE_INSTANCE, instance); intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intentContext); } else { Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show(); } - } - - @Override - public void federatedAccount(Account account) { - Log.v(Helper.TAG, ">account: " + account); - } - }); + }); + } else { + Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show(); + } } else { Toasty.warning(ContextActivity.this, getString(R.string.toast_error_fetch_message), Toasty.LENGTH_SHORT).show(); } @@ -216,5 +218,6 @@ public class ContextActivity extends BaseActivity implements FragmentMastodonCon @Override public void get(Status status) { firstMessage = status; + invalidateOptionsMenu(); } } \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java index be38d26c..c9c85dbd 100644 --- a/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/CrossActionHelper.java @@ -20,7 +20,6 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.util.Log; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; @@ -449,7 +448,6 @@ public class CrossActionHelper { new Thread(() -> { Call resultsCall = mastodonSearchService.search(null, url, null, "statuses", null, null, null, null, null, null, null); Results results = null; - Log.v(Helper.TAG, ">request: " + resultsCall.request()); if (resultsCall != null) { try { Response resultsResponse = resultsCall.execute(); @@ -461,8 +459,6 @@ public class CrossActionHelper { results.statuses = new ArrayList<>(); } } - } else { - Log.v(Helper.TAG, ">err: " + resultsResponse.errorBody().string()); } } catch (IOException e) { e.printStackTrace(); @@ -471,7 +467,6 @@ public class CrossActionHelper { Handler mainHandler = new Handler(Looper.getMainLooper()); Results finalResults = results; Runnable myRunnable = () -> { - Log.v(Helper.TAG, ">finalResults.statuses " + finalResults.statuses); if (finalResults != null && finalResults.statuses != null && finalResults.statuses.size() > 0) { callback.federatedStatus(finalResults.statuses.get(0)); } diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 8b10a874..2b1aa30a 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -343,6 +343,8 @@ public class Helper { public static final Pattern codePattern = Pattern.compile("code=([\\w-]+)"); public static final Pattern nitterIDPattern = Pattern.compile("/status/(\\d+)"); public static final Pattern emailPattern = Pattern.compile("(\\s+[\\w_.-]+@[a-zA-Z0-9][a-zA-Z0-9.-]{1,61}[a-zA-Z0-9](?:\\.[a-zA-Z]{2,})+)"); + public static final Pattern statusIdInUrl = Pattern.compile("statuses/(\\w+)"); + /*public static final Pattern urlPattern = Pattern.compile( "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))", diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index 1bb6e761..9cf4478a 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -1453,25 +1453,27 @@ public class StatusAdapter extends RecyclerView.Adapter } return; } - if (context instanceof ContextActivity) { + if (context instanceof ContextActivity && !remote) { Bundle bundle = new Bundle(); bundle.putSerializable(Helper.ARG_STATUS, statusToDeal); Fragment fragment = Helper.addFragment(((AppCompatActivity) context).getSupportFragmentManager(), R.id.nav_host_fragment_content_main, new FragmentMastodonContext(), bundle, null, FragmentMastodonContext.class.getName()); ((ContextActivity) context).setCurrentFragment((FragmentMastodonContext) fragment); } else { if (remote) { - Toasty.info(context, context.getString(R.string.retrieve_remote_status), Toasty.LENGTH_SHORT).show(); - searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.uri, null, "statuses", false, true, false, 0, null, null, 1) - .observe((LifecycleOwner) context, results -> { - if (results != null && results.statuses != null && results.statuses.size() > 0) { - Status fetchedStatus = results.statuses.get(0); - Intent intent = new Intent(context, ContextActivity.class); - intent.putExtra(Helper.ARG_STATUS, fetchedStatus); - context.startActivity(intent); - } else { - Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show(); - } - }); + if (!(context instanceof ContextActivity)) { //We are not already checking a remote conversation + Toasty.info(context, context.getString(R.string.retrieve_remote_status), Toasty.LENGTH_SHORT).show(); + searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, statusToDeal.uri, null, "statuses", false, true, false, 0, null, null, 1) + .observe((LifecycleOwner) context, results -> { + if (results != null && results.statuses != null && results.statuses.size() > 0) { + Status fetchedStatus = results.statuses.get(0); + Intent intent = new Intent(context, ContextActivity.class); + intent.putExtra(Helper.ARG_STATUS, fetchedStatus); + context.startActivity(intent); + } else { + Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show(); + } + }); + } } else { Intent intent = new Intent(context, ContextActivity.class); intent.putExtra(Helper.ARG_STATUS, statusToDeal); diff --git a/app/src/main/res/layout/activity_conversation.xml b/app/src/main/res/layout/activity_conversation.xml index 21bb55f4..ac77201d 100644 --- a/app/src/main/res/layout/activity_conversation.xml +++ b/app/src/main/res/layout/activity_conversation.xml @@ -50,6 +50,8 @@