mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-18 07:14:01 +00:00
refactor: config database changes, new protos, adding in support for config base namespace queries
This commit is contained in:
@@ -8,34 +8,39 @@ import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||
class ConfigDatabase(context: Context, helper: SQLCipherOpenHelper): Database(context, helper) {
|
||||
|
||||
companion object {
|
||||
private const val KEY = "key"
|
||||
private const val VALUE = "value"
|
||||
private const val VARIANT = "variant"
|
||||
private const val PUBKEY = "publicKey"
|
||||
private const val DATA = "data"
|
||||
private const val COMBINED_MESSAGE_HASHES = "combined_message_hashes"
|
||||
|
||||
private const val TABLE_NAME = "configs_table"
|
||||
|
||||
const val CREATE_CONFIG_TABLE_COMMAND = "CREATE TABLE $TABLE_NAME ($KEY TEXT NOT NULL, $VALUE BLOB NOT NULL, PRIMARY KEY($KEY));"
|
||||
private const val KEY_WHERE = "$KEY = ?"
|
||||
const val CREATE_CONFIG_TABLE_COMMAND =
|
||||
"CREATE TABLE $TABLE_NAME ($VARIANT TEXT NOT NULL, $PUBKEY TEXT NOT NULL, $DATA BLOB, $COMBINED_MESSAGE_HASHES TEXT, PRIMARY KEY($VARIANT, $PUBKEY));"
|
||||
private const val VARIANT_WHERE = "$VARIANT = ?"
|
||||
private const val VARIANT_AND_PUBKEY_WHERE = "$VARIANT = ? AND $PUBKEY = ?"
|
||||
|
||||
const val USER_KEY = "user"
|
||||
const val CONTACTS_KEY = "contacts"
|
||||
// conversations use publicKey / URL
|
||||
}
|
||||
|
||||
fun storeConfig(key: String, data: ByteArray) {
|
||||
fun storeConfig(variant: String, publicKey: String, data: ByteArray) {
|
||||
val db = writableDatabase
|
||||
val contentValues = contentValuesOf(
|
||||
KEY to key,
|
||||
VALUE to data
|
||||
VARIANT to variant,
|
||||
PUBKEY to publicKey,
|
||||
DATA to data
|
||||
)
|
||||
db.insertOrUpdate(TABLE_NAME, contentValues, KEY_WHERE, arrayOf(key))
|
||||
db.insertOrUpdate(TABLE_NAME, contentValues, VARIANT_AND_PUBKEY_WHERE, arrayOf(variant, publicKey))
|
||||
}
|
||||
|
||||
fun retrieveConfig(key: String): ByteArray? {
|
||||
fun retrieveConfig(variant: String, publicKey: String): ByteArray? {
|
||||
val db = readableDatabase
|
||||
val query = db.query(TABLE_NAME, arrayOf(VALUE), KEY_WHERE, arrayOf(key),null, null, null)
|
||||
val query = db.query(TABLE_NAME, arrayOf(DATA), VARIANT_AND_PUBKEY_WHERE, arrayOf(variant, publicKey),null, null, null)
|
||||
val bytes = query?.use { cursor ->
|
||||
if (!cursor.moveToFirst()) return null
|
||||
cursor.getBlobOrNull(cursor.getColumnIndex(VALUE))
|
||||
cursor.getBlobOrNull(cursor.getColumnIndex(DATA))
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
@@ -2,57 +2,54 @@ package org.thoughtcrime.securesms.util
|
||||
|
||||
import android.content.Context
|
||||
import nl.komponents.kovenant.Promise
|
||||
import org.session.libsession.messaging.messages.Destination
|
||||
import org.session.libsession.messaging.messages.control.ConfigurationMessage
|
||||
import org.session.libsession.messaging.sending_receiving.MessageSender
|
||||
import org.session.libsession.utilities.Address
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
|
||||
object ConfigurationMessageUtilities {
|
||||
|
||||
@JvmStatic
|
||||
fun syncConfigurationIfNeeded(context: Context) {
|
||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context) ?: return
|
||||
val lastSyncTime = TextSecurePreferences.getLastConfigurationSyncTime(context)
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - lastSyncTime < 7 * 24 * 60 * 60 * 1000) return
|
||||
val contacts = ContactUtilities.getAllContacts(context).filter { recipient ->
|
||||
!recipient.name.isNullOrEmpty() && !recipient.isLocalNumber && recipient.address.serialize().isNotEmpty()
|
||||
}.map { recipient ->
|
||||
ConfigurationMessage.Contact(
|
||||
publicKey = recipient.address.serialize(),
|
||||
name = recipient.name!!,
|
||||
profilePicture = recipient.profileAvatar,
|
||||
profileKey = recipient.profileKey,
|
||||
isApproved = recipient.isApproved,
|
||||
isBlocked = recipient.isBlocked,
|
||||
didApproveMe = recipient.hasApprovedMe()
|
||||
)
|
||||
}
|
||||
val configurationMessage = ConfigurationMessage.getCurrent(contacts) ?: return
|
||||
MessageSender.send(configurationMessage, Address.fromSerialized(userPublicKey))
|
||||
TextSecurePreferences.setLastConfigurationSyncTime(context, now)
|
||||
return
|
||||
// val userPublicKey = TextSecurePreferences.getLocalNumber(context) ?: return
|
||||
// val lastSyncTime = TextSecurePreferences.getLastConfigurationSyncTime(context)
|
||||
// val now = System.currentTimeMillis()
|
||||
// if (now - lastSyncTime < 7 * 24 * 60 * 60 * 1000) return
|
||||
// val contacts = ContactUtilities.getAllContacts(context).filter { recipient ->
|
||||
// !recipient.name.isNullOrEmpty() && !recipient.isLocalNumber && recipient.address.serialize().isNotEmpty()
|
||||
// }.map { recipient ->
|
||||
// ConfigurationMessage.Contact(
|
||||
// publicKey = recipient.address.serialize(),
|
||||
// name = recipient.name!!,
|
||||
// profilePicture = recipient.profileAvatar,
|
||||
// profileKey = recipient.profileKey,
|
||||
// isApproved = recipient.isApproved,
|
||||
// isBlocked = recipient.isBlocked,
|
||||
// didApproveMe = recipient.hasApprovedMe()
|
||||
// )
|
||||
// }
|
||||
// val configurationMessage = ConfigurationMessage.getCurrent(contacts) ?: return
|
||||
// MessageSender.send(configurationMessage, Address.fromSerialized(userPublicKey))
|
||||
// TextSecurePreferences.setLastConfigurationSyncTime(context, now)
|
||||
}
|
||||
|
||||
fun forceSyncConfigurationNowIfNeeded(context: Context): Promise<Unit, Exception> {
|
||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context) ?: return Promise.ofSuccess(Unit)
|
||||
val contacts = ContactUtilities.getAllContacts(context).filter { recipient ->
|
||||
!recipient.isGroupRecipient && !recipient.name.isNullOrEmpty() && !recipient.isLocalNumber && recipient.address.serialize().isNotEmpty()
|
||||
}.map { recipient ->
|
||||
ConfigurationMessage.Contact(
|
||||
publicKey = recipient.address.serialize(),
|
||||
name = recipient.name!!,
|
||||
profilePicture = recipient.profileAvatar,
|
||||
profileKey = recipient.profileKey,
|
||||
isApproved = recipient.isApproved,
|
||||
isBlocked = recipient.isBlocked,
|
||||
didApproveMe = recipient.hasApprovedMe()
|
||||
)
|
||||
}
|
||||
val configurationMessage = ConfigurationMessage.getCurrent(contacts) ?: return Promise.ofSuccess(Unit)
|
||||
val promise = MessageSender.send(configurationMessage, Destination.from(Address.fromSerialized(userPublicKey)))
|
||||
TextSecurePreferences.setLastConfigurationSyncTime(context, System.currentTimeMillis())
|
||||
return promise
|
||||
return Promise.ofSuccess(Unit)
|
||||
// val userPublicKey = TextSecurePreferences.getLocalNumber(context) ?: return Promise.ofSuccess(Unit)
|
||||
// val contacts = ContactUtilities.getAllContacts(context).filter { recipient ->
|
||||
// !recipient.isGroupRecipient && !recipient.name.isNullOrEmpty() && !recipient.isLocalNumber && recipient.address.serialize().isNotEmpty()
|
||||
// }.map { recipient ->
|
||||
// ConfigurationMessage.Contact(
|
||||
// publicKey = recipient.address.serialize(),
|
||||
// name = recipient.name!!,
|
||||
// profilePicture = recipient.profileAvatar,
|
||||
// profileKey = recipient.profileKey,
|
||||
// isApproved = recipient.isApproved,
|
||||
// isBlocked = recipient.isBlocked,
|
||||
// didApproveMe = recipient.hasApprovedMe()
|
||||
// )
|
||||
// }
|
||||
// val configurationMessage = ConfigurationMessage.getCurrent(contacts) ?: return Promise.ofSuccess(Unit)
|
||||
// val promise = MessageSender.send(configurationMessage, Destination.from(Address.fromSerialized(userPublicKey)))
|
||||
// TextSecurePreferences.setLastConfigurationSyncTime(context, System.currentTimeMillis())
|
||||
// return promise
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user