1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-05-20 05:34:26 +02:00

Fix comment issue when posting

This commit is contained in:
Thomas 2020-10-14 16:06:17 +02:00
parent 2f773b2f0f
commit 2d33d7f970
6 changed files with 118 additions and 80 deletions

View File

@ -975,8 +975,10 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, APIResponse apiResponse) { public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, APIResponse apiResponse) {
if (peertube.isCommentsEnabled() && statusAction == ADD_COMMENT) { if (peertube.isCommentsEnabled() && statusAction == ADD_COMMENT) {
CommentVM commentViewModel = new ViewModelProvider(PeertubeActivity.this).get(CommentVM.class); if( apiResponse.getComments() != null && apiResponse.getComments().size() > 0 ) {
commentViewModel.getThread(sepiaSearch?peertubeInstance:null, videoUuid, max_id).observe(PeertubeActivity.this, this::manageVIewComment); comments.add(0, apiResponse.getComments().get(0));
commentListAdapter.notifyItemInserted(0);
}
} else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT) { } else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT) {
Toasty.success(PeertubeActivity.this, getString(R.string.successful_report), Toasty.LENGTH_LONG).show(); Toasty.success(PeertubeActivity.this, getString(R.string.successful_report), Toasty.LENGTH_LONG).show();
} else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_VIDEO) { } else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_VIDEO) {

View File

@ -17,7 +17,7 @@ package app.fedilab.fedilabtube.client;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.client.data.BlockData; import app.fedilab.fedilabtube.client.data.BlockData;
import app.fedilab.fedilabtube.client.data.CaptionData; import app.fedilab.fedilabtube.client.data.CaptionData;
import app.fedilab.fedilabtube.client.data.ChannelData; import app.fedilab.fedilabtube.client.data.ChannelData;
@ -332,11 +332,11 @@ public interface PeertubeService {
@FormUrlEncoded @FormUrlEncoded
@POST("videos/{id}/comment-threads") @POST("videos/{id}/comment-threads")
Call<String> postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text); Call<CommentData.CommentPosted> postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text);
@FormUrlEncoded @FormUrlEncoded
@POST("videos/{id}/comments/{commentId}") @POST("videos/{id}/comments/{commentId}")
Call<String> postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text); Call<CommentData.CommentPosted> postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text);
@DELETE("videos/{id}/comments/{commentId}") @DELETE("videos/{id}/comments/{commentId}")
Call<String> deleteComment(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId); Call<String> deleteComment(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId);

View File

@ -82,12 +82,12 @@ import retrofit2.converter.gson.GsonConverterFactory;
@SuppressWarnings({"unused", "RedundantSuppression"}) @SuppressWarnings({"unused", "RedundantSuppression"})
public class RetrofitPeertubeAPI { public class RetrofitPeertubeAPI {
private String finalUrl; private final String finalUrl;
private Context _context; private final Context _context;
private String instance; private final String instance;
private String token; private String token;
private Set<String> selection; private Set<String> selection;
private String count; private final String count;
public RetrofitPeertubeAPI(Context context) { public RetrofitPeertubeAPI(Context context) {
_context = context; _context = context;
@ -1232,18 +1232,22 @@ public class RetrofitPeertubeAPI {
APIResponse apiResponse = new APIResponse(); APIResponse apiResponse = new APIResponse();
try { try {
if (type == ActionType.ADD_COMMENT) { if (type == ActionType.ADD_COMMENT) {
Call<String> stringCall = peertubeService.postComment(getToken(), videoId, text); Call<CommentData.CommentPosted> commentPostedCall = peertubeService.postComment(getToken(), videoId, text);
Response<String> response = stringCall.execute(); Response<CommentData.CommentPosted> response = commentPostedCall.execute();
if (response.isSuccessful()) { if (response.isSuccessful() && response.body() != null) {
apiResponse.setActionReturn(response.body()); List<CommentData.Comment> comments = new ArrayList<>();
comments.add(response.body().getComment());
apiResponse.setComments(comments);
} else { } else {
setError(apiResponse, response.code(), response.errorBody()); setError(apiResponse, response.code(), response.errorBody());
} }
} else if (type == ActionType.REPLY) { } else if (type == ActionType.REPLY) {
Call<String> stringCall = peertubeService.postReply(getToken(), videoId, toCommentId, text); Call<CommentData.CommentPosted> commentPostedCall = peertubeService.postReply(getToken(), videoId, toCommentId, text);
Response<String> response = stringCall.execute(); Response<CommentData.CommentPosted> response = commentPostedCall.execute();
if (response.isSuccessful()) { if (response.isSuccessful() && response.body() != null) {
apiResponse.setActionReturn(response.body()); List<CommentData.Comment> comments = new ArrayList<>();
comments.add(response.body().getComment());
apiResponse.setComments(comments);
} else { } else {
setError(apiResponse, response.code(), response.errorBody()); setError(apiResponse, response.code(), response.errorBody());
} }

View File

@ -163,6 +163,20 @@ public class CommentData {
} }
} }
public static class CommentPosted{
@SerializedName("comment")
private Comment comment;
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
}
public static class NotificationComment { public static class NotificationComment {
@SerializedName("id") @SerializedName("id")
private String id; private String id;

View File

@ -16,6 +16,7 @@ package app.fedilab.fedilabtube.drawer;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
@ -49,7 +50,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.ShowAccountActivity;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.CommentData.Comment; import app.fedilab.fedilabtube.client.data.CommentData.Comment;
@ -66,8 +66,8 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
public AllCommentRemoved allCommentRemoved; public AllCommentRemoved allCommentRemoved;
private Context context; private Context context;
private List<Comment> comments; private final List<Comment> comments;
private CommentListAdapter commentListAdapter; private final CommentListAdapter commentListAdapter;
boolean isVideoOwner; boolean isVideoOwner;
public CommentListAdapter(List<Comment> comments, boolean isVideoOwner) { public CommentListAdapter(List<Comment> comments, boolean isVideoOwner) {
@ -131,9 +131,9 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
popup.getMenu().findItem(R.id.action_delete).setVisible(false); popup.getMenu().findItem(R.id.action_delete).setVisible(false);
} }
popup.setOnMenuItemClickListener(item -> { popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) { int itemId = item.getItemId();
case R.id.action_delete: if (itemId == R.id.action_delete) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_comment); builder.setTitle(R.string.delete_comment);
builder.setMessage(R.string.delete_comment_confirm); builder.setMessage(R.string.delete_comment_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert) builder.setIcon(android.R.drawable.ic_dialog_alert)
@ -155,11 +155,9 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
}) })
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show(); .show();
break; } else if (itemId == R.id.action_report) {
case R.id.action_report:
reportComment(comment); reportComment(comment);
break; } else if (itemId == R.id.action_mute) {
case R.id.action_mute:
PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class); PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class);
viewModel.post(MUTE, comment.getAccount().getAcct(), null).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(MUTE, apiResponse)); viewModel.post(MUTE, comment.getAccount().getAcct(), null).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(MUTE, apiResponse));
comments.remove(comment); comments.remove(comment);
@ -167,9 +165,9 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
if (comments.size() == 0) { if (comments.size() == 0) {
allCommentRemoved.onAllCommentRemoved(); allCommentRemoved.onAllCommentRemoved();
} }
break; } else if (itemId == R.id.action_remove_comments) {
case R.id.action_remove_comments: AlertDialog.Builder builder;
builder = new android.app.AlertDialog.Builder(context); builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_account_comment); builder.setTitle(R.string.delete_account_comment);
builder.setMessage(R.string.delete_account_comment_confirm); builder.setMessage(R.string.delete_account_comment_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert) builder.setIcon(android.R.drawable.ic_dialog_alert)
@ -191,7 +189,6 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
}) })
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show(); .show();
break;
} }
return true; return true;
}); });

View File

@ -253,6 +253,10 @@ public class Helper {
} }
} }
/**
* Get a default instance host name depending of the device locale
* @return peertube host String
*/
private static String getDefaultInstance() { private static String getDefaultInstance() {
String lang = Locale.getDefault().getLanguage(); String lang = Locale.getDefault().getLanguage();
if (lang.contains("-")) { if (lang.contains("-")) {
@ -331,6 +335,11 @@ public class Helper {
} }
/**
* Convert second to String formated date
* @param pTime timestamp
* @return String formatted value
*/
public static String secondsToString(int pTime) { public static String secondsToString(int pTime) {
int hour = pTime / 3600; int hour = pTime / 3600;
@ -391,6 +400,11 @@ public class Helper {
} }
/**
* Return rounded numbers depending of the value
* @param count long
* @return String rounded value to be displayed
*/
public static String withSuffix(long count) { public static String withSuffix(long count) {
if (count < 1000) return "" + count; if (count < 1000) return "" + count;
int exp = (int) (Math.log(count) / Math.log(1000)); int exp = (int) (Math.log(count) / Math.log(1000));
@ -419,7 +433,7 @@ public class Helper {
} }
public static void loadGiF(final Context context, String url, final ImageView imageView, int round) { public static void loadGiF(final Context context, String url, final ImageView imageView, int round) {
loadGif(context, null, url, imageView, 10); loadGif(context, null, url, imageView, round);
} }
@SuppressWarnings("SameParameterValue") @SuppressWarnings("SameParameterValue")
@ -458,6 +472,13 @@ public class Helper {
} }
/**
* Initialize the webview
* @param activity Current Activity
* @param webviewId int id of the webview layout
* @param rootView View the root view
* @return CustomWebview
*/
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
public static CustomWebview initializeWebview(Activity activity, int webviewId, View rootView) { public static CustomWebview initializeWebview(Activity activity, int webviewId, View rootView) {