From d7124c3b7e44f119e34072a1e0ce85dfa06f230a Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 11 Mar 2021 14:45:08 +1100 Subject: [PATCH] minor refactor --- .../conversation/ConversationFragment.java | 15 ++++++--------- .../messaging/opengroups/OpenGroupAPI.kt | 3 +++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationFragment.java b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationFragment.java index 8d1df38d23..54a1a57c8c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationFragment.java @@ -59,6 +59,8 @@ import com.annimon.stream.Stream; import org.session.libsession.messaging.messages.visible.Quote; import org.session.libsession.messaging.messages.visible.VisibleMessage; +import org.session.libsession.messaging.opengroups.OpenGroup; +import org.session.libsession.messaging.opengroups.OpenGroupAPI; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.MessageDetailsActivity; import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity; @@ -93,7 +95,6 @@ import org.thoughtcrime.securesms.util.StickyHeaderDecoration; import org.session.libsession.utilities.task.ProgressDialogAsyncTask; import org.session.libsignal.libsignal.util.guava.Optional; import org.session.libsignal.service.loki.api.opengroups.PublicChat; -import org.session.libsignal.service.loki.api.opengroups.PublicChatAPI; import org.session.libsession.utilities.TextSecurePreferences; import org.session.libsession.utilities.Util; @@ -407,7 +408,7 @@ public class ConversationFragment extends Fragment menu.findItem(R.id.menu_context_copy_public_key).setVisible(selectedMessageCount == 1 && !areAllSentByUser); menu.findItem(R.id.menu_context_reply).setVisible(selectedMessageCount == 1); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(getContext()); - boolean userCanModerate = isPublicChat && PublicChatAPI.Companion.isUserModerator(userHexEncodedPublicKey, publicChat.getChannel(), publicChat.getServer()); + boolean userCanModerate = isPublicChat && OpenGroupAPI.isUserModerator(userHexEncodedPublicKey, publicChat.getChannel(), publicChat.getServer()); boolean isDeleteOptionVisible = !isPublicChat || (areAllSentByUser || userCanModerate); // allow banning if moderating a public chat and only one user's messages are selected boolean isBanOptionVisible = isPublicChat && userCanModerate && !areAllSentByUser && uniqueUserSet.size() == 1; @@ -523,7 +524,6 @@ public class ConversationFragment extends Fragment ArrayList ignoredMessages = new ArrayList<>(); ArrayList failedMessages = new ArrayList<>(); boolean isSentByUser = true; - PublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getPublicChatAPI(); for (MessageRecord messageRecord : messageRecords) { isSentByUser = isSentByUser && messageRecord.isOutgoing(); Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); @@ -533,8 +533,8 @@ public class ConversationFragment extends Fragment ignoredMessages.add(messageRecord.getId()); } } - if (publicChat != null && publicChatAPI != null) { - publicChatAPI + if (publicChat != null) { + OpenGroupAPI .deleteMessages(serverIDs, publicChat.getChannel(), publicChat.getServer(), isSentByUser) .success(l -> { for (MessageRecord messageRecord : messageRecords) { @@ -604,9 +604,7 @@ public class ConversationFragment extends Fragment protected Void doInBackground(String... userPublicKeyParam) { String userPublicKey = userPublicKeyParam[0]; if (publicChat != null) { - PublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getPublicChatAPI(); - if (publicChat != null && publicChatAPI != null) { - publicChatAPI + OpenGroupAPI .ban(userPublicKey, publicChat.getServer()) .success(l -> { Log.d("Loki", "User banned"); @@ -615,7 +613,6 @@ public class ConversationFragment extends Fragment Log.d("Loki", "Couldn't ban user due to error: " + e.toString() + "."); return null; }); - } } else { Log.d("Loki", "Tried to ban user from a non-public chat"); } diff --git a/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPI.kt b/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPI.kt index 30c5b339d8..c8c53dc6bd 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPI.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/opengroups/OpenGroupAPI.kt @@ -41,6 +41,7 @@ object OpenGroupAPI: DotNetAPI() { return listOf() // Don't auto-join any open groups right now } + @JvmStatic fun isUserModerator(hexEncodedPublicKey: String, channel: Long, server: String): Boolean { if (moderators[server] != null && moderators[server]!![channel] != null) { return moderators[server]!![channel]!!.contains(hexEncodedPublicKey) @@ -237,6 +238,7 @@ object OpenGroupAPI: DotNetAPI() { } } + @JvmStatic fun deleteMessages(messageServerIDs: List, channel: Long, server: String, isSentByUser: Boolean): Promise, Exception> { return retryIfNeeded(maxRetryCount) { val isModerationRequest = !isSentByUser @@ -337,6 +339,7 @@ object OpenGroupAPI: DotNetAPI() { } } + @JvmStatic fun ban(publicKey: String, server: String): Promise { return retryIfNeeded(maxRetryCount) { execute(HTTPVerb.POST, server, "/loki/v1/moderation/blacklist/@$publicKey").then {