diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 51d45e6c2d..efbe89544a 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -131,6 +131,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiMessageDatabase.getCreateTableCommand()); db.execSQL(LokiThreadDatabase.getCreateFriendRequestTableCommand()); db.execSQL(LokiThreadDatabase.getCreateSessionResetTableCommand()); + db.execSQL(LokiThreadDatabase.getCreateGroupChatMappingTableCommand()); db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); @@ -497,6 +498,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { if (oldVersion < lokiV3) { db.execSQL(LokiAPIDatabase.getCreatePairingAuthorisationTableCommand()); + db.execSQL(LokiThreadDatabase.getCreateGroupChatMappingTableCommand()); + + // TODO: Map old public chat threads to new manager format } db.setTransactionSuccessful(); diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index 294a9249e8..0d4435746a 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -161,7 +161,7 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG finalize() } } - api.getMessages(group.serverID, group.server).success { messages -> + api.getMessages(group.channel, group.server).success { messages -> messages.forEach { message -> if (message.hexEncodedPublicKey != userHexEncodedPublicKey) { processIncomingMessage(message) @@ -170,12 +170,12 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG } } }.fail { - Log.d("Loki", "Failed to get messages for group chat with ID: ${group.serverID} on server: ${group.server}.") + Log.d("Loki", "Failed to get messages for group chat with ID: ${group.channel} on server: ${group.server}.") } } private fun pollForDeletedMessages() { - api.getDeletedMessageServerIDs(group.serverID, group.server).success { deletedMessageServerIDs -> + api.getDeletedMessageServerIDs(group.channel, group.server).success { deletedMessageServerIDs -> val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) } val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context) @@ -185,12 +185,12 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG mmsMessageDatabase.delete(it) } }.fail { - Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.serverID} on server: ${group.server}.") + Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.channel} on server: ${group.server}.") } } private fun pollForModerators() { - api.getModerators(group.serverID, group.server) + api.getModerators(group.channel, group.server) } // endregion } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiThreadDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiThreadDatabase.kt index b291bdad10..1d1158caa3 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiThreadDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiThreadDatabase.kt @@ -7,6 +7,8 @@ import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.thoughtcrime.securesms.recipients.Recipient +import org.whispersystems.signalservice.internal.util.JsonUtil +import org.whispersystems.signalservice.loki.api.LokiGroupChat import org.whispersystems.signalservice.loki.messaging.LokiThreadDatabaseProtocol import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus import org.whispersystems.signalservice.loki.messaging.LokiThreadSessionResetStatus @@ -17,11 +19,14 @@ class LokiThreadDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa companion object { private val friendRequestTableName = "loki_thread_friend_request_database" private val sessionResetTableName = "loki_thread_session_reset_database" + private val groupChatMappingTableName = "loki_group_chat_mapping_database" private val threadID = "thread_id" private val friendRequestStatus = "friend_request_status" private val sessionResetStatus = "session_reset_status" + private val groupChatJSON = "group_chat_json" @JvmStatic val createFriendRequestTableCommand = "CREATE TABLE $friendRequestTableName ($threadID INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);" @JvmStatic val createSessionResetTableCommand = "CREATE TABLE $sessionResetTableName ($threadID INTEGER PRIMARY KEY, $sessionResetStatus INTEGER DEFAULT 0);" + @JvmStatic val createGroupChatMappingTableCommand = "CREATE TABLE $groupChatMappingTableName ($threadID INTEGER PRIMARY KEY, $groupChatJSON TEXT);" } override fun getThreadID(hexEncodedPublicKey: String): Long { @@ -30,7 +35,7 @@ class LokiThreadDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient) } - override fun getThreadID(messageID: Long): Long { + fun getThreadID(messageID: Long): Long { return DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageID) } @@ -84,4 +89,24 @@ class LokiThreadDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa notifyConversationListListeners() notifyConversationListeners(threadID) } + + override fun getGroupChat(threadID: Long): LokiGroupChat? { + val database = databaseHelper.readableDatabase + return database.get(groupChatMappingTableName, "${Companion.threadID} = ?", arrayOf( threadID.toString() )) { cursor -> + val string = cursor.getString(groupChatJSON) + LokiGroupChat.fromJSON(string) + } + } + + override fun setGroupChat(groupChat: LokiGroupChat, threadID: Long) { + val database = databaseHelper.writableDatabase + val contentValues = ContentValues(2) + contentValues.put(Companion.threadID, threadID) + contentValues.put(Companion.groupChatJSON, JsonUtil.toJson(groupChat.toJSON())) + database.insertOrUpdate(groupChatMappingTableName, contentValues, "${Companion.threadID} = ?", arrayOf( threadID.toString() )) + } + + override fun removeGroupChat(threadID: Long) { + databaseHelper.writableDatabase.delete(groupChatMappingTableName, "${Companion.threadID} = ?", arrayOf( threadID.toString() )) + } } \ No newline at end of file