Poll for deleted group chat messages

This commit is contained in:
Niels Andriesse 2019-08-26 13:03:38 +10:00
parent 42dec85884
commit 3f24f8753a
2 changed files with 50 additions and 12 deletions

View File

@ -18,35 +18,51 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
private val handler = Handler() private val handler = Handler()
private var hasStarted = false private var hasStarted = false
private val task = object : Runnable { private val api: LokiGroupChatAPI
get() = {
override fun run() {
poll()
handler.postDelayed(this, pollInterval)
}
}
companion object {
private val pollInterval: Long = 4 * 1000
}
fun startIfNeeded() {
if (hasStarted) return
task.run()
hasStarted = true
}
fun stop() {
handler.removeCallbacks(task)
hasStarted = false
}
private fun poll() {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context) val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize() val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase).getMessages(groupID).success { messages -> LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
}()
private val pollForNewMessagesTask = object : Runnable {
override fun run() {
pollForNewMessages()
handler.postDelayed(this, pollForNewMessagesInterval)
}
}
private val pollForDeletedMessagesTask = object : Runnable {
override fun run() {
pollForDeletedMessages()
handler.postDelayed(this, pollForDeletedMessagesInterval)
}
}
companion object {
private val pollForNewMessagesInterval: Long = 4 * 1000
private val pollForDeletedMessagesInterval: Long = 120 * 1000
}
fun startIfNeeded() {
if (hasStarted) return
pollForNewMessagesTask.run()
pollForDeletedMessagesTask.run()
hasStarted = true
}
fun stop() {
handler.removeCallbacks(pollForNewMessagesTask)
handler.removeCallbacks(pollForDeletedMessagesTask)
hasStarted = false
}
private fun pollForNewMessages() {
api.getMessages(groupID).success { messages ->
messages.reversed().map { message -> messages.reversed().map { message ->
val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray() val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray()
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) 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.") 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.")
}
}
} }

View File

@ -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);" @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) { override fun setServerID(messageID: Long, serverID: Long) {
val database = databaseHelper.writableDatabase val database = databaseHelper.writableDatabase
val contentValues = ContentValues(2) val contentValues = ContentValues(2)