2021-07-09 05:25:57 +02:00
|
|
|
package org.thoughtcrime.securesms.database
|
2021-01-22 03:30:00 +01:00
|
|
|
|
2021-01-28 05:24:27 +01:00
|
|
|
import android.content.ContentValues
|
|
|
|
import android.content.Context
|
2023-01-05 06:56:52 +01:00
|
|
|
import android.database.Cursor
|
2021-12-15 07:11:55 +01:00
|
|
|
import org.session.libsession.messaging.jobs.AttachmentUploadJob
|
Performance improvements and bug fixes (#869)
* refactor: fail on testSnode instead of recursively using up snode list. add call timeout on http client
* refactor: refactoring batch message receives and pollers
* refactor: reduce thread utils pool count to a 2 thread fixed pool. Do a check against pubkey instead of room names for oxenHostedOpenGroup
* refactor: caching lib with potential loader fixes and no-cache for giphy
* refactor: remove store and instead use ConcurrentHashMap with a backing update coroutine
* refactor: queue trim thread jobs instead of add every message processed
* fix: wrapping auth token and initial sync for open groups in a threadutils queued runnable, getting initial sync times down
* fix: fixing the user contacts cache in ConversationAdapter.kt
* refactor: improve polling and initial sync, move group joins from config messages into a background job fetching image.
* refactor: improving the job queuing for open groups, replacing placeholder avatar generation with a custom glide loader and archiving initial sync of open groups
* feat: add OpenGroupDeleteJob.kt
* feat: add open group delete job to process deletions after batch adding
* feat: add vacuum and fix job queue re-adding jobs forever, only try to set message hash values in DB if they have changed
* refactor: remove redundant inflation for profile image views throughout app
* refactor(wip): reducing layout inflation and starting to refactor the open group deletion issues taking a long time
* refactor(wip): refactoring group deletion to not iterate through and delete messages individually
* refactor(wip): refactoring group deletion to not iterate through and delete messages individually
* fix: group deletion optimisation
* build: bump build number
* build: bump build number and fix batch message receive retry logic
* fix: clear out open group deletes
* fix: update visible ConversationAdapter.kt binding for initial contact fetching and better traces for debugging background jobs
* fix: add in check for / force sync latest encryption key pair from linked devices if we already have that closed group
* Rename .java to .kt
* refactor: change MmsDatabase to kotlin to make list operations easier
* fix: nullable type
* fix: compilation issues and constants in .kt instead of .java
* fix: bug fix expiration timer on closed group recipient
* feat: use the job queue properly across executors
* feat: start on open group dispatcher-specific logic, probably a queue factory based on openGroupId if that is the same across new message and deletion jobs to ensure consistent entry and removal
* refactor: removing redundant code and fixing jobqueue per opengroup
* fix: allow attachments in note to self
* fix: make the minWidth in quote view bind max of text / title and body, wrapped ?
* fix: fixing up layouts and code view layouts
* fix: remove TODO, remove timestamp binding
* feat: fix view logic, avatars and padding, downloading attachments lazily (on bind), fixing potential crash, add WindowDebouncer.kt
* fix: NPE on viewModel recipient from removed thread while tearing down the Recipient observer in ConversationActivityV2.kt
* refactor: replace conversation notification debouncer handler with handlerthread, same as conversation list debouncer
* refactor: UI for groups and poller improvements
* fix: revert some changes in poller
* feat: add header back in for message requests
* refactor: remove Trace calls, add more conditions to the HomeDiffUtil for updating more efficiently
* feat: try update the home adapter if we get a profile picture modified event
* feat: bump build numbers
* fix: try to start with list in homeViewModel if we don't have already, render quotes to be width of attachment slide view instead of fixed
* fix: set channel to be conflated instead of no buffer
* fix: set unreads based off last local user message vs incrementing unreads to be all amount
* feat: add profile update flag, update build number
* fix: link preview thumbnails download on bind
* fix: centercrop placeholder in glide request
* feat: recycle the contact selection list and profile image in unbind
* fix: try to prevent user KP crash at weird times
* fix: remove additional log, improve attachment download success rate, fix share logs dialog issue
2022-06-08 09:12:34 +02:00
|
|
|
import org.session.libsession.messaging.jobs.BackgroundGroupAddJob
|
2021-12-15 07:11:55 +01:00
|
|
|
import org.session.libsession.messaging.jobs.GroupAvatarDownloadJob
|
|
|
|
import org.session.libsession.messaging.jobs.Job
|
|
|
|
import org.session.libsession.messaging.jobs.MessageReceiveJob
|
|
|
|
import org.session.libsession.messaging.jobs.MessageSendJob
|
|
|
|
import org.session.libsession.messaging.jobs.SessionJobInstantiator
|
|
|
|
import org.session.libsession.messaging.jobs.SessionJobManagerFactories
|
2021-05-13 02:31:06 +02:00
|
|
|
import org.session.libsession.messaging.utilities.Data
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log
|
2021-01-28 05:24:27 +01:00
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer
|
|
|
|
|
|
|
|
class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
|
|
|
|
|
|
|
|
companion object {
|
2021-05-21 01:08:16 +02:00
|
|
|
const val sessionJobTable = "session_job_database"
|
2021-03-17 06:09:01 +01:00
|
|
|
const val jobID = "job_id"
|
|
|
|
const val jobType = "job_type"
|
|
|
|
const val failureCount = "failure_count"
|
|
|
|
const val serializedData = "serialized_data"
|
2021-05-12 08:17:25 +02:00
|
|
|
@JvmStatic val createSessionJobTableCommand
|
|
|
|
= "CREATE TABLE $sessionJobTable ($jobID INTEGER PRIMARY KEY, $jobType STRING, $failureCount INTEGER DEFAULT 0, $serializedData TEXT);"
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun persistJob(job: Job) {
|
|
|
|
val database = databaseHelper.writableDatabase
|
2021-03-03 05:14:45 +01:00
|
|
|
val contentValues = ContentValues(4)
|
2021-05-13 01:38:39 +02:00
|
|
|
contentValues.put(jobID, job.id!!)
|
2021-01-28 05:24:27 +01:00
|
|
|
contentValues.put(jobType, job.getFactoryKey())
|
|
|
|
contentValues.put(failureCount, job.failureCount)
|
|
|
|
contentValues.put(serializedData, SessionJobHelper.dataSerializer.serialize(job.serialize()))
|
2021-05-13 01:38:39 +02:00
|
|
|
database.insertOrUpdate(sessionJobTable, contentValues, "$jobID = ?", arrayOf( job.id!! ))
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 08:17:25 +02:00
|
|
|
fun markJobAsSucceeded(jobID: String) {
|
2021-05-12 08:48:18 +02:00
|
|
|
databaseHelper.writableDatabase.delete(sessionJobTable, "${Companion.jobID} = ?", arrayOf( jobID ))
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 01:38:39 +02:00
|
|
|
fun markJobAsFailedPermanently(jobID: String) {
|
2021-05-12 08:48:18 +02:00
|
|
|
databaseHelper.writableDatabase.delete(sessionJobTable, "${Companion.jobID} = ?", arrayOf( jobID ))
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
2023-05-04 07:30:09 +02:00
|
|
|
fun getAllJobs(type: String): Map<String, Job?> {
|
2021-01-28 05:24:27 +01:00
|
|
|
val database = databaseHelper.readableDatabase
|
2021-05-12 08:17:25 +02:00
|
|
|
return database.getAll(sessionJobTable, "$jobType = ?", arrayOf( type )) { cursor ->
|
|
|
|
val jobID = cursor.getString(jobID)
|
2021-05-07 03:48:03 +02:00
|
|
|
try {
|
2021-05-12 08:17:25 +02:00
|
|
|
jobID to jobFromCursor(cursor)
|
2021-05-07 03:48:03 +02:00
|
|
|
} catch (e: Exception) {
|
2021-05-12 08:17:25 +02:00
|
|
|
Log.e("Loki", "Error deserializing job of type: $type.", e)
|
|
|
|
jobID to null
|
2021-05-07 03:48:03 +02:00
|
|
|
}
|
|
|
|
}.toMap()
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun getAttachmentUploadJob(attachmentID: Long): AttachmentUploadJob? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
2021-05-12 08:17:25 +02:00
|
|
|
val result = mutableListOf<AttachmentUploadJob>()
|
|
|
|
database.getAll(sessionJobTable, "$jobType = ?", arrayOf( AttachmentUploadJob.KEY )) { cursor ->
|
|
|
|
val job = jobFromCursor(cursor) as AttachmentUploadJob?
|
|
|
|
if (job != null) { result.add(job) }
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
2021-03-02 07:22:56 +01:00
|
|
|
return result.firstOrNull { job -> job.attachmentID == attachmentID }
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun getMessageSendJob(messageSendJobID: String): MessageSendJob? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
2021-05-12 08:17:25 +02:00
|
|
|
return database.get(sessionJobTable, "$jobID = ? AND $jobType = ?", arrayOf( messageSendJobID, MessageSendJob.KEY )) { cursor ->
|
|
|
|
jobFromCursor(cursor) as MessageSendJob?
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 06:55:06 +02:00
|
|
|
fun getMessageReceiveJob(messageReceiveJobID: String): MessageReceiveJob? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.get(sessionJobTable, "$jobID = ? AND $jobType = ?", arrayOf( messageReceiveJobID, MessageReceiveJob.KEY )) { cursor ->
|
|
|
|
jobFromCursor(cursor) as MessageReceiveJob?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 03:42:35 +01:00
|
|
|
fun getGroupAvatarDownloadJob(server: String, room: String, imageId: String?): GroupAvatarDownloadJob? {
|
2021-12-15 07:11:55 +01:00
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.getAll(sessionJobTable, "$jobType = ?", arrayOf(GroupAvatarDownloadJob.KEY)) {
|
|
|
|
jobFromCursor(it) as GroupAvatarDownloadJob?
|
2023-02-08 03:42:35 +01:00
|
|
|
}.filterNotNull().find { it.server == server && it.room == room && (imageId == null || it.imageId == imageId) }
|
2021-12-15 07:11:55 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 05:09:40 +02:00
|
|
|
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 ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 05:24:27 +01:00
|
|
|
fun isJobCanceled(job: Job): Boolean {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
var cursor: android.database.Cursor? = null
|
|
|
|
try {
|
2021-05-13 01:38:39 +02:00
|
|
|
cursor = database.rawQuery("SELECT * FROM $sessionJobTable WHERE $jobID = ?", arrayOf( job.id!! ))
|
2021-05-12 08:48:18 +02:00
|
|
|
return cursor == null || !cursor.moveToFirst()
|
2021-01-28 05:24:27 +01:00
|
|
|
} catch (e: Exception) {
|
|
|
|
// Do nothing
|
|
|
|
} finally {
|
|
|
|
cursor?.close()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:17:25 +02:00
|
|
|
private fun jobFromCursor(cursor: Cursor): Job? {
|
2021-01-28 05:24:27 +01:00
|
|
|
val type = cursor.getString(jobType)
|
|
|
|
val data = SessionJobHelper.dataSerializer.deserialize(cursor.getString(serializedData))
|
2021-05-12 08:17:25 +02:00
|
|
|
val job = SessionJobHelper.sessionJobInstantiator.instantiate(type, data) ?: return null
|
2021-01-28 05:24:27 +01:00
|
|
|
job.id = cursor.getString(jobID)
|
|
|
|
job.failureCount = cursor.getInt(failureCount)
|
|
|
|
return job
|
|
|
|
}
|
Performance improvements and bug fixes (#869)
* refactor: fail on testSnode instead of recursively using up snode list. add call timeout on http client
* refactor: refactoring batch message receives and pollers
* refactor: reduce thread utils pool count to a 2 thread fixed pool. Do a check against pubkey instead of room names for oxenHostedOpenGroup
* refactor: caching lib with potential loader fixes and no-cache for giphy
* refactor: remove store and instead use ConcurrentHashMap with a backing update coroutine
* refactor: queue trim thread jobs instead of add every message processed
* fix: wrapping auth token and initial sync for open groups in a threadutils queued runnable, getting initial sync times down
* fix: fixing the user contacts cache in ConversationAdapter.kt
* refactor: improve polling and initial sync, move group joins from config messages into a background job fetching image.
* refactor: improving the job queuing for open groups, replacing placeholder avatar generation with a custom glide loader and archiving initial sync of open groups
* feat: add OpenGroupDeleteJob.kt
* feat: add open group delete job to process deletions after batch adding
* feat: add vacuum and fix job queue re-adding jobs forever, only try to set message hash values in DB if they have changed
* refactor: remove redundant inflation for profile image views throughout app
* refactor(wip): reducing layout inflation and starting to refactor the open group deletion issues taking a long time
* refactor(wip): refactoring group deletion to not iterate through and delete messages individually
* refactor(wip): refactoring group deletion to not iterate through and delete messages individually
* fix: group deletion optimisation
* build: bump build number
* build: bump build number and fix batch message receive retry logic
* fix: clear out open group deletes
* fix: update visible ConversationAdapter.kt binding for initial contact fetching and better traces for debugging background jobs
* fix: add in check for / force sync latest encryption key pair from linked devices if we already have that closed group
* Rename .java to .kt
* refactor: change MmsDatabase to kotlin to make list operations easier
* fix: nullable type
* fix: compilation issues and constants in .kt instead of .java
* fix: bug fix expiration timer on closed group recipient
* feat: use the job queue properly across executors
* feat: start on open group dispatcher-specific logic, probably a queue factory based on openGroupId if that is the same across new message and deletion jobs to ensure consistent entry and removal
* refactor: removing redundant code and fixing jobqueue per opengroup
* fix: allow attachments in note to self
* fix: make the minWidth in quote view bind max of text / title and body, wrapped ?
* fix: fixing up layouts and code view layouts
* fix: remove TODO, remove timestamp binding
* feat: fix view logic, avatars and padding, downloading attachments lazily (on bind), fixing potential crash, add WindowDebouncer.kt
* fix: NPE on viewModel recipient from removed thread while tearing down the Recipient observer in ConversationActivityV2.kt
* refactor: replace conversation notification debouncer handler with handlerthread, same as conversation list debouncer
* refactor: UI for groups and poller improvements
* fix: revert some changes in poller
* feat: add header back in for message requests
* refactor: remove Trace calls, add more conditions to the HomeDiffUtil for updating more efficiently
* feat: try update the home adapter if we get a profile picture modified event
* feat: bump build numbers
* fix: try to start with list in homeViewModel if we don't have already, render quotes to be width of attachment slide view instead of fixed
* fix: set channel to be conflated instead of no buffer
* fix: set unreads based off last local user message vs incrementing unreads to be all amount
* feat: add profile update flag, update build number
* fix: link preview thumbnails download on bind
* fix: centercrop placeholder in glide request
* feat: recycle the contact selection list and profile image in unbind
* fix: try to prevent user KP crash at weird times
* fix: remove additional log, improve attachment download success rate, fix share logs dialog issue
2022-06-08 09:12:34 +02:00
|
|
|
|
|
|
|
fun hasBackgroundGroupAddJob(groupJoinUrl: String): Boolean {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.getAll(sessionJobTable, "$jobType = ?", arrayOf(BackgroundGroupAddJob.KEY)) { cursor ->
|
|
|
|
jobFromCursor(cursor) as? BackgroundGroupAddJob
|
|
|
|
}.filterNotNull().any { it.joinUrl == groupJoinUrl }
|
|
|
|
}
|
2021-01-28 05:24:27 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 02:06:44 +01:00
|
|
|
object SessionJobHelper {
|
|
|
|
val dataSerializer: Data.Serializer = JsonDataSerializer()
|
|
|
|
val sessionJobInstantiator: SessionJobInstantiator = SessionJobInstantiator(SessionJobManagerFactories.getSessionJobFactories())
|
2021-01-22 03:30:00 +01:00
|
|
|
}
|