Resolve comments from links

Now you can set the app as a the default app for lemmy URLs.
Closes #32 and #18
This commit is contained in:
Balazs Toldi 2023-07-31 14:34:21 +02:00
parent a2e22bc525
commit 1974322013
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
3 changed files with 80 additions and 13 deletions

View File

@ -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<Comment> comments, Integer parentId, ArrayList<Integer> 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;

View File

@ -165,4 +165,10 @@ public interface LemmyAPI {
@Query("q") String query,
@Query("auth") String auth
);
@GET("api/v3/comment")
Call<String> getComment(
@Query("id") int commentId,
@Query("auth") String auth
);
}

View File

@ -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<String> comment = api.getComment(commentId, accessToken);
comment.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful() && response.body() != null) {
try {
Comment c = ParseComment.parseSingleComment(new JSONObject(response.body()).getJSONObject("comment_view"));
ArrayList<Comment> comments = new ArrayList<>();
comments.add(c);
fetchCommentListener.onFetchCommentSuccess(comments, null, null);
} catch (JSONException e) {
fetchCommentListener.onFetchCommentFailed();
}
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchCommentListener.onFetchCommentFailed();
}
});
}
public interface FetchCommentListener {
void onFetchCommentSuccess(ArrayList<Comment> expandedComments, Integer parentId, ArrayList<Integer> children);