Working post comments from Mastodon/Pleroma

This commit is contained in:
Thomas 2021-01-02 18:09:09 +01:00
parent b298fbfa3e
commit b7855cb1a2
15 changed files with 386 additions and 19 deletions

View File

@ -41,7 +41,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import app.fedilab.fedilabtube.client.RetrofitMastodonAPI;
import app.fedilab.fedilabtube.client.mastodon.RetrofitMastodonAPI;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.Oauth;
@ -365,6 +365,7 @@ public class LoginActivity extends AppCompatActivity {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, remote_account ? software : null);
editor.putString(Helper.PREF_REMOTE_INSTANCE, remote_account ? host : null);
if (!remote_account) {
editor.putString(Helper.PREF_INSTANCE, host);
}

View File

@ -34,7 +34,7 @@ import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import app.fedilab.fedilabtube.client.RetrofitMastodonAPI;
import app.fedilab.fedilabtube.client.mastodon.RetrofitMastodonAPI;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.OauthParams;
import app.fedilab.fedilabtube.client.entities.Token;

View File

@ -157,6 +157,7 @@ import app.fedilab.fedilabtube.viewmodel.PlaylistsVM;
import app.fedilab.fedilabtube.viewmodel.PostActionsVM;
import app.fedilab.fedilabtube.viewmodel.SearchVM;
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
import app.fedilab.fedilabtube.viewmodel.mastodon.MastodonPostActionsVM;
import app.fedilab.fedilabtube.webview.CustomWebview;
import app.fedilab.fedilabtube.webview.MastalabWebChromeClient;
import app.fedilab.fedilabtube.webview.MastalabWebViewClient;
@ -2024,15 +2025,26 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
if (comment == null) {
String commentStr = binding.addCommentWrite.getText() != null ? binding.addCommentWrite.getText().toString() : "";
if (commentStr.trim().length() > 0) {
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
viewModelComment.comment(ADD_COMMENT, peertube.getId(), null, commentStr).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(ADD_COMMENT, 0, apiResponse1));
if (Helper.isLoggedIn(PeertubeActivity.this)) {
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
viewModelComment.comment(ADD_COMMENT, peertube.getId(), null, commentStr).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(ADD_COMMENT, 0, apiResponse1));
} else {//Remote account is posting a message
String url = "https://" + peertube.getChannel().getHost() + "/videos/watch/" + peertube.getUuid();
MastodonPostActionsVM viewModelCommentMastodon = new ViewModelProvider(PeertubeActivity.this).get(MastodonPostActionsVM.class);
viewModelCommentMastodon.comment(url, commentStr).observe(PeertubeActivity.this, status -> manageVIewPostActionsMastodon(ADD_COMMENT, position, status));
}
binding.addCommentWrite.setText("");
}
} else {
String commentView = binding.addCommentWrite.getText() != null ? binding.addCommentWrite.getText().toString() : "";
if (commentView.trim().length() > 0) {
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
viewModelComment.comment(REPLY, peertube.getId(), comment.getId(), commentView).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(REPLY, position, apiResponse1));
if (Helper.isLoggedIn(PeertubeActivity.this)) {
PostActionsVM viewModelComment = new ViewModelProvider(PeertubeActivity.this).get(PostActionsVM.class);
viewModelComment.comment(REPLY, peertube.getId(), comment.getId(), commentView).observe(PeertubeActivity.this, apiResponse1 -> manageVIewPostActions(REPLY, position, apiResponse1));
} else {//Remote account is posting a message
MastodonPostActionsVM viewModelCommentMastodon = new ViewModelProvider(PeertubeActivity.this).get(MastodonPostActionsVM.class);
viewModelCommentMastodon.comment(comment.getUrl(), commentView).observe(PeertubeActivity.this, status -> manageVIewPostActionsMastodon(REPLY, position, status));
}
binding.addCommentWrite.setText("");
}
}
@ -2072,6 +2084,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
binding.replyThread.startAnimation(animate);
}
public void openPostComment(Comment comment, int position) {
if (comment != null) {
binding.replyContent.setVisibility(View.VISIBLE);
@ -2122,8 +2135,10 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
binding.postComment.startAnimation(animateComment);
if (comment != null) {
binding.addCommentWrite.setText(String.format("@%s ", comment.getAccount().getAcct()));
binding.addCommentWrite.setSelection(binding.addCommentWrite.getText() != null ? binding.addCommentWrite.getText().length() : 0);
} else {
binding.addCommentWrite.setText(String.format("@%s ", peertube.getAccount().getAcct()));
}
binding.addCommentWrite.setSelection(binding.addCommentWrite.getText() != null ? binding.addCommentWrite.getText().length() : 0);
binding.send.setOnClickListener(null);
binding.send.setOnClickListener(v -> sendComment(comment, position));
@ -2158,6 +2173,25 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
binding.postComment.startAnimation(animateComment);
}
public void manageVIewPostActionsMastodon(RetrofitPeertubeAPI.ActionType statusAction, int position, app.fedilab.fedilabtube.client.mastodon.Status status) {
if (peertube.isCommentsEnabled() && statusAction == ADD_COMMENT) {
if (status != null) {
Comment comment = app.fedilab.fedilabtube.client.mastodon.Status.convertStatusToComment(status);
comments.add(0, comment);
commentListAdapter.notifyItemInserted(0);
}
} else if (peertube.isCommentsEnabled() && statusAction == REPLY) {
if (status != null) {
Comment comment = app.fedilab.fedilabtube.client.mastodon.Status.convertStatusToComment(status);
if (commentsThread == null) {
commentsThread = new ArrayList<>();
}
commentsThread.add(position + 1, comment);
commentReplyListAdapter.notifyItemInserted(position + 1);
}
}
}
@SuppressWarnings({"unused", "RedundantSuppression"})
public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, int position, APIResponse apiResponse) {

View File

@ -140,6 +140,7 @@ public class WebviewConnectActivity extends AppCompatActivity {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, null);
editor.putString(Helper.PREF_REMOTE_INSTANCE, null);
editor.putString(Helper.PREF_INSTANCE, instance);
editor.apply();
updateCredential(WebviewConnectActivity.this, token.getAccess_token(), clientId, clientSecret, token.getRefresh_token(), new URL(url).getHost(), null);

View File

@ -243,6 +243,7 @@ public class RetrofitPeertubeAPI {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, tokenReply.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, null);
editor.putString(Helper.PREF_REMOTE_INSTANCE, null);
editor.apply();
SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new AccountDAO(_context, db).updateAccountToken(tokenReply);

View File

@ -1,4 +1,4 @@
package app.fedilab.fedilabtube.client.entities;
package app.fedilab.fedilabtube.client.mastodon;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
@ -21,6 +21,9 @@ import com.google.gson.annotations.SerializedName;
import java.util.Date;
import app.fedilab.fedilabtube.client.data.AccountData;
import app.fedilab.fedilabtube.client.entities.Avatar;
@SuppressWarnings({"unused", "RedundantSuppression"})
public class MastodonAccount {
@ -237,4 +240,16 @@ public class MastodonAccount {
dest.writeString(this.url);
}
}
public static AccountData.Account convertToPeertubeAccount(MastodonAccount.Account initialAccount) {
AccountData.Account account = new AccountData.Account();
Avatar avatar = new Avatar();
avatar.setPath(initialAccount.getAvatar());
account.setAvatar(avatar);
account.setDescription(initialAccount.getDescription());
account.setDisplayName(initialAccount.getDisplayName());
account.setUsername(initialAccount.getUsername());
account.setHost(initialAccount.getHost());
return account;
}
}

View File

@ -1,4 +1,4 @@
package app.fedilab.fedilabtube.client;
package app.fedilab.fedilabtube.client.mastodon;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
@ -15,7 +15,6 @@ package app.fedilab.fedilabtube.client;
* see <http://www.gnu.org/licenses>. */
import app.fedilab.fedilabtube.client.entities.MastodonAccount;
import app.fedilab.fedilabtube.client.entities.Oauth;
import app.fedilab.fedilabtube.client.entities.Token;
import retrofit2.Call;
@ -47,4 +46,18 @@ interface MastodonService {
@GET("accounts/verify_credentials")
Call<MastodonAccount.Account> verifyCredentials(@Header("Authorization") String credentials);
@GET("search?type=statuses&resolve=true")
Call<Results> searchMessage(
@Header("Authorization") String credentials,
@Query("q") String messageURL
);
@POST("statuses")
Call<Status> postReply(
@Header("Authorization") String credentials,
@Query("in_reply_to_id") String inReplyToId,
@Query("status") String content,
@Query("visibility") String visibility
);
}

View File

@ -0,0 +1,44 @@
package app.fedilab.fedilabtube.client.mastodon;
/* Copyright 2021 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Results {
@SerializedName("accounts")
private List<MastodonAccount.Account> accounts;
@SerializedName("statuses")
private List<Status> statuses;
public List<MastodonAccount.Account> getAccounts() {
return accounts;
}
public void setAccounts(List<MastodonAccount.Account> accounts) {
this.accounts = accounts;
}
public List<Status> getStatuses() {
return statuses;
}
public void setStatuses(List<Status> statuses) {
this.statuses = statuses;
}
}

View File

@ -1,4 +1,18 @@
package app.fedilab.fedilabtube.client;
package app.fedilab.fedilabtube.client.mastodon;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.app.Activity;
import android.content.Context;
@ -14,8 +28,8 @@ import java.net.URLDecoder;
import app.fedilab.fedilabtube.MainActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.MastodonAccount;
import app.fedilab.fedilabtube.client.entities.Oauth;
import app.fedilab.fedilabtube.client.entities.OauthParams;
import app.fedilab.fedilabtube.client.entities.Token;
@ -30,16 +44,27 @@ import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitMastodonAPI {
private final String finalUrl;
private final String finalUrl2;
private final Context _context;
private String instance;
private String token;
public RetrofitMastodonAPI(Context context) {
_context = context;
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
this.instance = sharedpreferences.getString(Helper.PREF_REMOTE_INSTANCE, null);
this.token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
finalUrl = "https://" + this.instance + "/api/v1/";
finalUrl2 = "https://" + this.instance + "/api/v2/";
}
public RetrofitMastodonAPI(Context context, String instance, String token) {
_context = context;
this.instance = instance;
this.token = token;
finalUrl = "https://" + instance + "/api/v1/";
finalUrl2 = "https://" + this.instance + "/api/v2/";
}
public void updateCredential(Activity activity, String client_id, String client_secret, String refresh_token, String software) {
@ -72,6 +97,7 @@ public class RetrofitMastodonAPI {
if (token != null) {
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
}
editor.putString(Helper.PREF_REMOTE_INSTANCE, account.getHost());
editor.putString(Helper.PREF_SOFTWARE, software);
editor.apply();
if (userExists) {
@ -97,8 +123,21 @@ public class RetrofitMastodonAPI {
.baseUrl(finalUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (token == null) {
token = Helper.getToken(_context);
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
}
return retrofit.create(MastodonService.class);
}
private MastodonService init2() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(finalUrl2)
.addConverterFactory(GsonConverterFactory.create())
.build();
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if (token == null) {
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
}
return retrofit.create(MastodonService.class);
}
@ -189,4 +228,53 @@ public class RetrofitMastodonAPI {
}
return null;
}
public Status commentAction(String url, String content) throws Error {
MastodonService mastodonService = init();
MastodonService mastodonService2 = init2();
Call<Results> statusCall = mastodonService2.searchMessage(getToken(), url);
Response<Results> response;
try {
response = statusCall.execute();
if (response.isSuccessful() && response.body() != null && response.body().getStatuses() != null && response.body().getStatuses().size() > 0) {
Status status = response.body().getStatuses().get(0);
if (status != null) {
Call<Status> postReplyCall = mastodonService.postReply(getToken(), status.getId(), content, null);
try {
Response<Status> responsePost = postReplyCall.execute();
if (responsePost.isSuccessful()) {
Status statusReturned = responsePost.body();
if (statusReturned != null && statusReturned.getAccount() != null) {
statusReturned.getAccount().setHost(instance);
}
return statusReturned;
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Error error = new Error();
error.setStatusCode(response.code());
if (response.errorBody() != null) {
error.setError(response.errorBody().string());
} else {
error.setError(_context.getString(R.string.toast_error));
}
throw error;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String getToken() {
if (token != null) {
return "Bearer " + token;
} else {
return null;
}
}
}

View File

@ -0,0 +1,96 @@
package app.fedilab.fedilabtube.client.mastodon;
/* Copyright 2021 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import com.google.gson.annotations.SerializedName;
import org.w3c.dom.Comment;
import java.util.Date;
import app.fedilab.fedilabtube.client.data.CommentData;
public class Status {
@SerializedName("id")
private String id;
@SerializedName("in_reply_to_id")
private String inReplyToCommentId;
@SerializedName("account")
private MastodonAccount.Account account;
@SerializedName("url")
private String url;
@SerializedName("content")
private String text;
@SerializedName("created_at")
private Date createdAt;
public static CommentData.Comment convertStatusToComment(Status status) {
CommentData.Comment comment = new CommentData.Comment();
comment.setAccount(MastodonAccount.convertToPeertubeAccount(status.getAccount()));
comment.setCreatedAt(status.getCreatedAt());
comment.setText(status.getText());
return comment;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInReplyToCommentId() {
return inReplyToCommentId;
}
public void setInReplyToCommentId(String inReplyToCommentId) {
this.inReplyToCommentId = inReplyToCommentId;
}
public MastodonAccount.Account getAccount() {
return account;
}
public void setAccount(MastodonAccount.Account account) {
this.account = account;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
}

View File

@ -66,7 +66,6 @@ import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.MUTE;
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY;
import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn;
public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@ -247,6 +246,7 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
if (context instanceof PeertubeActivity && !isThread) {
holder.binding.mainContainer.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
holder.binding.commentContent.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
holder.binding.replyButton.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
}
if (comment.getTotalReplies() > 0) {
holder.binding.numberOfReplies.setVisibility(View.VISIBLE);
@ -275,7 +275,11 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
String avatarUrl;
if (instance != null) {
avatarUrl = comment.getAccount().getAvatar() != null ? "https://" + instance + comment.getAccount().getAvatar().getPath() : null;
if (comment.getAccount().getAvatar() != null && comment.getAccount().getAvatar().getPath().startsWith("http")) {
avatarUrl = comment.getAccount().getAvatar().getPath();
} else {
avatarUrl = comment.getAccount().getAvatar() != null ? "https://" + instance + comment.getAccount().getAvatar().getPath() : null;
}
} else {
avatarUrl = comment.getAccount().getAvatar() != null ? comment.getAccount().getAvatar().getPath() : null;
}
@ -300,7 +304,7 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
holder.binding.postReplyButton.setVisibility(View.GONE);
}
holder.binding.postReplyButton.setOnClickListener(v -> {
if (isLoggedIn(context) && !sepiaSearch) {
if (Helper.canMakeAction(context) && !sepiaSearch) {
((PeertubeActivity) context).openPostComment(comment, i);
} else {
if (sepiaSearch) {
@ -311,9 +315,8 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
}
});
if (isLoggedIn(context) && !sepiaSearch) {
if (Helper.canMakeAction(context) && !sepiaSearch) {
holder.binding.replyButton.setVisibility(View.VISIBLE);
holder.binding.replyButton.setOnClickListener(v -> ((PeertubeActivity) context).openPostComment(comment, i));
} else {
holder.binding.replyButton.setVisibility(View.GONE);
}

View File

@ -118,6 +118,7 @@ public class Helper {
public static final String REDIRECT_CONTENT = "urn:ietf:wg:oauth:2.0:oob";
public static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
public static final String PREF_SOFTWARE = "pref_software";
public static final String PREF_REMOTE_INSTANCE = "pref_remote_instance";
public static final Pattern redirectPattern = Pattern.compile("externalAuthToken=(\\w+)&username=([\\w.-]+)");
public static final String SET_VIDEO_CACHE = "set_video_cache";
public static final String RECEIVE_CAST_SETTINGS = "receive_cast_settings";

View File

@ -57,6 +57,7 @@ public class SwitchAccountHelper {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, account.getToken());
editor.putString(Helper.PREF_SOFTWARE, remote_account ? account.getSoftware() : null);
editor.putString(Helper.PREF_REMOTE_INSTANCE, remote_account ? account.getHost() : null);
if (!remote_account) {
editor.putString(Helper.PREF_INSTANCE, account.getHost());
}

View File

@ -21,7 +21,8 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.Date;
import app.fedilab.fedilabtube.client.entities.MastodonAccount.Account;
import app.fedilab.fedilabtube.client.mastodon.MastodonAccount.Account;
import app.fedilab.fedilabtube.client.entities.Token;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.helper.HelperInstance;

View File

@ -0,0 +1,68 @@
package app.fedilab.fedilabtube.viewmodel.mastodon;
/* Copyright 2021 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.mastodon.RetrofitMastodonAPI;
import app.fedilab.fedilabtube.client.mastodon.Status;
public class MastodonPostActionsVM extends AndroidViewModel {
private MutableLiveData<Status> statusMutableLiveData;
public MastodonPostActionsVM(@NonNull Application application) {
super(application);
}
public LiveData<Status> comment(String url, String content) {
statusMutableLiveData = new MutableLiveData<>();
postComment(url, content);
return statusMutableLiveData;
}
private void postComment(String videoURL, String content) {
Context _mContext = getApplication().getApplicationContext();
new Thread(() -> {
try {
RetrofitMastodonAPI mastodonAPI = new RetrofitMastodonAPI(_mContext);
Status status = null;
try {
status = mastodonAPI.commentAction(videoURL, content);
} catch (Error error) {
error.printStackTrace();
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Status finalStatus = status;
Runnable myRunnable = () -> statusMutableLiveData.setValue(finalStatus);
mainHandler.post(myRunnable);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}