Merge pull request #41 from RyanRory/multiple-deletion

multiple deletion in public group chat
This commit is contained in:
gmbnt 2019-11-18 15:01:17 +11:00 committed by GitHub
commit d5fbab4116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,8 @@ import android.widget.Toast;
import android.widget.ViewSwitcher; import android.widget.ViewSwitcher;
import com.annimon.stream.Stream; 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.ApplicationContext;
import org.thoughtcrime.securesms.MessageDetailsActivity; import org.thoughtcrime.securesms.MessageDetailsActivity;
@ -113,6 +115,7 @@ import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import kotlin.Unit; import kotlin.Unit;
@ -414,7 +417,8 @@ public class ConversationFragment extends Fragment
menu.findItem(R.id.menu_context_reply).setVisible(isPublicChat && selectedMessageCount == 1); menu.findItem(R.id.menu_context_reply).setVisible(isPublicChat && selectedMessageCount == 1);
String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(getContext()); String userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(getContext());
boolean userCanModerate = isPublicChat && LokiPublicChatAPI.Companion.isUserModerator(userHexEncodedPublicKey, publicChat.getChannel(), publicChat.getServer()); 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); menu.findItem(R.id.menu_context_delete_message).setVisible(isDeleteOptionVisible);
} else { } else {
menu.findItem(R.id.menu_context_copy_public_key).setVisible(false); menu.findItem(R.id.menu_context_copy_public_key).setVisible(false);
@ -520,52 +524,44 @@ public class ConversationFragment extends Fragment
{ {
@Override @Override
protected Void doInBackground(MessageRecord... messageRecords) { protected Void doInBackground(MessageRecord... messageRecords) {
ArrayList<Long> serverIDs = new ArrayList<>();
ArrayList<Long> ignoredMessages = new ArrayList<>();
ArrayList<Long> failedMessages = new ArrayList<>();
boolean isSentByUser = true;
LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI();
for (MessageRecord messageRecord : messageRecords) { for (MessageRecord messageRecord : messageRecords) {
boolean isThreadDeleted; isSentByUser = isSentByUser && messageRecord.isOutgoing();
Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id);
if (publicChat != null) { if (serverID != null) {
final SettableFuture<?>[] future = { new SettableFuture<Unit>() }; serverIDs.add(serverID);
}
LokiPublicChatAPI publicChatAPI = ApplicationContext.getInstance(getContext()).getLokiPublicChatAPI(); else {
boolean isSentByUser = messageRecord.isOutgoing(); ignoredMessages.add(messageRecord.getId());
Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id); }
}
if (publicChatAPI != null && serverID != null) { if (publicChat != null && publicChatAPI != null) {
publicChatAPI publicChatAPI
.deleteMessage(serverID, publicChat.getChannel(), publicChat.getServer(), isSentByUser) .deleteMessages(serverIDs, publicChat.getChannel(), publicChat.getServer(), isSentByUser)
.success(l -> { .success(l -> {
@SuppressWarnings("unchecked") SettableFuture<Unit> f = (SettableFuture<Unit>) future[0]; for (MessageRecord messageRecord : messageRecords) {
f.set(Unit.INSTANCE); Long serverID = DatabaseFactory.getLokiMessageDatabase(getContext()).getServerID(messageRecord.id);
return Unit.INSTANCE; if (l.contains(serverID)) {
}).fail(e -> { if (messageRecord.isMms()) {
@SuppressWarnings("unchecked") SettableFuture<Unit> f = (SettableFuture<Unit>) future[0]; DatabaseFactory.getMmsDatabase(getActivity()).delete(messageRecord.getId());
f.setException(e); } else {
return Unit.INSTANCE; DatabaseFactory.getSmsDatabase(getActivity()).deleteMessage(messageRecord.getId());
}); }
} else { }
@SuppressWarnings("unchecked") SettableFuture<Unit> f = (SettableFuture<Unit>) future[0]; else if (!ignoredMessages.contains(serverID)) {
f.setException(new Exception("Message server ID is null.")); failedMessages.add(messageRecord.getId());
Log.d("Loki", "Failed to delete message: " + messageRecord.getId() + ".");
}
} }
return null;
try { }). fail(e -> {
@SuppressWarnings("unchecked") SettableFuture<Unit> f = (SettableFuture<Unit>)future[0]; Log.d("Loki", "Couldn't delete message due to error: " + e.toString() + ".");
f.get(); return null;
} 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; return null;