From 3f24f8753a7f6c2343e521e875df762530c9135f Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 26 Aug 2019 13:03:38 +1000 Subject: [PATCH] Poll for deleted group chat messages --- .../securesms/loki/LokiGroupChatPoller.kt | 55 +++++++++++++++---- .../securesms/loki/LokiMessageDatabase.kt | 7 +++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index 43265beae2..f97332807f 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -18,35 +18,51 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon private val handler = Handler() private var hasStarted = false - private val task = object : Runnable { + private val api: LokiGroupChatAPI + get() = { + val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) + val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) + val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context) + val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize() + LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase) + }() + + private val pollForNewMessagesTask = object : Runnable { override fun run() { - poll() - handler.postDelayed(this, pollInterval) + pollForNewMessages() + handler.postDelayed(this, pollForNewMessagesInterval) + } + } + + private val pollForDeletedMessagesTask = object : Runnable { + + override fun run() { + pollForDeletedMessages() + handler.postDelayed(this, pollForDeletedMessagesInterval) } } companion object { - private val pollInterval: Long = 4 * 1000 + private val pollForNewMessagesInterval: Long = 4 * 1000 + private val pollForDeletedMessagesInterval: Long = 120 * 1000 } fun startIfNeeded() { if (hasStarted) return - task.run() + pollForNewMessagesTask.run() + pollForDeletedMessagesTask.run() hasStarted = true } fun stop() { - handler.removeCallbacks(task) + handler.removeCallbacks(pollForNewMessagesTask) + handler.removeCallbacks(pollForDeletedMessagesTask) hasStarted = false } - private fun poll() { - val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) - val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) - val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context) - val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize() - LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase).getMessages(groupID).success { messages -> + private fun pollForNewMessages() { + api.getMessages(groupID).success { messages -> messages.reversed().map { message -> val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray() val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) @@ -59,4 +75,19 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon Log.d("Loki", "Failed to get messages for group chat with ID: $groupID.") } } + + private fun pollForDeletedMessages() { + api.getMessages(groupID, 0).success { messages -> + val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) + val deletedMessageIDs = messages.filter { it.isDeleted }.mapNotNull { it.serverID }.mapNotNull { lokiMessageDatabase.getMessageID(it) } + val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context) + val mmsMessageDatabase = DatabaseFactory.getMmsDatabase(context) + deletedMessageIDs.forEach { + smsMessageDatabase.deleteMessage(it) + mmsMessageDatabase.delete(it) + } + }.fail { + Log.d("Loki", "Failed to get deleted messages for group chat with ID: $groupID.") + } + } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiMessageDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiMessageDatabase.kt index d6f208ac02..0548e9e6fe 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiMessageDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiMessageDatabase.kt @@ -18,6 +18,13 @@ class LokiMessageDatabase(context: Context, helper: SQLCipherOpenHelper) : Datab @JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);" } + fun getMessageID(serverID: Long): Long? { + val database = databaseHelper.readableDatabase + return database.get(tableName, "${Companion.serverID} = ?", arrayOf( serverID.toString() )) { cursor -> + cursor.getInt(messageID) + }?.toLong() + } + override fun setServerID(messageID: Long, serverID: Long) { val database = databaseHelper.writableDatabase val contentValues = ContentValues(2)