Cancel jobs when thread is deleted

This commit is contained in:
Niels Andriesse 2021-05-14 13:09:40 +10:00
parent 446ff908ba
commit 72540aa787
2 changed files with 26 additions and 0 deletions

View File

@ -332,6 +332,8 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
lifecycleScope.launch(Dispatchers.Main) {
val context = this@HomeActivity as Context
DatabaseFactory.getSessionJobDatabase(this@HomeActivity).cancelPendingMessageSendJobs(threadID)
// Send a leave group message if this is an active closed group
if (recipient.address.isClosedGroup && DatabaseFactory.getGroupDatabase(context).isActive(recipient.address.toGroupString())) {
var isClosedGroup: Boolean

View File

@ -71,6 +71,30 @@ class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa
}
}
fun cancelPendingMessageSendJobs(threadID: Long) {
val database = databaseHelper.writableDatabase
val attachmentUploadJobKeys = mutableListOf<String>()
database.getAll(sessionJobTable, "$jobType = ?", arrayOf( AttachmentUploadJob.KEY )) { cursor ->
val job = jobFromCursor(cursor) as AttachmentUploadJob?
if (job != null && job.threadID == threadID.toString()) { attachmentUploadJobKeys.add(job.id!!) }
}
val messageSendJobKeys = mutableListOf<String>()
database.getAll(sessionJobTable, "$jobType = ?", arrayOf( MessageSendJob.KEY )) { cursor ->
val job = jobFromCursor(cursor) as MessageSendJob?
if (job != null && job.message.threadID == threadID) { messageSendJobKeys.add(job.id!!) }
}
if (attachmentUploadJobKeys.isNotEmpty()) {
val attachmentUploadJobKeysAsString = attachmentUploadJobKeys.joinToString(", ")
database.delete(sessionJobTable, "${Companion.jobType} = ? AND ${Companion.jobID} IN (?)",
arrayOf( AttachmentUploadJob.KEY, attachmentUploadJobKeysAsString ))
}
if (messageSendJobKeys.isNotEmpty()) {
val messageSendJobKeysAsString = messageSendJobKeys.joinToString(", ")
database.delete(sessionJobTable, "${Companion.jobType} = ? AND ${Companion.jobID} IN (?)",
arrayOf( MessageSendJob.KEY, messageSendJobKeysAsString ))
}
}
fun isJobCanceled(job: Job): Boolean {
val database = databaseHelper.readableDatabase
var cursor: android.database.Cursor? = null