From af6c19690bf76ebac20c7e24a23d005d501771ad Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 11 Aug 2021 15:17:53 +1000 Subject: [PATCH] send unsend request & delete remotely --- .../attachments/DatabaseAttachmentProvider.kt | 1 + .../conversation/v2/ConversationActivityV2.kt | 78 +++++++++++++------ .../securesms/database/LokiMessageDatabase.kt | 5 ++ .../database/helpers/SQLCipherOpenHelper.java | 2 +- 4 files changed, 60 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt b/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt index 51835fc88e..076a1e5c01 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt @@ -175,6 +175,7 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) db.delete(messageID) } DatabaseFactory.getLokiMessageDatabase(context).deleteMessage(messageID, isSms) + DatabaseFactory.getLokiMessageDatabase(context).deleteMessageServerHash(messageID) } override fun updateMessageAsDeleted(messageID: Long) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 4a86d5edc5..e90dc830a3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -50,6 +50,7 @@ import org.session.libsession.messaging.contacts.Contact import org.session.libsession.messaging.mentions.Mention import org.session.libsession.messaging.mentions.MentionsManager import org.session.libsession.messaging.messages.control.DataExtractionNotification +import org.session.libsession.messaging.messages.control.UnsendRequest import org.session.libsession.messaging.messages.signal.OutgoingMediaMessage import org.session.libsession.messaging.messages.signal.OutgoingTextMessage import org.session.libsession.messaging.messages.visible.OpenGroupInvitation @@ -59,6 +60,7 @@ import org.session.libsession.messaging.sending_receiving.MessageSender import org.session.libsession.messaging.sending_receiving.attachments.Attachment import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview import org.session.libsession.messaging.sending_receiving.quotes.QuoteModel +import org.session.libsession.snode.SnodeAPI import org.session.libsession.utilities.Address import org.session.libsession.utilities.Address.Companion.fromSerialized import org.session.libsession.utilities.MediaTypes @@ -1114,38 +1116,64 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe stopAudioHandler.removeCallbacks(stopVoiceMessageRecordingTask) } - override fun deleteMessages(messages: Set) { - val messageCount = messages.size + private fun buildUsendRequest(message: MessageRecord): UnsendRequest? { + if (this.thread.isOpenGroupRecipient) return null + val messageDataProvider = MessagingModuleConfiguration.shared.messageDataProvider + messageDataProvider.getServerHashForMessage(message.id) ?: return null + val unsendRequest = UnsendRequest() + if (message.isOutgoing) { + unsendRequest.author = TextSecurePreferences.getLocalNumber(this) + } else { + unsendRequest.author = message.individualRecipient.address.contactIdentifier() + } + unsendRequest.timestamp = message.timestamp + + return unsendRequest + } + + private fun deleteLocally(message: MessageRecord) { + buildUsendRequest(message)?.let { unsendRequest -> + MessageSender.send(unsendRequest, thread.address) + } + MessagingModuleConfiguration.shared.messageDataProvider.deleteMessage(message.id, !message.isMms) + } + + private fun deleteForEveryone(message: MessageRecord) { + buildUsendRequest(message)?.let { unsendRequest -> + MessageSender.send(unsendRequest, thread.address) + } val messageDataProvider = MessagingModuleConfiguration.shared.messageDataProvider val messageDB = DatabaseFactory.getLokiMessageDatabase(this@ConversationActivityV2) + val openGroup = DatabaseFactory.getLokiThreadDatabase(this).getOpenGroupChat(threadID) + if (openGroup != null) { + messageDB.getServerID(message.id, !message.isMms)?.let { messageServerID -> + OpenGroupAPIV2.deleteMessage(messageServerID, openGroup.room, openGroup.server) + .success { + messageDataProvider.deleteMessage(message.id, !message.isMms) + }.failUi { error -> + Toast.makeText(this@ConversationActivityV2, "Couldn't delete message due to error: $error", Toast.LENGTH_LONG).show() + } + } + } else { + messageDataProvider.getServerHashForMessage(message.id)?.let { serverHash -> + SnodeAPI.deleteMessage(thread.address.serialize(), listOf(serverHash)) + .failUi { error -> + Toast.makeText(this@ConversationActivityV2, "Couldn't delete message due to error: $error", Toast.LENGTH_LONG).show() + } + } + messageDataProvider.deleteMessage(message.id, !message.isMms) + } + } + + override fun deleteMessages(messages: Set) { + val messageCount = messages.size val builder = AlertDialog.Builder(this) builder.setTitle(resources.getQuantityString(R.plurals.ConversationFragment_delete_selected_messages, messageCount, messageCount)) builder.setMessage(resources.getQuantityString(R.plurals.ConversationFragment_this_will_permanently_delete_all_n_selected_messages, messageCount, messageCount)) builder.setCancelable(true) - val openGroup = DatabaseFactory.getLokiThreadDatabase(this).getOpenGroupChat(threadID) builder.setPositiveButton(R.string.delete) { _, _ -> - if (openGroup != null) { - val messageServerIDs = mutableMapOf() - for (message in messages) { - val messageServerID = messageDB.getServerID(message.id, !message.isMms) ?: continue - messageServerIDs[messageServerID] = message - } - for ((messageServerID, message) in messageServerIDs) { - OpenGroupAPIV2.deleteMessage(messageServerID, openGroup.room, openGroup.server) - .success { - messageDataProvider.deleteMessage(message.id, !message.isMms) - }.failUi { error -> - Toast.makeText(this@ConversationActivityV2, "Couldn't delete message due to error: $error", Toast.LENGTH_LONG).show() - } - } - } else { - for (message in messages) { - if (message.isMms) { - DatabaseFactory.getMmsDatabase(this@ConversationActivityV2).delete(message.id) - } else { - DatabaseFactory.getSmsDatabase(this@ConversationActivityV2).deleteMessage(message.id) - } - } + for (message in messages) { + this.deleteForEveryone(message) } endActionMode() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/LokiMessageDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/LokiMessageDatabase.kt index 85b345e3f6..07513ec328 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/LokiMessageDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/LokiMessageDatabase.kt @@ -169,4 +169,9 @@ class LokiMessageDatabase(context: Context, helper: SQLCipherOpenHelper) : Datab contentValues.put(Companion.serverHash, serverHash) database.insertOrUpdate(messageHashTable, contentValues, "${Companion.messageID} = ?", arrayOf(messageID.toString())) } + + fun deleteMessageServerHash(messageID: Long) { + val database = databaseHelper.writableDatabase + database.delete(messageHashTable, "${Companion.messageID} = ?", arrayOf(messageID.toString())) + } } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index a7fa86cb5a..ccf5c9e777 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -62,7 +62,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private static final int lokiV28 = 49; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes - private static final int DATABASE_VERSION = lokiV27; + private static final int DATABASE_VERSION = lokiV28; private static final String DATABASE_NAME = "signal.db"; private final Context context;