mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-24 02:25:19 +00:00
Refactor MmsDatabase (#1169)
This commit is contained in:
parent
ffef98ecc9
commit
70aab2994b
@ -65,6 +65,7 @@ import org.thoughtcrime.securesms.database.model.Quote
|
|||||||
import org.thoughtcrime.securesms.dependencies.DatabaseComponent.Companion.get
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent.Companion.get
|
||||||
import org.thoughtcrime.securesms.mms.MmsException
|
import org.thoughtcrime.securesms.mms.MmsException
|
||||||
import org.thoughtcrime.securesms.mms.SlideDeck
|
import org.thoughtcrime.securesms.mms.SlideDeck
|
||||||
|
import org.thoughtcrime.securesms.util.asSequence
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
@ -91,53 +92,21 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addFailures(messageId: Long, failure: List<NetworkFailure>) {
|
fun isOutgoingMessage(timestamp: Long): Boolean =
|
||||||
try {
|
databaseHelper.writableDatabase.query(
|
||||||
addToDocument(messageId, NETWORK_FAILURE, failure, NetworkFailureList::class.java)
|
|
||||||
} catch (e: IOException) {
|
|
||||||
Log.w(TAG, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeFailure(messageId: Long, failure: NetworkFailure?) {
|
|
||||||
try {
|
|
||||||
removeFromDocument(messageId, NETWORK_FAILURE, failure, NetworkFailureList::class.java)
|
|
||||||
} catch (e: IOException) {
|
|
||||||
Log.w(TAG, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isOutgoingMessage(timestamp: Long): Boolean {
|
|
||||||
val database = databaseHelper.writableDatabase
|
|
||||||
var cursor: Cursor? = null
|
|
||||||
var isOutgoing = false
|
|
||||||
try {
|
|
||||||
cursor = database.query(
|
|
||||||
TABLE_NAME,
|
TABLE_NAME,
|
||||||
arrayOf<String>(ID, THREAD_ID, MESSAGE_BOX, ADDRESS),
|
arrayOf(ID, THREAD_ID, MESSAGE_BOX, ADDRESS),
|
||||||
DATE_SENT + " = ?",
|
DATE_SENT + " = ?",
|
||||||
arrayOf(timestamp.toString()),
|
arrayOf(timestamp.toString()),
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null
|
null
|
||||||
)
|
).use { cursor ->
|
||||||
while (cursor.moveToNext()) {
|
cursor.asSequence()
|
||||||
if (MmsSmsColumns.Types.isOutgoingMessageType(
|
.map { cursor.getColumnIndexOrThrow(MESSAGE_BOX) }
|
||||||
cursor.getLong(
|
.map(cursor::getLong)
|
||||||
cursor.getColumnIndexOrThrow(
|
.any { MmsSmsColumns.Types.isOutgoingMessageType(it) }
|
||||||
MESSAGE_BOX
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
isOutgoing = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
|
||||||
return isOutgoing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun incrementReceiptCount(
|
fun incrementReceiptCount(
|
||||||
@ -254,15 +223,6 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getThreadIdFor(notification: NotificationInd): Long {
|
|
||||||
val fromString =
|
|
||||||
if (notification.from != null && notification.from.textString != null) toIsoString(
|
|
||||||
notification.from.textString
|
|
||||||
) else ""
|
|
||||||
val recipient = Recipient.from(context, fromExternal(context, fromString), false)
|
|
||||||
return get(context).threadDatabase().getOrCreateThreadIdFor(recipient)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun rawQuery(where: String, arguments: Array<String>?): Cursor {
|
private fun rawQuery(where: String, arguments: Array<String>?): Cursor {
|
||||||
val database = databaseHelper.readableDatabase
|
val database = databaseHelper.readableDatabase
|
||||||
return database.rawQuery(
|
return database.rawQuery(
|
||||||
@ -273,10 +233,6 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessages(idsAsString: String): Cursor {
|
|
||||||
return rawQuery(idsAsString, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getMessage(messageId: Long): Cursor {
|
fun getMessage(messageId: Long): Cursor {
|
||||||
val cursor = rawQuery(RAW_ID_WHERE, arrayOf(messageId.toString()))
|
val cursor = rawQuery(RAW_ID_WHERE, arrayOf(messageId.toString()))
|
||||||
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId))
|
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId))
|
||||||
@ -306,48 +262,30 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markAsPendingInsecureSmsFallback(messageId: Long) {
|
private fun markAs(
|
||||||
val threadId = getThreadIdForMessage(messageId)
|
messageId: Long,
|
||||||
|
baseType: Long,
|
||||||
|
threadId: Long = getThreadIdForMessage(messageId)
|
||||||
|
) {
|
||||||
updateMailboxBitmask(
|
updateMailboxBitmask(
|
||||||
messageId,
|
messageId,
|
||||||
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
||||||
MmsSmsColumns.Types.BASE_PENDING_INSECURE_SMS_FALLBACK,
|
baseType,
|
||||||
Optional.of(threadId)
|
Optional.of(threadId)
|
||||||
)
|
)
|
||||||
notifyConversationListeners(threadId)
|
notifyConversationListeners(threadId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markAsSending(messageId: Long) {
|
fun markAsSending(messageId: Long) {
|
||||||
val threadId = getThreadIdForMessage(messageId)
|
markAs(messageId, MmsSmsColumns.Types.BASE_SENDING_TYPE)
|
||||||
updateMailboxBitmask(
|
|
||||||
messageId,
|
|
||||||
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
|
||||||
MmsSmsColumns.Types.BASE_SENDING_TYPE,
|
|
||||||
Optional.of(threadId)
|
|
||||||
)
|
|
||||||
notifyConversationListeners(threadId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markAsSentFailed(messageId: Long) {
|
fun markAsSentFailed(messageId: Long) {
|
||||||
val threadId = getThreadIdForMessage(messageId)
|
markAs(messageId, MmsSmsColumns.Types.BASE_SENT_FAILED_TYPE)
|
||||||
updateMailboxBitmask(
|
|
||||||
messageId,
|
|
||||||
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
|
||||||
MmsSmsColumns.Types.BASE_SENT_FAILED_TYPE,
|
|
||||||
Optional.of(threadId)
|
|
||||||
)
|
|
||||||
notifyConversationListeners(threadId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun markAsSent(messageId: Long, secure: Boolean) {
|
override fun markAsSent(messageId: Long, secure: Boolean) {
|
||||||
val threadId = getThreadIdForMessage(messageId)
|
markAs(messageId, MmsSmsColumns.Types.BASE_SENT_TYPE or if (secure) MmsSmsColumns.Types.PUSH_MESSAGE_BIT or MmsSmsColumns.Types.SECURE_MESSAGE_BIT else 0)
|
||||||
updateMailboxBitmask(
|
|
||||||
messageId,
|
|
||||||
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
|
||||||
MmsSmsColumns.Types.BASE_SENT_TYPE or if (secure) MmsSmsColumns.Types.PUSH_MESSAGE_BIT or MmsSmsColumns.Types.SECURE_MESSAGE_BIT else 0,
|
|
||||||
Optional.of(threadId)
|
|
||||||
)
|
|
||||||
notifyConversationListeners(threadId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun markUnidentified(messageId: Long, unidentified: Boolean) {
|
override fun markUnidentified(messageId: Long, unidentified: Boolean) {
|
||||||
@ -371,13 +309,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
val mentionChange = if (hasMention) { 1 } else { 0 }
|
val mentionChange = if (hasMention) { 1 } else { 0 }
|
||||||
get(context).threadDatabase().decrementUnread(threadId, 1, mentionChange)
|
get(context).threadDatabase().decrementUnread(threadId, 1, mentionChange)
|
||||||
}
|
}
|
||||||
updateMailboxBitmask(
|
markAs(messageId, MmsSmsColumns.Types.BASE_DELETED_TYPE, threadId)
|
||||||
messageId,
|
|
||||||
MmsSmsColumns.Types.BASE_TYPE_MASK,
|
|
||||||
MmsSmsColumns.Types.BASE_DELETED_TYPE,
|
|
||||||
Optional.of(threadId)
|
|
||||||
)
|
|
||||||
notifyConversationListeners(threadId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun markExpireStarted(messageId: Long) {
|
override fun markExpireStarted(messageId: Long) {
|
||||||
@ -407,10 +339,6 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setAllMessagesRead(): List<MarkedMessageInfo> {
|
|
||||||
return setMessagesRead(READ + " = 0", null)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setMessagesRead(where: String, arguments: Array<String>?): List<MarkedMessageInfo> {
|
private fun setMessagesRead(where: String, arguments: Array<String>?): List<MarkedMessageInfo> {
|
||||||
val database = databaseHelper.writableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
val result: MutableList<MarkedMessageInfo> = LinkedList()
|
val result: MutableList<MarkedMessageInfo> = LinkedList()
|
||||||
@ -419,7 +347,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
try {
|
try {
|
||||||
cursor = database.query(
|
cursor = database.query(
|
||||||
TABLE_NAME,
|
TABLE_NAME,
|
||||||
arrayOf<String>(ID, ADDRESS, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED),
|
arrayOf(ID, ADDRESS, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED),
|
||||||
where,
|
where,
|
||||||
arguments,
|
arguments,
|
||||||
null,
|
null,
|
||||||
@ -1400,25 +1328,16 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa
|
|||||||
val attachments = get(context).attachmentDatabase().getAttachment(
|
val attachments = get(context).attachmentDatabase().getAttachment(
|
||||||
cursor
|
cursor
|
||||||
)
|
)
|
||||||
val contacts: List<Contact?> = getSharedContacts(
|
val contacts: List<Contact?> = getSharedContacts(cursor, attachments)
|
||||||
cursor, attachments
|
val contactAttachments: Set<Attachment?> =
|
||||||
)
|
contacts.mapNotNull { it?.avatarAttachment }.toSet()
|
||||||
val contactAttachments =
|
val previews: List<LinkPreview?> = getLinkPreviews(cursor, attachments)
|
||||||
contacts.map { obj: Contact? -> obj!!.avatarAttachment }
|
val previewAttachments: Set<Attachment?> =
|
||||||
.filter { a: Attachment? -> a != null }
|
previews.mapNotNull { it?.getThumbnail()?.orNull() }.toSet()
|
||||||
.toSet()
|
|
||||||
val previews: List<LinkPreview?> = getLinkPreviews(
|
|
||||||
cursor, attachments
|
|
||||||
)
|
|
||||||
val previewAttachments =
|
|
||||||
previews.filter { lp: LinkPreview? -> lp!!.getThumbnail().isPresent }
|
|
||||||
.map { lp: LinkPreview? -> lp!!.getThumbnail().get() }
|
|
||||||
.toSet()
|
|
||||||
val slideDeck = getSlideDeck(
|
val slideDeck = getSlideDeck(
|
||||||
Stream.of(attachments)
|
attachments
|
||||||
.filterNot { o: DatabaseAttachment? -> contactAttachments.contains(o) }
|
.filterNot { o: DatabaseAttachment? -> o in contactAttachments }
|
||||||
.filterNot { o: DatabaseAttachment? -> previewAttachments.contains(o) }
|
.filterNot { o: DatabaseAttachment? -> o in previewAttachments }
|
||||||
.toList()
|
|
||||||
)
|
)
|
||||||
val quote = getQuote(cursor)
|
val quote = getQuote(cursor)
|
||||||
val reactions = get(context).reactionDatabase().getReactions(cursor)
|
val reactions = get(context).reactionDatabase().getReactions(cursor)
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
package org.thoughtcrime.securesms.util
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
|
||||||
|
fun Cursor.asSequence(): Sequence<Cursor> =
|
||||||
|
generateSequence { if (moveToNext()) this else null }
|
Loading…
Reference in New Issue
Block a user