Basic commenting feature

Signed-off-by: Bazsalanszky <balazs@toldi.eu>
This commit is contained in:
Balázs Ádám Toldi 2023-07-25 14:40:01 +02:00 committed by Bazsalanszky
parent eed13f0e59
commit 0054413e89
11 changed files with 108 additions and 53 deletions

View File

@ -35,7 +35,6 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
@ -48,7 +47,6 @@ import io.noties.markwon.MarkwonPlugin;
import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.recycler.MarkwonAdapter;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import eu.toldi.infinityforlemmy.AnyAccountAccessTokenAuthenticator;
import eu.toldi.infinityforlemmy.Infinity;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
@ -68,8 +66,6 @@ import eu.toldi.infinityforlemmy.events.SwitchAccountEvent;
import eu.toldi.infinityforlemmy.markdown.MarkdownUtils;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
public class CommentActivity extends BaseActivity implements UploadImageEnabledActivity, AccountChooserBottomSheetFragment.AccountChooserListener {
@ -77,7 +73,9 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
public static final String EXTRA_COMMENT_PARENT_TITLE_KEY = "ECPTK";
public static final String EXTRA_COMMENT_PARENT_BODY_KEY = "ECPBK";
public static final String EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY = "ECPBMK";
public static final String EXTRA_PARENT_FULLNAME_KEY = "EPFK";
public static final String EXTRA_POST_ID_KEY = "EPIK";
public static final String EXTRA_COMMENT_PARENT_ID_KEY = "ECPIDK";
public static final String EXTRA_PARENT_DEPTH_KEY = "EPDK";
public static final String EXTRA_PARENT_POSITION_KEY = "EPPK";
public static final String EXTRA_IS_REPLYING_KEY = "EIRK";
@ -93,9 +91,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
@Inject
@Named("no_oauth")
RetrofitHolder mRetrofit;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
@Named("upload_media")
Retrofit mUploadMediaRetrofit;
@ -114,7 +110,9 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
private RequestManager mGlide;
private Account selectedAccount;
private String mAccessToken;
private String parentFullname;
private int postId;
private Integer parentId = null;
private int parentDepth;
private int parentPosition;
private boolean isSubmitting = false;
@ -215,11 +213,15 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
// noinspection NotifyDataSetChanged
markwonAdapter.notifyDataSetChanged();
}
parentFullname = intent.getStringExtra(EXTRA_PARENT_FULLNAME_KEY);
postId = intent.getIntExtra(EXTRA_POST_ID_KEY,0);
if(postId == 0)
finish();
parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY);
parentPosition = intent.getExtras().getInt(EXTRA_PARENT_POSITION_KEY);
if (isReplying) {
binding.commentToolbar.setTitle(getString(R.string.comment_activity_label_is_replying));
parentId = intent.getExtras().getInt(EXTRA_COMMENT_PARENT_ID_KEY);
}
setSupportActionBar(binding.commentToolbar);
@ -388,15 +390,8 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
Snackbar sendingSnackbar = Snackbar.make(binding.commentCoordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE);
sendingSnackbar.show();
Retrofit newAuthenticatorOauthRetrofit = mOauthRetrofit.newBuilder().client(new OkHttpClient.Builder().authenticator(new AnyAccountAccessTokenAuthenticator(mRetrofit.getRetrofit(), mRedditDataRoomDatabase, selectedAccount, mCurrentAccountSharedPreferences))
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
.build())
.build();
SendComment.sendComment(mExecutor, new Handler(), binding.commentCommentEditText.getText().toString(),
parentFullname, parentDepth, newAuthenticatorOauthRetrofit, selectedAccount,
postId, parentId, mRetrofit.getRetrofit(), selectedAccount,
new SendComment.SendCommentListener() {
@Override
public void sendCommentSuccess(Comment comment) {
@ -408,7 +403,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show();
Intent returnIntent = new Intent();
returnIntent.putExtra(RETURN_EXTRA_COMMENT_DATA_KEY, comment);
returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname);
returnIntent.putExtra(EXTRA_POST_ID_KEY, postId);
if (isReplying) {
returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition);
}
@ -454,10 +449,10 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
Toast.makeText(CommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit.getRetrofit(), mUploadMediaRetrofit,
mAccessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit.getRetrofit(), mUploadMediaRetrofit,
mAccessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
sendComment(mMenu == null ? null : mMenu.findItem(R.id.action_send_comment_activity));

View File

@ -780,9 +780,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
if (comment != null && comment.getDepth() == 0) {
fragment.addComment(comment);
} else {
String parentFullname = data.getStringExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY);
int parentFullname = data.getIntExtra(CommentActivity.EXTRA_POST_ID_KEY,0);
int parentPosition = data.getIntExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, -1);
if (parentFullname != null && parentPosition >= 0) {
if (parentFullname > 0) {
fragment.addChildComment(comment, parentFullname, parentPosition);
}
}

View File

@ -899,10 +899,10 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
}
}
public void addChildComment(Comment comment, String parentFullname, int parentPosition) {
if (!parentFullname.equals(mVisibleComments.get(parentPosition).getFullName())) {
public void addChildComment(Comment comment, int parentFullname, int parentPosition) {
if (parentFullname == mVisibleComments.get(parentPosition).getId()) {
for (int i = 0; i < mVisibleComments.size(); i++) {
if (parentFullname.equals(mVisibleComments.get(i).getFullName())) {
if (parentFullname == mVisibleComments.get(i).getId()) {
parentPosition = i;
break;
}
@ -1358,7 +1358,8 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, comment.getDepth() + 1);
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY, comment.getCommentMarkdown());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, comment.getCommentRawText());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, comment.getFullName());
intent.putExtra(CommentActivity.EXTRA_POST_ID_KEY, mPost.getId());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_ID_KEY, comment.getId());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true);
int parentPosition = mIsSingleCommentThreadMode ? getBindingAdapterPosition() - 1 : getBindingAdapterPosition();

View File

@ -1333,7 +1333,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
}
Intent intent = new Intent(mActivity, CommentActivity.class);
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
intent.putExtra(CommentActivity.EXTRA_POST_ID_KEY, mPost.getId());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TITLE_KEY, mPost.getTitle());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY, mPost.getSelfText());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, mPost.getSelfTextPlain());

View File

@ -3,6 +3,7 @@ package eu.toldi.infinityforlemmy.apis;
import com.google.common.util.concurrent.ListenableFuture;
import eu.toldi.infinityforlemmy.dto.AccountLoginDTO;
import eu.toldi.infinityforlemmy.dto.CommentDTO;
import eu.toldi.infinityforlemmy.dto.CommentVoteDTO;
import eu.toldi.infinityforlemmy.dto.PostVoteDTO;
import retrofit2.Call;
@ -106,4 +107,8 @@ public interface LemmyAPI {
@Query("limit") Integer limit,
@Query("auth") String auth
);
@Headers("Content-Type: application/json")
@POST("api/v3/comment")
Call<String> postComment(@Body CommentDTO params);
}

View File

@ -150,7 +150,8 @@ public class CommentMoreBottomSheetFragment extends LandscapeExpandedRoundedBott
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, comment.getDepth() + 1);
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY, comment.getCommentMarkdown());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, comment.getCommentRawText());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, comment.getFullName());
intent.putExtra(CommentActivity.EXTRA_POST_ID_KEY, comment.getPostId());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_ID_KEY, comment.getId());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true);
intent.putExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, bundle.getInt(EXTRA_POSITION));

View File

@ -4,6 +4,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import eu.toldi.infinityforlemmy.BuildConfig;
@ -59,14 +60,16 @@ public class Comment implements Parcelable {
private boolean loadMoreChildrenFailed;
private long editedTimeMillis;
private String[] path;
private List<String> path;
private int postId;
public Comment(int id, String fullName, String author, String authorQualifiedName, String linkAuthor,
public Comment(int id, int postId, String fullName, String author, String authorQualifiedName, String linkAuthor,
long commentTimeMillis, String commentMarkdown, String commentRawText,
String linkId, String communityName, String communityQualifiedName, Integer parentId, int score,
int voteType, boolean isSubmitter, String distinguished, String permalink,
int depth, boolean collapsed, boolean hasReply, boolean saved, long edited, String[] path) {
this.id = id;
this.postId = postId;
this.fullName = fullName;
this.author = author;
this.authorQualifiedName = authorQualifiedName;
@ -90,7 +93,7 @@ public class Comment implements Parcelable {
this.isExpanded = false;
this.hasExpandedBefore = false;
this.editedTimeMillis = edited;
this.path = path;
this.path = new ArrayList<>(Arrays.asList(path));
placeholderType = NOT_PLACEHOLDER;
}
@ -113,6 +116,7 @@ public class Comment implements Parcelable {
protected Comment(Parcel in) {
id = in.readInt();
postId = in.readInt();
fullName = in.readString();
author = in.readString();
authorQualifiedName = in.readString();
@ -147,7 +151,8 @@ public class Comment implements Parcelable {
placeholderType = in.readInt();
isLoadingMoreChildren = in.readByte() != 0;
loadMoreChildrenFailed = in.readByte() != 0;
in.readStringArray(path);
path = new ArrayList<>();
in.readStringList(path);
}
public int getId() {
@ -411,6 +416,7 @@ public class Comment implements Parcelable {
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeInt(postId);
parcel.writeString(fullName);
parcel.writeString(author);
parcel.writeString(authorQualifiedName);
@ -445,10 +451,10 @@ public class Comment implements Parcelable {
parcel.writeInt(placeholderType);
parcel.writeByte((byte) (isLoadingMoreChildren ? 1 : 0));
parcel.writeByte((byte) (loadMoreChildrenFailed ? 1 : 0));
parcel.writeStringArray(path);
parcel.writeStringList(path);
}
public String[] getPath() {
public List<String> getPath() {
return path;
}
@ -467,4 +473,8 @@ public class Comment implements Parcelable {
public String getCommunityQualifiedName() {
return communityQualifiedName;
}
public int getPostId() {
return postId;
}
}

View File

@ -180,11 +180,11 @@ public class ParseComment {
});
}
static void parseSentComment(Executor executor, Handler handler, String response, int depth,
static void parseSentComment(Executor executor, Handler handler, String response,
ParseSentCommentListener parseSentCommentListener) {
executor.execute(() -> {
try {
JSONObject sentCommentData = new JSONObject(response);
JSONObject sentCommentData = new JSONObject(response).getJSONObject("comment_view");
Comment comment = parseSingleComment(sentCommentData);
handler.post(() -> parseSentCommentListener.onParseSentCommentSuccess(comment));
@ -272,6 +272,7 @@ public class ParseComment {
JSONObject countsObj = jsonObject.getJSONObject("counts");
int id = commentObj.getInt("id");
int postID = postObj.getInt("id");
String fullName = creatorObj.getString("name");
String author = creatorObj.getString("name");
String authorQualifiedName = LemmyUtils.actorID2FullName(creatorObj.getString("actor_id"));
@ -293,7 +294,7 @@ public class ParseComment {
String distinguished = commentObj.getString("distinguished");
String permalink = commentObj.getString("ap_id");
String[] path = commentObj.getString("path").split(Pattern.quote("."));
;
int depth = path.length - 2;
@ -303,7 +304,7 @@ public class ParseComment {
boolean saved = jsonObject.getBoolean("saved");
long edited = 0;
Comment comment = new Comment(id, fullName, author, authorQualifiedName, linkAuthor, commentTimeMillis,
Comment comment = new Comment(id,postID, fullName, author, authorQualifiedName, linkAuthor, commentTimeMillis,
commentMarkdown, commentRawText, linkId, communityName, communityQualifiedName, parentId,
score, voteType, isSubmitter, distinguished, permalink, depth, collapsed, hasReply, saved, edited, path);
int child_count = countsObj.getInt("child_count");

View File

@ -10,7 +10,9 @@ import java.util.Map;
import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.account.Account;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.dto.CommentDTO;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import retrofit2.Call;
import retrofit2.Callback;
@ -19,21 +21,17 @@ import retrofit2.Retrofit;
public class SendComment {
public static void sendComment(Executor executor, Handler handler, String commentMarkdown,
String thingFullname, int parentDepth,
Retrofit newAuthenticatorOauthRetrofit, Account account,
Integer post_id, Integer parent_id,
Retrofit retrofit, Account account,
SendCommentListener sendCommentListener) {
Map<String, String> headers = APIUtils.getOAuthHeader(account.getAccessToken());
Map<String, String> params = new HashMap<>();
params.put(APIUtils.API_TYPE_KEY, "json");
params.put(APIUtils.RETURN_RTJSON_KEY, "true");
params.put(APIUtils.TEXT_KEY, commentMarkdown);
params.put(APIUtils.THING_ID_KEY, thingFullname);
newAuthenticatorOauthRetrofit.create(RedditAPI.class).sendCommentOrReplyToMessage(headers, params).enqueue(new Callback<String>() {
retrofit.create(LemmyAPI.class).postComment(new CommentDTO(commentMarkdown, post_id,parent_id, null,null,account.getAccessToken())).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
ParseComment.parseSentComment(executor, handler, response.body(), parentDepth, new ParseComment.ParseSentCommentListener() {
ParseComment.parseSentComment(executor, handler, response.body(), new ParseComment.ParseSentCommentListener() {
@Override
public void onParseSentCommentSuccess(Comment comment) {
sendCommentListener.sendCommentSuccess(comment);

View File

@ -0,0 +1,44 @@
package eu.toldi.infinityforlemmy.dto;
public class CommentDTO {
private String content;
private Integer post_id;
private Integer parent_id;
private Integer language_id;
private String form_id;
private String auth;
public CommentDTO(String content, Integer post_id, Integer parent_id, Integer language_id, String form_id, String auth) {
this.content = content;
this.post_id = post_id;
this.parent_id = parent_id;
this.language_id = language_id;
this.form_id = form_id;
this.auth = auth;
}
public String getContent() {
return content;
}
public Integer getPost_id() {
return post_id;
}
public Integer getParent_id() {
return parent_id;
}
public Integer getLanguage_id() {
return language_id;
}
public String getForm_id() {
return form_id;
}
public String getAuth() {
return auth;
}
}

View File

@ -756,7 +756,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
}
public void addChildComment(Comment comment, String parentFullname, int parentPosition) {
public void addChildComment(Comment comment, int parentFullname, int parentPosition) {
if (mCommentsAdapter != null) {
mCommentsAdapter.addChildComment(comment, parentFullname, parentPosition);
}
@ -940,7 +940,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TITLE_KEY, mPost.getTitle());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_MARKDOWN_KEY, mPost.getSelfText());
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_BODY_KEY, mPost.getSelfTextPlain());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName());
intent.putExtra(CommentActivity.EXTRA_POST_ID_KEY, mPost.getId());
intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0);
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false);
startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE);
@ -1147,9 +1147,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
if (comment != null && comment.getDepth() == 0) {
addComment(comment);
} else {
String parentFullname = data.getStringExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY);
int parentFullname = data.getIntExtra(CommentActivity.EXTRA_POST_ID_KEY,0);
int parentPosition = data.getIntExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, -1);
if (parentFullname != null && parentPosition >= 0) {
if (parentFullname > 0) {
addChildComment(comment, parentFullname, parentPosition);
}
}