60 lines
2.9 KiB
Kotlin
Raw Normal View History

2019-07-19 15:02:47 +10:00
package org.thoughtcrime.securesms.loki
import android.content.ContentValues
import android.content.Context
2019-07-19 15:23:45 +10:00
import org.thoughtcrime.securesms.database.Address
2019-07-19 15:02:47 +10:00
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
2019-07-19 15:23:45 +10:00
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.messaging.LokiUserDatabaseProtocol
2019-07-19 15:02:47 +10:00
class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiUserDatabaseProtocol {
2019-07-19 15:02:47 +10:00
companion object {
private val displayNameTable = "loki_user_display_name_database"
2019-07-19 15:02:47 +10:00
private val hexEncodedPublicKey = "hex_encoded_public_key"
private val displayName = "display_name"
@JvmStatic val createDisplayNameTableCommand = "CREATE TABLE $displayNameTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);"
private val publicChatTokenTable = "loki_user_public_chat_token_database"
private val server = "server"
private val token = "token"
@JvmStatic val createPublicChatTokenTableCommand = "CREATE TABLE $publicChatTokenTable ($server TEXT PRIMARY KEY, $token TEXT);"
2019-07-19 15:02:47 +10:00
}
override fun getDisplayName(hexEncodedPublicKey: String): String? {
if (hexEncodedPublicKey == TextSecurePreferences.getLocalNumber(context)) {
return TextSecurePreferences.getProfileName(context)
} else {
val database = databaseHelper.readableDatabase
return database.get(displayNameTable, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey)) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(displayName))
}
2019-07-19 15:02:47 +10:00
}
}
fun setDisplayName(hexEncodedPublicKey: String, displayName: String) {
val database = databaseHelper.writableDatabase
val row = ContentValues(2)
row.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey)
row.put(Companion.displayName, displayName)
database.insertOrUpdate(displayNameTable, row, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey ))
2019-07-19 15:23:45 +10:00
Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners()
2019-07-19 15:02:47 +10:00
}
override fun getToken(server: String): String? {
val database = databaseHelper.readableDatabase
return database.get(publicChatTokenTable, "${Companion.server} = ?", arrayOf( server )) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(token))
}
}
override fun setToken(token: String, server: String) {
val database = databaseHelper.writableDatabase
val row = ContentValues(2)
row.put(Companion.server, server)
row.put(Companion.token, token)
database.insertOrUpdate(publicChatTokenTable, row, "${Companion.server} = ?", arrayOf( server ))
}
2019-07-19 15:02:47 +10:00
}