From 4837c520e384217ea43c465b0a18dfa0f68a9ac3 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Thu, 14 Nov 2019 13:14:09 +1100 Subject: [PATCH 1/3] multiple deletion frontend --- .../conversation/ConversationFragment.java | 136 ++++++++++++------ 1 file changed, 92 insertions(+), 44 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java index 87b03e5118..93d360b532 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java @@ -55,6 +55,8 @@ import android.widget.Toast; import android.widget.ViewSwitcher; import com.annimon.stream.Stream; +import com.annimon.stream.operator.LongArray; +import com.google.common.primitives.Longs; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.MessageDetailsActivity; @@ -113,6 +115,7 @@ import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.Set; import kotlin.Unit; @@ -414,7 +417,8 @@ public class ConversationFragment extends Fragment menu.findItem(R.id.menu_context_reply).setVisible(isPublicChat && selectedMessageCount == 1); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(getContext()); boolean userCanModerate = isPublicChat && LokiPublicChatAPI.Companion.isUserModerator(userHexEncodedPublicKey, publicChat.getChannel(), publicChat.getServer()); - boolean isDeleteOptionVisible = isPublicChat && selectedMessageCount == 1 && (isSentByUser || userCanModerate); +// boolean isDeleteOptionVisible = isPublicChat && selectedMessageCount == 1 && (isSentByUser || userCanModerate); + boolean isDeleteOptionVisible = isPublicChat && (isSentByUser || userCanModerate); menu.findItem(R.id.menu_context_delete_message).setVisible(isDeleteOptionVisible); } else { menu.findItem(R.id.menu_context_copy_public_key).setVisible(false); @@ -520,53 +524,97 @@ public class ConversationFragment extends Fragment { @Override protected Void doInBackground(MessageRecord... messageRecords) { + ArrayList serverIDs = new ArrayList<>(); + ArrayList ignoredMessages = new ArrayList<>(); + ArrayList failedMessages = new ArrayList<>(); + boolean isSentByUser = true; + LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); for (MessageRecord messageRecord : messageRecords) { - boolean isThreadDeleted; - - if (publicChat != null) { - final SettableFuture[] future = { new SettableFuture() }; - - LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); - boolean isSentByUser = messageRecord.isOutgoing(); - Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); - - if (publicChatAPI != null && serverID != null) { - publicChatAPI - .deleteMessage(serverID, publicChat.getChannel(), publicChat.getServer(), isSentByUser) - .success(l -> { - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; - f.set(Unit.INSTANCE); - return Unit.INSTANCE; - }).fail(e -> { - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; - f.setException(e); - return Unit.INSTANCE; - }); - } else { - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; - f.setException(new Exception("Message server ID is null.")); - } - - try { - @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture)future[0]; - f.get(); - } catch (Exception exception) { - Log.d("Loki", "Couldn't delete message due to error: " + exception.toString() + "."); - return null; - } + isSentByUser = isSentByUser && messageRecord.isOutgoing(); + Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); + if (serverID != null) { + serverIDs.add(serverID); } - - if (messageRecord.isMms()) { - isThreadDeleted = DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); - } else { - isThreadDeleted = DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); - } - - if (isThreadDeleted) { - threadId = -1; - listener.setThreadId(threadId); + else { + ignoredMessages.add(messageRecord.getId()); } } + long[] serverIDsArray = new long[serverIDs.size()]; + for (int i=0; i < serverIDs.size(); i++) { + serverIDsArray[i] = serverIDs.get(i); + } + if (publicChat != null && publicChatAPI != null) { + publicChatAPI + .deleteMessages(serverIDsArray, publicChat.getChannel(), publicChat.getServer(), isSentByUser) + .success(l -> { + List longList = Longs.asList(l); + for (MessageRecord messageRecord : messageRecords) { + Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); + if (longList.contains(serverID)) { + if (messageRecord.isMms()) { + DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); + } else { + DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); + } + } + else if (!ignoredMessages.contains(serverID)) { + failedMessages.add(messageRecord.getId()); + Log.d("Loki", "Failed to delete message: " + messageRecord.getId() + "."); + } + } + return null; + }). fail(e -> { + Log.d("Loki", "Couldn't delete message due to error: " + e.toString() + "."); + return null; + }); + } +// for (MessageRecord messageRecord : messageRecords) { +// boolean isThreadDeleted; +// +// if (publicChat != null) { +// final SettableFuture[] future = { new SettableFuture() }; +// +// LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); +// boolean isSentByUser = messageRecord.isOutgoing(); +// Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); +// +// if (publicChatAPI != null && serverID != null) { +// publicChatAPI +// .deleteMessage(serverID, publicChat.getChannel(), publicChat.getServer(), isSentByUser) +// .success(l -> { +// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; +// f.set(Unit.INSTANCE); +// return Unit.INSTANCE; +// }).fail(e -> { +// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; +// f.setException(e); +// return Unit.INSTANCE; +// }); +// } else { +// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; +// f.setException(new Exception("Message server ID is null.")); +// } +// +// try { +// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture)future[0]; +// f.get(); +// } catch (Exception exception) { +// Log.d("Loki", "Couldn't delete message due to error: " + exception.toString() + "."); +// return null; +// } +// } +// +// if (messageRecord.isMms()) { +// isThreadDeleted = DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); +// } else { +// isThreadDeleted = DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); +// } +// +// if (isThreadDeleted) { +// threadId = -1; +// listener.setThreadId(threadId); +// } +// } return null; } From 9ed5ebab6d9cc694546ef99bbc602ce1959b3b20 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 15 Nov 2019 09:27:14 +1100 Subject: [PATCH 2/3] Update ConversationFragement.java with the change of server. Use List instead of Array. Delete the converting codes. --- .../securesms/conversation/ConversationFragment.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java index 93d360b532..10b82b8ba2 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java @@ -539,18 +539,13 @@ public class ConversationFragment extends Fragment ignoredMessages.add(messageRecord.getId()); } } - long[] serverIDsArray = new long[serverIDs.size()]; - for (int i=0; i < serverIDs.size(); i++) { - serverIDsArray[i] = serverIDs.get(i); - } if (publicChat != null && publicChatAPI != null) { publicChatAPI - .deleteMessages(serverIDsArray, publicChat.getChannel(), publicChat.getServer(), isSentByUser) + .deleteMessages(serverIDs, publicChat.getChannel(), publicChat.getServer(), isSentByUser) .success(l -> { - List longList = Longs.asList(l); for (MessageRecord messageRecord : messageRecords) { Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); - if (longList.contains(serverID)) { + if (l.contains(serverID)) { if (messageRecord.isMms()) { DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); } else { From ed2dfdbf683f4f09d06630f65bdfe221a5dba070 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Fri, 15 Nov 2019 15:47:46 +1100 Subject: [PATCH 3/3] remove the commented out code --- .../conversation/ConversationFragment.java | 47 ------------------- 1 file changed, 47 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java index 10b82b8ba2..a5e58b4a8e 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationFragment.java @@ -563,53 +563,6 @@ public class ConversationFragment extends Fragment return null; }); } -// for (MessageRecord messageRecord : messageRecords) { -// boolean isThreadDeleted; -// -// if (publicChat != null) { -// final SettableFuture[] future = { new SettableFuture() }; -// -// LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); -// boolean isSentByUser = messageRecord.isOutgoing(); -// Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); -// -// if (publicChatAPI != null && serverID != null) { -// publicChatAPI -// .deleteMessage(serverID, publicChat.getChannel(), publicChat.getServer(), isSentByUser) -// .success(l -> { -// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; -// f.set(Unit.INSTANCE); -// return Unit.INSTANCE; -// }).fail(e -> { -// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; -// f.setException(e); -// return Unit.INSTANCE; -// }); -// } else { -// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture) future[0]; -// f.setException(new Exception("Message server ID is null.")); -// } -// -// try { -// @SuppressWarnings("unchecked") SettableFuture f = (SettableFuture)future[0]; -// f.get(); -// } catch (Exception exception) { -// Log.d("Loki", "Couldn't delete message due to error: " + exception.toString() + "."); -// return null; -// } -// } -// -// if (messageRecord.isMms()) { -// isThreadDeleted = DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId()); -// } else { -// isThreadDeleted = DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId()); -// } -// -// if (isThreadDeleted) { -// threadId = -1; -// listener.setThreadId(threadId); -// } -// } return null; }