diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java index 78c82e16..56c88f0c 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java @@ -26,6 +26,8 @@ import javax.inject.Named; import eu.toldi.infinityforlemmy.Infinity; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RetrofitHolder; +import eu.toldi.infinityforlemmy.comment.Comment; +import eu.toldi.infinityforlemmy.comment.FetchComment; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.post.ObjectResolver; import eu.toldi.infinityforlemmy.post.Post; @@ -40,7 +42,7 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String POST_PATTERN = "https?:\\/\\/\\S+\\/post\\/\\d+"; private static final String POST_PATTERN_3 = "/[\\w-]+$"; - private static final String COMMENT_PATTERN = "/(r|u|U|user)/[\\w-]+/comments/\\w+/?[\\w-]+/\\w+/?"; + private static final String COMMENT_PATTERN = "https?:\\/\\/\\S+\\/comment\\/\\d+"; private static final String SUBREDDIT_PATTERN = "(?:https?://[\\w.-]+)?/c/[\\w-]+(@[\\w.-]+)?"; private static final String USER_PATTERN = "(?:https?://[\\w.-]+)?/u(sers)?/[\\w-]+(@[\\w.-]+)?"; private static final String SIDEBAR_PATTERN = "/[rR]/[\\w-]+/about/sidebar"; @@ -207,6 +209,48 @@ public class LinkResolverActivity extends AppCompatActivity { startActivity(intent); } + @Override + public void onResolveObjectFailed() { + Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); + finish(); + } + }); + } + } else if (uri.toString().matches(COMMENT_PATTERN)) { + if (mAccessToken == null) { + mRetrofit.setBaseURL(uri.getScheme() + "://" + uri.getHost() + "/"); + FetchComment.fetchSingleComment(mRetrofit.getRetrofit(), null, Integer.parseInt(segments.get(segments.size() - 1)), new FetchComment.FetchCommentListener() { + @Override + public void onFetchCommentSuccess(ArrayList comments, Integer parentId, ArrayList moreChildrenIds) { + Intent intent = new Intent(LinkResolverActivity.this, ViewPostDetailActivity.class); + Comment comment = comments.get(0); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getPostId()); + intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId()); + startActivity(intent); + } + + @Override + public void onFetchCommentFailed() { + Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); + finish(); + } + }); + } else { + ((Infinity) getApplication()).getAppComponent().inject(mObjectResolver); + mObjectResolver.resolveComment(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() { + @Override + public void onResolveObjectSuccess(Object c) { + Comment comment = (Comment) c; + Intent intent = new Intent(LinkResolverActivity.this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getPostId()); + intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); + intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId()); + startActivity(intent); + } + @Override public void onResolveObjectFailed() { Toast.makeText(LinkResolverActivity.this, R.string.could_not_resolve_link, Toast.LENGTH_SHORT).show(); @@ -238,18 +282,6 @@ public class LinkResolverActivity extends AppCompatActivity { intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); startActivity(intent); - } else if (path.matches(COMMENT_PATTERN)) { - int commentsIndex = segments.lastIndexOf("comments"); - if (commentsIndex >= 0 && commentsIndex < segments.size() - 1) { - Intent intent = new Intent(this, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); - intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1)); - intent.putExtra(ViewPostDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); - intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); - startActivity(intent); - } else { - deepLinkError(uri); - } } else if (path.matches(WIKI_PATTERN)) { String[] pathSegments = path.split("/"); String wikiPage; diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java index e72ec511..cc84cc9b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java @@ -165,4 +165,10 @@ public interface LemmyAPI { @Query("q") String query, @Query("auth") String auth ); + + @GET("api/v3/comment") + Call getComment( + @Query("id") int commentId, + @Query("auth") String auth + ); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java index d6c3d42c..b7b7253e 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/comment/FetchComment.java @@ -5,6 +5,9 @@ import android.os.Handler; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.concurrent.Executor; @@ -98,6 +101,32 @@ public class FetchComment { }); } + public static void fetchSingleComment(Retrofit retrofit, @Nullable String accessToken, int commentId, + FetchCommentListener fetchCommentListener) { + LemmyAPI api = retrofit.create(LemmyAPI.class); + Call comment = api.getComment(commentId, accessToken); + comment.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful() && response.body() != null) { + try { + Comment c = ParseComment.parseSingleComment(new JSONObject(response.body()).getJSONObject("comment_view")); + ArrayList comments = new ArrayList<>(); + comments.add(c); + fetchCommentListener.onFetchCommentSuccess(comments, null, null); + } catch (JSONException e) { + fetchCommentListener.onFetchCommentFailed(); + } + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchCommentListener.onFetchCommentFailed(); + } + }); + } + public interface FetchCommentListener { void onFetchCommentSuccess(ArrayList expandedComments, Integer parentId, ArrayList children);