diff options
author | Docile-Alligator <chineseperson5@gmail.com> | 2020-06-17 04:32:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-17 04:32:58 +0000 |
commit | dc550f1e18de72ce510dfc08c5a0815ee8d72b22 (patch) | |
tree | 192e3ed729f16930585aaed5918280339de8a077 /app/src/main/java/ml/docilealligator/infinityforreddit | |
parent | ab14405b7756c17312ed06c0b356c3245861a476 (diff) | |
parent | 46a8b0d276e5e232b742e45577c94929a1f226e2 (diff) | |
download | infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar.gz infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar.bz2 infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar.lz infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar.xz infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.tar.zst infinity-for-reddit-dc550f1e18de72ce510dfc08c5a0815ee8d72b22.zip |
Merge pull request #120 from OHermesJunior/see-removed
Feature: See removed posts and comments
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
9 files changed, 413 insertions, 195 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java new file mode 100644 index 00000000..1dc7b411 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/PushshiftAPI.java @@ -0,0 +1,13 @@ +package ml.docilealligator.infinityforreddit.API; + +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Query; + +public interface PushshiftAPI { + @GET("reddit/comment/search/") + Call<String> getRemovedComment(@Query("ids") String commentId); + + @GET("reddit/submission/search/") + Call<String> getRemovedPost(@Query("ids") String postId); +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index 51a5beaa..81e1ba8a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -60,6 +60,8 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Adapter.CommentAndPostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.FlairBottomSheetFragment; +import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.CommentData; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.CustomToroContainer; @@ -71,9 +73,9 @@ import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivit import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.FetchComment; +import ml.docilealligator.infinityforreddit.FetchRemovedComment; +import ml.docilealligator.infinityforreddit.FetchRemovedPost; import ml.docilealligator.infinityforreddit.Flair; -import ml.docilealligator.infinityforreddit.BottomSheetFragment.FlairBottomSheetFragment; -import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostCommentSortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.ParseComment; import ml.docilealligator.infinityforreddit.Post.FetchPost; @@ -161,6 +163,9 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS @Named("no_oauth") Retrofit mRetrofit; @Inject + @Named("pushshift") + Retrofit pushshiftRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -449,63 +454,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS if (mPost == null) { fetchPostAndCommentsById(getIntent().getStringExtra(EXTRA_POST_ID)); } else { - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (mPost.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (mPost.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - - MenuItem nsfwItem = mMenu.findItem(R.id.action_nsfw_view_post_detail_activity); - nsfwItem.setVisible(true); - if (mPost.isNSFW()) { - nsfwItem.setTitle(R.string.action_unmark_nsfw); - } else { - nsfwItem.setTitle(R.string.action_mark_nsfw); - } - - MenuItem spoilerItem = mMenu.findItem(R.id.action_spoiler_view_post_detail_activity); - spoilerItem.setVisible(true); - if (mPost.isSpoiler()) { - spoilerItem.setTitle(R.string.action_unmark_spoiler); - } else { - spoilerItem.setTitle(R.string.action_mark_spoiler); - } - - mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, @@ -559,6 +508,73 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS fab.setOnClickListener(view -> scrollToNextParentComment()); } + private void setupMenu() { + if (mMenu != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); + + mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); + mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); + + if (mAccessToken != null) { + if (mPost.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(mSavedIcon); + } else { + saveItem.setVisible(true); + saveItem.setIcon(mUnsavedIcon); + } + + if (mPost.isHidden()) { + hideItem.setVisible(true); + hideItem.setTitle(R.string.action_unhide_post); + } else { + hideItem.setVisible(true); + hideItem.setTitle(R.string.action_hide_post); + } + + mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); + } else { + saveItem.setVisible(false); + hideItem.setVisible(false); + } + + if (mPost.getAuthor().equals(mAccountName)) { + if (mPost.getPostType() == Post.TEXT_TYPE) { + mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); + } + mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); + + MenuItem nsfwItem = mMenu.findItem(R.id.action_nsfw_view_post_detail_activity); + nsfwItem.setVisible(true); + if (mPost.isNSFW()) { + nsfwItem.setTitle(R.string.action_unmark_nsfw); + } else { + nsfwItem.setTitle(R.string.action_mark_nsfw); + } + + MenuItem spoilerItem = mMenu.findItem(R.id.action_spoiler_view_post_detail_activity); + spoilerItem.setVisible(true); + if (mPost.isSpoiler()) { + spoilerItem.setTitle(R.string.action_unmark_spoiler); + } else { + spoilerItem.setTitle(R.string.action_mark_spoiler); + } + + mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + } + + mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); + + if ("[deleted]".equals(mPost.getAuthor()) || + "[deleted]".equals(mPost.getSelfText()) || + "[removed]".equals(mPost.getSelfText()) + ) { + mMenu.findItem(R.id.action_see_removed_view_post_detail_activity).setVisible(true); + } + } + } + private Drawable getMenuItemIcon(int drawableId) { Drawable icon = getDrawable(drawableId); if (icon != null) { @@ -602,45 +618,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS public void onParsePostSuccess(Post post) { mPost = post; - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (post.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (post.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, @@ -910,38 +888,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAdapter.updatePost(mPost); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); isRefreshing = false; - if (mMenu != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (post.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (post.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - mMenu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); - } + setupMenu(); mSwipeRefreshLayout.setRefreshing(false); } @@ -1144,6 +1091,24 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS .show(); } + public void showRemovedComment(CommentData comment, int position) { + Toast.makeText(ViewPostDetailActivity.this, R.string.fetching_removed_comment, Toast.LENGTH_SHORT).show(); + FetchRemovedComment.fetchRemovedComment( + pushshiftRetrofit, + comment, + new FetchRemovedComment.FetchRemovedCommentListener() { + @Override + public void fetchSuccess(CommentData comment) { + mAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position); + } + + @Override + public void fetchFailed() { + Toast.makeText(ViewPostDetailActivity.this, R.string.show_removed_comment_failed, Toast.LENGTH_SHORT).show(); + } + }); + } + public void changeToSingleThreadMode() { isSingleCommentThreadMode = false; mSingleCommentId = null; @@ -1257,65 +1222,30 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS applyMenuItemTheme(menu); mMenu = menu; if (mPost != null) { - MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); - MenuItem hideItem = mMenu.findItem(R.id.action_hide_view_post_detail_activity); - - mMenu.findItem(R.id.action_comment_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_sort_view_post_detail_activity).setVisible(true); - - if (mAccessToken != null) { - if (mPost.isSaved()) { - saveItem.setVisible(true); - saveItem.setIcon(mSavedIcon); - } else { - saveItem.setVisible(true); - saveItem.setIcon(mUnsavedIcon); - } - - if (mPost.isHidden()) { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_unhide_post); - } else { - hideItem.setVisible(true); - hideItem.setTitle(R.string.action_hide_post); - } - - mMenu.findItem(R.id.action_report_view_post_detail_activity).setVisible(true); - } else { - saveItem.setVisible(false); - hideItem.setVisible(false); - } - - if (mPost.getAuthor().equals(mAccountName)) { - if (mPost.getPostType() == Post.TEXT_TYPE) { - menu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - menu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - - MenuItem nsfwItem = menu.findItem(R.id.action_nsfw_view_post_detail_activity); - nsfwItem.setVisible(true); - if (mPost.isNSFW()) { - nsfwItem.setTitle(R.string.action_unmark_nsfw); - } else { - nsfwItem.setTitle(R.string.action_mark_nsfw); - } - - MenuItem spoilerItem = menu.findItem(R.id.action_spoiler_view_post_detail_activity); - spoilerItem.setVisible(true); - if (mPost.isSpoiler()) { - spoilerItem.setTitle(R.string.action_unmark_spoiler); - } else { - spoilerItem.setTitle(R.string.action_mark_spoiler); - } - - menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); - } - - menu.findItem(R.id.action_view_crosspost_parent_view_post_detail_activity).setVisible(mPost.getCrosspostParentId() != null); + setupMenu(); } return true; } + public void showRemovedPost() { + Toast.makeText(ViewPostDetailActivity.this, R.string.fetching_removed_post, Toast.LENGTH_SHORT).show(); + FetchRemovedPost.fetchRemovedPost( + pushshiftRetrofit, + mPost, + new FetchRemovedPost.FetchRemovedPostListener() { + @Override + public void fetchSuccess(Post post) { + mPost = post; + mAdapter.updatePost(post); + } + + @Override + public void fetchFailed() { + Toast.makeText(ViewPostDetailActivity.this, R.string.show_removed_post_failed, Toast.LENGTH_SHORT).show(); + } + }); + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { @@ -1450,12 +1380,12 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } return true; case R.id.action_edit_view_post_detail_activity: - Intent editPostItent = new Intent(this, EditPostActivity.class); - editPostItent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken); - editPostItent.putExtra(EditPostActivity.EXTRA_FULLNAME, mPost.getFullName()); - editPostItent.putExtra(EditPostActivity.EXTRA_TITLE, mPost.getTitle()); - editPostItent.putExtra(EditPostActivity.EXTRA_CONTENT, mPost.getSelfText()); - startActivityForResult(editPostItent, EDIT_POST_REQUEST_CODE); + Intent editPostIntent = new Intent(this, EditPostActivity.class); + editPostIntent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken); + editPostIntent.putExtra(EditPostActivity.EXTRA_FULLNAME, mPost.getFullName()); + editPostIntent.putExtra(EditPostActivity.EXTRA_TITLE, mPost.getTitle()); + editPostIntent.putExtra(EditPostActivity.EXTRA_CONTENT, mPost.getSelfText()); + startActivityForResult(editPostIntent, EDIT_POST_REQUEST_CODE); return true; case R.id.action_delete_view_post_detail_activity: new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) @@ -1504,6 +1434,10 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS intent.putExtra(ReportActivity.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); intent.putExtra(ReportActivity.EXTRA_THING_FULLNAME, mPost.getFullName()); startActivity(intent); + return true; + case R.id.action_see_removed_view_post_detail_activity: + showRemovedPost(); + return true; case android.R.id.home: onBackPressed(); return true; @@ -1537,7 +1471,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } else if (requestCode == EDIT_COMMENT_REQUEST_CODE) { if (data != null && resultCode == RESULT_OK) { - mAdapter.editComment(data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), + mAdapter.editComment(null, + data.getStringExtra(EditCommentActivity.EXTRA_EDITED_COMMENT_CONTENT), data.getExtras().getInt(EditCommentActivity.EXTRA_EDITED_COMMENT_POSITION)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 0510fae6..41acea18 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1384,8 +1384,11 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy } } - public void editComment(String commentContent, int position) { - mVisibleComments.get(position).setCommentMarkdown(commentContent); + public void editComment(String commentAuthor, String commentContentMarkdown, int position) { + if (commentAuthor != null) + mVisibleComments.get(position).setAuthor(commentAuthor); + + mVisibleComments.get(position).setCommentMarkdown(commentContentMarkdown); if (mIsSingleCommentThreadMode) { notifyItemChanged(position + 2); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index f4f32b20..d70a167d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -131,6 +131,16 @@ class AppModule { } @Provides + @Named("pushshift") + @Singleton + Retrofit providePushshiftRetrofit() { + return new Retrofit.Builder() + .baseUrl(APIUtils.PUSHSHIFT_API_BASE_URI) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + } + + @Provides @Singleton OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java index 364096e6..de4f5778 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java @@ -47,7 +47,10 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag TextView copyTextView; @BindView(R.id.report_view_comment_more_bottom_sheet_fragment) TextView reportTextView; + @BindView(R.id.see_removed_view_comment_more_bottom_sheet_fragment) + TextView seeRemovedTextView; private AppCompatActivity activity; + public CommentMoreBottomSheetFragment() { // Required empty public constructor } @@ -131,6 +134,20 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag dismiss(); }); + if ("[deleted]".equals(commentData.getAuthor()) || + "[deleted]".equals(commentData.getCommentRawText()) || + "[removed]".equals(commentData.getCommentRawText()) + ) { + seeRemovedTextView.setVisibility(View.VISIBLE); + + seeRemovedTextView.setOnClickListener(view -> { + dismiss(); + if (activity instanceof ViewPostDetailActivity) { + ((ViewPostDetailActivity) activity).showRemovedComment(commentData, bundle.getInt(EXTRA_POSITION)); + } + }); + } + return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java new file mode 100644 index 00000000..6e18e927 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedComment.java @@ -0,0 +1,97 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedComment { + + public static void fetchRemovedComment(Retrofit retrofit, CommentData comment, FetchRemovedCommentListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId()) + .enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + new ParseCommentAsyncTask(response.body(), comment, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + t.printStackTrace(); + listener.fetchFailed(); + } + }); + } + + private static CommentData parseRemovedComment(JSONObject comment, CommentData commentData) throws JSONException { + String id = comment.getString(JSONUtils.ID_KEY); + if (id.equals(commentData.getId())) { + String author = comment.getString(JSONUtils.AUTHOR_KEY); + String commentMarkdown = ""; + if (!comment.isNull(JSONUtils.BODY_KEY)) { + commentMarkdown = Utils.modifyMarkdown(comment.getString(JSONUtils.BODY_KEY).trim()); + } + + commentData.setAuthor(author); + commentData.setCommentMarkdown(commentMarkdown); + commentData.setCommentRawText(commentMarkdown); + return commentData; + } else { + return null; + } + } + + public interface FetchRemovedCommentListener { + void fetchSuccess(CommentData comment); + + void fetchFailed(); + } + + private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> { + + private String responseBody; + private FetchRemovedCommentListener listener; + CommentData comment; + + public ParseCommentAsyncTask(String responseBody, CommentData comment, FetchRemovedCommentListener listener) { + this.responseBody = responseBody; + this.comment = comment; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject commentJSON = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + comment = parseRemovedComment(commentJSON, comment); + } catch (JSONException e) { + e.printStackTrace(); + comment = null; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (comment != null) + listener.fetchSuccess(comment); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java new file mode 100644 index 00000000..2a8076a9 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRemovedPost.java @@ -0,0 +1,133 @@ +package ml.docilealligator.infinityforreddit; + +import android.net.Uri; +import android.os.AsyncTask; +import android.text.Html; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import ml.docilealligator.infinityforreddit.API.PushshiftAPI; +import ml.docilealligator.infinityforreddit.Post.Post; +import ml.docilealligator.infinityforreddit.Utils.JSONUtils; +import ml.docilealligator.infinityforreddit.Utils.Utils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class FetchRemovedPost { + + public static void fetchRemovedPost(Retrofit retrofit, Post post, FetchRemovedPostListener listener) { + retrofit.create(PushshiftAPI.class).getRemovedPost(post.getId()) + .enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { + if (response.isSuccessful()) { + new ParsePostAsyncTask(response.body(), post, listener).execute(); + } else { + listener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + t.printStackTrace(); + listener.fetchFailed(); + } + }); + } + + private static Post parseRemovedPost(JSONObject postJson, Post post) throws JSONException { + String id = postJson.getString(JSONUtils.ID_KEY); + if (id.equals(post.getId())) { + String author = postJson.getString(JSONUtils.AUTHOR_KEY); + String title = postJson.getString(JSONUtils.TITLE_KEY); + String postContent = Utils.modifyMarkdown(postJson.getString(JSONUtils.SELFTEXT_KEY).trim()); + + post.setAuthor(author); + post.setTitle(title); + post.setSelfText(postContent); + post.setSelfTextPlain(""); + post.setSelfTextPlainTrimmed(""); + + String url = postJson.optString(JSONUtils.URL_KEY); + + if (url.endsWith("gif") || url.endsWith("mp4")) { + post.setVideoUrl(url); + post.setVideoDownloadUrl(url); + } else if (post.getPostType() == Post.VIDEO_TYPE || post.getPostType() == Post.GIF_TYPE) { + JSONObject redditVideoObject = postJson.getJSONObject("secure_media").getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); + String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); + String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); + + post.setVideoUrl(videoUrl); + post.setVideoDownloadUrl(videoDownloadUrl); + } else if (post.getPostType() == Post.LINK_TYPE) { + post.setUrl(url); + } + + if (post.getPostType() == Post.VIDEO_TYPE) { + try { + Uri uri = Uri.parse(url); + String authority = uri.getAuthority(); + if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { + post.setPostType(Post.LINK_TYPE); + post.setUrl(url); + } + } catch (IllegalArgumentException ignore) { + } + } + + if (!postJson.isNull("thumbnail")) { + post.setThumbnailPreviewUrl(postJson.getString("thumbnail")); + } + + return post; + } else { + return null; + } + } + + public interface FetchRemovedPostListener { + void fetchSuccess(Post post); + + void fetchFailed(); + } + + private static class ParsePostAsyncTask extends AsyncTask<Void, Void, Void> { + + private String responseBody; + private FetchRemovedPostListener listener; + Post post; + + public ParsePostAsyncTask(String responseBody, Post post, FetchRemovedPostListener listener) { + this.responseBody = responseBody; + this.post = post; + this.listener = listener; + } + + @Override + protected Void doInBackground(Void... voids) { + try { + JSONObject postJson = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY).getJSONObject(0); + post = parseRemovedPost(postJson, post); + } catch (JSONException e) { + e.printStackTrace(); + post = null; + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + if (post != null) + listener.fetchSuccess(post); + else + listener.fetchFailed(); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index a05a97f2..5a60bbdd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -252,6 +252,11 @@ public class Post implements Parcelable { return author; } + public void setAuthor(String author) { + this.author = author; + this.authorNamePrefixed = "u/" + author; + } + public String getAuthorNamePrefixed() { return authorNamePrefixed; } @@ -320,6 +325,10 @@ public class Post implements Parcelable { return thumbnailPreviewUrl; } + public void setThumbnailPreviewUrl(String thumbnailPreviewUrl) { + this.thumbnailPreviewUrl = thumbnailPreviewUrl; + } + public String getUrl() { return url; } @@ -524,4 +533,4 @@ public class Post implements Parcelable { } return ((Post) obj).id.equals(id); } -}
\ No newline at end of file +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java index c080bdd4..7f49deee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/APIUtils.java @@ -21,6 +21,7 @@ public class APIUtils { public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/"; public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v1/gfycats/"; public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/"; + public static final String PUSHSHIFT_API_BASE_URI = "https://api.pushshift.io/"; public static final String CLIENT_ID_KEY = "client_id"; public static final String CLIENT_ID = ""; |