minor refactor

This commit is contained in:
Ryan ZHAO 2021-03-11 14:45:08 +11:00
parent 42c7f440e9
commit d7124c3b7e
2 changed files with 9 additions and 9 deletions

View File

@ -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<Long> ignoredMessages = new ArrayList<>();
ArrayList<Long> 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");
}

View File

@ -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<Long>, channel: Long, server: String, isSentByUser: Boolean): Promise<List<Long>, Exception> {
return retryIfNeeded(maxRetryCount) {
val isModerationRequest = !isSentByUser
@ -337,6 +339,7 @@ object OpenGroupAPI: DotNetAPI() {
}
}
@JvmStatic
fun ban(publicKey: String, server: String): Promise<Unit,Exception> {
return retryIfNeeded(maxRetryCount) {
execute(HTTPVerb.POST, server, "/loki/v1/moderation/blacklist/@$publicKey").then {