diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 9d9e5f7969..5a39d66e3b 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -124,13 +124,13 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiAPIDatabase.getCreateSwarmCacheTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTableCommand()); db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand()); + db.execSQL(LokiAPIDatabase.getCreateGroupChatAuthTokenTableCommand()); db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand()); db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand()); db.execSQL(LokiMessageDatabase.getCreateTableCommand()); db.execSQL(LokiThreadDatabase.getCreateFriendRequestTableCommand()); db.execSQL(LokiThreadDatabase.getCreateSessionResetTableCommand()); - db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); - db.execSQL(LokiUserDatabase.getCreatePublicChatTokenTableCommand()); + db.execSQL(LokiUserDatabase.getCreateTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index f58e0d849c..9cf4d66e72 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -28,6 +28,11 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( private val userID = "user_id" private val receivedMessageHashValues = "received_message_hash_values" @JvmStatic val createReceivedMessageHashValuesTableCommand = "CREATE TABLE $receivedMessageHashValuesCache ($userID TEXT PRIMARY KEY, $receivedMessageHashValues TEXT);" + // Group chat auth token cache + private val groupChatAuthTokenTable = "loki_api_group_chat_auth_token_database" + private val serverURL = "server_url" + private val token = "token" + @JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($serverURL TEXT PRIMARY KEY, $token TEXT);" } override fun getSwarmCache(hexEncodedPublicKey: String): Set? { @@ -77,6 +82,23 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( val row = wrap(mapOf( userID to userPublicKey, receivedMessageHashValues to receivedMessageHashValuesAsString )) database.insertOrUpdate(receivedMessageHashValuesCache, row, "$userID = ?", wrap(userPublicKey)) } + + override fun getGroupChatAuthToken(serverURL: String): String? { + val database = databaseHelper.readableDatabase + return database.get(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL)) { cursor -> + cursor.getString(cursor.getColumnIndexOrThrow(token)) + } + } + + override fun setGroupChatAuthToken(token: String?, serverURL: String) { + val database = databaseHelper.writableDatabase + if (token != null) { + val row = wrap(mapOf(Companion.serverURL to serverURL, Companion.token to token!!)) + database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.serverURL} = ?", wrap(serverURL)) + } else { + database.delete(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL)) + } + } } // region Convenience diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index 9346f2bda6..43265beae2 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -43,9 +43,10 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon private fun poll() { val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) + val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context) - val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context) - LokiGroupChatAPI(userHexEncodedPublicKey, identityKeyPair.privateKey.serialize(), lokiUserDatabase).getMessages(groupID).success { messages -> + val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize() + LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase).getMessages(groupID).success { messages -> messages.reversed().map { message -> val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray() val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) diff --git a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt index a2d04b71b1..50a2faee8f 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt @@ -15,12 +15,7 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database private val displayNameTable = "loki_user_display_name_database" 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);" + @JvmStatic val createTableCommand = "CREATE TABLE $displayNameTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);" } override fun getDisplayName(hexEncodedPublicKey: String): String? { @@ -42,19 +37,4 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database database.insertOrUpdate(displayNameTable, row, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners() } - - 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 )) - } } \ No newline at end of file