mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-28 18:44:44 +01:00
Use Executor to fetch removed comments.
This commit is contained in:
parent
f3b0a04f3c
commit
59fadbb0e0
@ -50,7 +50,7 @@ dependencies {
|
|||||||
implementation 'androidx.biometric:biometric:1.2.0-alpha03'
|
implementation 'androidx.biometric:biometric:1.2.0-alpha03'
|
||||||
implementation 'androidx.browser:browser:1.3.0'
|
implementation 'androidx.browser:browser:1.3.0'
|
||||||
implementation 'androidx.cardview:cardview:1.0.0'
|
implementation 'androidx.cardview:cardview:1.0.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
|
||||||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||||
def lifecycleVersion = "2.3.1"
|
def lifecycleVersion = "2.3.1"
|
||||||
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
|
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
|
||||||
@ -67,7 +67,7 @@ dependencies {
|
|||||||
implementation "androidx.room:room-runtime:$roomVersion"
|
implementation "androidx.room:room-runtime:$roomVersion"
|
||||||
annotationProcessor "androidx.room:room-compiler:$roomVersion"
|
annotationProcessor "androidx.room:room-compiler:$roomVersion"
|
||||||
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01'
|
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01'
|
||||||
implementation 'androidx.work:work-runtime:2.5.0'
|
implementation 'androidx.work:work-runtime:2.7.1'
|
||||||
implementation 'com.google.android.material:material:1.5.0-alpha05'
|
implementation 'com.google.android.material:material:1.5.0-alpha05'
|
||||||
|
|
||||||
/** ExoPlayer **/
|
/** ExoPlayer **/
|
||||||
@ -94,7 +94,7 @@ dependencies {
|
|||||||
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
|
||||||
|
|
||||||
// Dependency injection
|
// Dependency injection
|
||||||
def daggerVersion = "2.34"
|
def daggerVersion = "2.38.1"
|
||||||
implementation "com.google.dagger:dagger:$daggerVersion"
|
implementation "com.google.dagger:dagger:$daggerVersion"
|
||||||
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
|
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
|
||||||
|
|
||||||
|
@ -1,74 +1,108 @@
|
|||||||
package ml.docilealligator.infinityforreddit.comment;
|
package ml.docilealligator.infinityforreddit.comment;
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
import android.os.Handler;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.apis.PushshiftAPI;
|
import ml.docilealligator.infinityforreddit.apis.PushshiftAPI;
|
||||||
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
||||||
import ml.docilealligator.infinityforreddit.utils.Utils;
|
import ml.docilealligator.infinityforreddit.utils.Utils;
|
||||||
import retrofit2.Call;
|
|
||||||
import retrofit2.Callback;
|
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public class FetchRemovedComment {
|
public class FetchRemovedComment {
|
||||||
|
|
||||||
public static void fetchRemovedComment(Retrofit retrofit, Comment comment, FetchRemovedCommentListener listener) {
|
public static void fetchRemovedComment(Executor executor, Handler handler, Retrofit retrofit, Comment comment,
|
||||||
retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId())
|
FetchRemovedCommentListener listener) {
|
||||||
.enqueue(new Callback<String>() {
|
executor.execute(() -> {
|
||||||
@Override
|
try {
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
Response<String> response = retrofit.create(PushshiftAPI.class).getRemovedComment(comment.getId()).execute();
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParseCommentAsyncTask(response.body(), comment, listener).execute();
|
Comment removedComment = parseComment(response.body(), comment);
|
||||||
|
handler.post(() -> {
|
||||||
|
if (removedComment != null) {
|
||||||
|
listener.fetchSuccess(removedComment);
|
||||||
} else {
|
} else {
|
||||||
listener.fetchFailed();
|
listener.fetchFailed();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
} else {
|
||||||
@Override
|
handler.post(listener::fetchFailed);
|
||||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
}
|
||||||
t.printStackTrace();
|
} catch (IOException e) {
|
||||||
listener.fetchFailed();
|
e.printStackTrace();
|
||||||
}
|
handler.post(listener::fetchFailed);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// At the moment of writing this code, directly fetching a removed comment from
|
// At the moment of writing this code, directly fetching a removed comment from
|
||||||
// Pushift.io API returns an Internal Server Error, so we temporarily do it this way instead.
|
// Pushift.io API returns an Internal Server Error, so we temporarily do it this way instead.
|
||||||
// If this fails to return the removed comment, we try our luck with Reveddit API
|
// If this fails to return the removed comment, we try our luck with Reveddit API
|
||||||
public static void searchRemovedComment(Retrofit retrofit, Comment comment, FetchRemovedCommentListener listener) {
|
public static void searchRemovedComment(Executor executor, Handler handler, Retrofit retrofit, Comment comment,
|
||||||
long after = (comment.getCommentTimeMillis() / 1000) - 1; // 1 second before comment creation epoch
|
FetchRemovedCommentListener listener) {
|
||||||
retrofit.create(PushshiftAPI.class).searchComments(
|
executor.execute(() -> {
|
||||||
comment.getLinkId(),
|
long after = (comment.getCommentTimeMillis() / 1000) - 1; // 1 second before comment creation epoch
|
||||||
3000,
|
try {
|
||||||
"asc",
|
Response<String> response = retrofit.create(PushshiftAPI.class).searchComments(
|
||||||
"id,author,body",
|
comment.getLinkId(),
|
||||||
after,
|
3000,
|
||||||
after + 43200, // 12 Hours later
|
"asc",
|
||||||
"*")
|
"id,author,body",
|
||||||
.enqueue(new Callback<String>() {
|
after,
|
||||||
@Override
|
after + 43200, // 12 Hours later
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
"*").execute();
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParseCommentAsyncTask(response.body(), comment, listener).execute();
|
Comment removedComment = parseComment(response.body(), comment);
|
||||||
|
handler.post(() -> {
|
||||||
|
if (removedComment != null) {
|
||||||
|
listener.fetchSuccess(removedComment);
|
||||||
} else {
|
} else {
|
||||||
listener.fetchFailed();
|
listener.fetchFailed();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
} else {
|
||||||
@Override
|
handler.post(listener::fetchFailed);
|
||||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
}
|
||||||
t.printStackTrace();
|
} catch (IOException e) {
|
||||||
listener.fetchFailed();
|
e.printStackTrace();
|
||||||
}
|
handler.post(listener::fetchFailed);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static Comment parseComment(String responseBody, Comment comment) {
|
||||||
|
try {
|
||||||
|
JSONArray commentJSONArray = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY);
|
||||||
|
JSONObject commentFound = null;
|
||||||
|
for (int i = 0; i < commentJSONArray.length(); i++) {
|
||||||
|
JSONObject commentJSON = commentJSONArray.getJSONObject(i);
|
||||||
|
if (!commentJSON.isNull("id") && commentJSON.getString("id").equals(comment.getId())) {
|
||||||
|
commentFound = commentJSON;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (commentFound == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseRemovedComment(commentFound, comment);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private static Comment parseRemovedComment(@NonNull JSONObject result, Comment comment) throws JSONException {
|
private static Comment parseRemovedComment(@NonNull JSONObject result, Comment comment) throws JSONException {
|
||||||
String id = result.getString(JSONUtils.ID_KEY);
|
String id = result.getString(JSONUtils.ID_KEY);
|
||||||
String author = result.getString(JSONUtils.AUTHOR_KEY);
|
String author = result.getString(JSONUtils.AUTHOR_KEY);
|
||||||
@ -92,47 +126,4 @@ public class FetchRemovedComment {
|
|||||||
|
|
||||||
void fetchFailed();
|
void fetchFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
||||||
|
|
||||||
private final String responseBody;
|
|
||||||
private final FetchRemovedCommentListener listener;
|
|
||||||
Comment comment;
|
|
||||||
|
|
||||||
public ParseCommentAsyncTask(String responseBody, Comment comment, FetchRemovedCommentListener listener) {
|
|
||||||
this.responseBody = responseBody;
|
|
||||||
this.comment = comment;
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... voids) {
|
|
||||||
try {
|
|
||||||
JSONArray commentJSONArray = new JSONObject(responseBody).getJSONArray(JSONUtils.DATA_KEY);
|
|
||||||
JSONObject commentFound = null;
|
|
||||||
for (int i = 0; i < commentJSONArray.length(); i++) {
|
|
||||||
JSONObject commentJSON = commentJSONArray.getJSONObject(i);
|
|
||||||
if (!commentJSON.isNull("id") && commentJSON.getString("id").equals(comment.getId())) {
|
|
||||||
commentFound = commentJSON;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert commentFound != null;
|
|
||||||
comment = parseRemovedComment(commentFound, 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,51 +1,54 @@
|
|||||||
package ml.docilealligator.infinityforreddit.comment;
|
package ml.docilealligator.infinityforreddit.comment;
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
import android.os.Handler;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.apis.RevedditAPI;
|
import ml.docilealligator.infinityforreddit.apis.RevedditAPI;
|
||||||
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||||
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
|
||||||
import ml.docilealligator.infinityforreddit.utils.Utils;
|
import ml.docilealligator.infinityforreddit.utils.Utils;
|
||||||
import retrofit2.Call;
|
|
||||||
import retrofit2.Callback;
|
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public class FetchRemovedCommentReveddit {
|
public class FetchRemovedCommentReveddit {
|
||||||
|
|
||||||
public static void fetchRemovedComment(Retrofit retrofit, Comment comment, long postCreatedUtc, int nComments, FetchRemovedCommentListener listener) {
|
public static void fetchRemovedComment(Executor executor, Handler handler, Retrofit retrofit, Comment comment,
|
||||||
String parentIdWithoutPrefix = comment.getParentId().substring(3);
|
long postCreatedUtc, int nComments, FetchRemovedCommentListener listener) {
|
||||||
String rootCommentId = parentIdWithoutPrefix.equals(comment.getLinkId()) ? comment.getId() : parentIdWithoutPrefix;
|
executor.execute(() -> {
|
||||||
retrofit.create(RevedditAPI.class).getRemovedComments(
|
String parentIdWithoutPrefix = comment.getParentId().substring(3);
|
||||||
APIUtils.getRevedditHeader(),
|
String rootCommentId = parentIdWithoutPrefix.equals(comment.getLinkId()) ? comment.getId() : parentIdWithoutPrefix;
|
||||||
comment.getLinkId(),
|
try {
|
||||||
(comment.getCommentTimeMillis() / 1000) - 1,
|
Response<String> response = retrofit.create(RevedditAPI.class).getRemovedComments(
|
||||||
rootCommentId,
|
APIUtils.getRevedditHeader(),
|
||||||
comment.getId(),
|
comment.getLinkId(),
|
||||||
nComments,
|
(comment.getCommentTimeMillis() / 1000) - 1,
|
||||||
postCreatedUtc / 1000,
|
rootCommentId,
|
||||||
true)
|
comment.getId(),
|
||||||
.enqueue(new Callback<String>() {
|
nComments,
|
||||||
@Override
|
postCreatedUtc / 1000,
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
true).execute();
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParseCommentAsyncTask(response.body(), comment, listener).execute();
|
Comment removedComment = parseRemovedComment(new JSONObject(response.body()).getJSONObject(comment.getId()), comment);
|
||||||
|
handler.post(() -> {
|
||||||
|
if (removedComment != null) {
|
||||||
|
listener.fetchSuccess(removedComment);
|
||||||
} else {
|
} else {
|
||||||
listener.fetchFailed();
|
listener.fetchFailed();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
} else {
|
||||||
@Override
|
handler.post(listener::fetchFailed);
|
||||||
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
|
}
|
||||||
t.printStackTrace();
|
} catch (IOException | JSONException e) {
|
||||||
listener.fetchFailed();
|
e.printStackTrace();
|
||||||
}
|
handler.post(listener::fetchFailed);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Comment parseRemovedComment(JSONObject result, Comment comment) throws JSONException {
|
private static Comment parseRemovedComment(JSONObject result, Comment comment) throws JSONException {
|
||||||
@ -53,10 +56,7 @@ public class FetchRemovedCommentReveddit {
|
|||||||
String author = result.getString(JSONUtils.AUTHOR_KEY);
|
String author = result.getString(JSONUtils.AUTHOR_KEY);
|
||||||
String body = Utils.modifyMarkdown(result.optString(JSONUtils.BODY_KEY).trim());
|
String body = Utils.modifyMarkdown(result.optString(JSONUtils.BODY_KEY).trim());
|
||||||
|
|
||||||
if (id.equals(comment.getId()) &&
|
if (id.equals(comment.getId()) && (!author.equals(comment.getAuthor()) || !body.equals(comment.getCommentRawText()))) {
|
||||||
(!author.equals(comment.getAuthor()) ||
|
|
||||||
!body.equals(comment.getCommentRawText()))
|
|
||||||
) {
|
|
||||||
comment.setAuthor(author);
|
comment.setAuthor(author);
|
||||||
comment.setCommentMarkdown(body);
|
comment.setCommentMarkdown(body);
|
||||||
comment.setCommentRawText(body);
|
comment.setCommentRawText(body);
|
||||||
@ -71,38 +71,4 @@ public class FetchRemovedCommentReveddit {
|
|||||||
|
|
||||||
void fetchFailed();
|
void fetchFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
||||||
|
|
||||||
private final String responseBody;
|
|
||||||
private final FetchRemovedCommentListener listener;
|
|
||||||
Comment comment;
|
|
||||||
|
|
||||||
public ParseCommentAsyncTask(String responseBody, Comment comment, FetchRemovedCommentListener listener) {
|
|
||||||
this.responseBody = responseBody;
|
|
||||||
this.comment = comment;
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... voids) {
|
|
||||||
try {
|
|
||||||
JSONObject commentJSON = new JSONObject(responseBody).getJSONObject(comment.getId());
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1722,8 +1722,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
|||||||
public void showRemovedComment(Comment comment, int position) {
|
public void showRemovedComment(Comment comment, int position) {
|
||||||
Toast.makeText(activity, R.string.fetching_removed_comment, Toast.LENGTH_SHORT).show();
|
Toast.makeText(activity, R.string.fetching_removed_comment, Toast.LENGTH_SHORT).show();
|
||||||
FetchRemovedComment.searchRemovedComment(
|
FetchRemovedComment.searchRemovedComment(
|
||||||
pushshiftRetrofit,
|
mExecutor, new Handler(), pushshiftRetrofit, comment,
|
||||||
comment,
|
|
||||||
new FetchRemovedComment.FetchRemovedCommentListener() {
|
new FetchRemovedComment.FetchRemovedCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void fetchSuccess(Comment comment) {
|
public void fetchSuccess(Comment comment) {
|
||||||
@ -1733,17 +1732,19 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
|||||||
@Override
|
@Override
|
||||||
public void fetchFailed() {
|
public void fetchFailed() {
|
||||||
// Reveddit fallback
|
// Reveddit fallback
|
||||||
FetchRemovedCommentReveddit.fetchRemovedComment(revedditRetrofit, comment, mPost.getPostTimeMillis(), mPost.getNComments(), new FetchRemovedCommentReveddit.FetchRemovedCommentListener() {
|
FetchRemovedCommentReveddit.fetchRemovedComment(mExecutor, new Handler(), revedditRetrofit,
|
||||||
@Override
|
comment, mPost.getPostTimeMillis(), mPost.getNComments(),
|
||||||
public void fetchSuccess(Comment comment) {
|
new FetchRemovedCommentReveddit.FetchRemovedCommentListener() {
|
||||||
mCommentsAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position);
|
@Override
|
||||||
}
|
public void fetchSuccess(Comment comment) {
|
||||||
|
mCommentsAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fetchFailed() {
|
public void fetchFailed() {
|
||||||
Toast.makeText(activity, R.string.show_removed_comment_failed, Toast.LENGTH_SHORT).show();
|
Toast.makeText(activity, R.string.show_removed_comment_failed, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -204,7 +204,7 @@ public final class Utils {
|
|||||||
}
|
}
|
||||||
String emote_url = s_key.getString(JSONUtils.U_KEY);
|
String emote_url = s_key.getString(JSONUtils.U_KEY);
|
||||||
|
|
||||||
markdown = markdown.replace("![img](" + emote_id + ")", "[[" + emote_type + "]](" + emote_url + ") ");
|
markdown = markdown.replace("![img](" + emote_id + ")", "[" + emote_type + "](" + emote_url + ") ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return markdown;
|
return markdown;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user