diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index c52e7bb..8463ebc 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -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); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/MastodonWebviewConnectActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MastodonWebviewConnectActivity.java index 460669b..e5aae6c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MastodonWebviewConnectActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MastodonWebviewConnectActivity.java @@ -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; diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index af2ceec..4517192 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -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) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java index cf80879..28ee24a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/WebviewConnectActivity.java @@ -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); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index f86d2b0..2d492fc 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -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); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonAccount.java similarity index 90% rename from app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java rename to app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonAccount.java index 3b6337a..97bb4c4 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/MastodonAccount.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonAccount.java @@ -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; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonService.java similarity index 77% rename from app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java rename to app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonService.java index 72a611f..94c946c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/MastodonService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/MastodonService.java @@ -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 . */ -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 verifyCredentials(@Header("Authorization") String credentials); + @GET("search?type=statuses&resolve=true") + Call searchMessage( + @Header("Authorization") String credentials, + @Query("q") String messageURL + ); + + @POST("statuses") + Call postReply( + @Header("Authorization") String credentials, + @Query("in_reply_to_id") String inReplyToId, + @Query("status") String content, + @Query("visibility") String visibility + ); + } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Results.java b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Results.java new file mode 100644 index 0000000..3d4e85c --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Results.java @@ -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 . */ + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + + +public class Results { + + @SerializedName("accounts") + private List accounts; + @SerializedName("statuses") + private List statuses; + + public List getAccounts() { + return accounts; + } + + public void setAccounts(List accounts) { + this.accounts = accounts; + } + + public List getStatuses() { + return statuses; + } + + public void setStatuses(List statuses) { + this.statuses = statuses; + } +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/RetrofitMastodonAPI.java similarity index 63% rename from app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java rename to app/src/main/java/app/fedilab/fedilabtube/client/mastodon/RetrofitMastodonAPI.java index 322d25e..a9a8c92 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitMastodonAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/RetrofitMastodonAPI.java @@ -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 . */ 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 statusCall = mastodonService2.searchMessage(getToken(), url); + Response 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 postReplyCall = mastodonService.postReply(getToken(), status.getId(), content, null); + try { + Response 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; + } + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Status.java b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Status.java new file mode 100644 index 0000000..f34ddd4 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/client/mastodon/Status.java @@ -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 . */ + +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; + } +} diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java index 8ecbb73..9c03a8d 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java @@ -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 { @@ -247,6 +246,7 @@ public class CommentListAdapter extends RecyclerView.Adapter ((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 { - 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 ((PeertubeActivity) context).openPostComment(comment, i)); } else { holder.binding.replyButton.setVisibility(View.GONE); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index d4a3714..a13bf67 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -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"; diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/SwitchAccountHelper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/SwitchAccountHelper.java index 928c5eb..91caf5a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/SwitchAccountHelper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/SwitchAccountHelper.java @@ -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()); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java b/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java index 23fa430..c6bdfd0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java +++ b/app/src/main/java/app/fedilab/fedilabtube/sqlite/MastodonAccountDAO.java @@ -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; diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/mastodon/MastodonPostActionsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/mastodon/MastodonPostActionsVM.java new file mode 100644 index 0000000..a8b649e --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/mastodon/MastodonPostActionsVM.java @@ -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 . */ + +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 statusMutableLiveData; + + public MastodonPostActionsVM(@NonNull Application application) { + super(application); + } + + + public LiveData 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(); + } + +}