2021-07-09 05:25:57 +02:00
|
|
|
package org.thoughtcrime.securesms.database
|
2019-07-22 01:38:12 +02:00
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.content.Context
|
2019-10-10 02:38:43 +02:00
|
|
|
import android.database.Cursor
|
2021-04-28 09:41:30 +02:00
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupV2
|
2021-05-18 08:11:38 +02:00
|
|
|
import org.session.libsession.utilities.Address
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
2021-02-01 01:35:53 +01:00
|
|
|
import org.session.libsignal.utilities.JsonUtil
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
2019-07-22 01:38:12 +02:00
|
|
|
|
2021-04-26 05:12:05 +02:00
|
|
|
class LokiThreadDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
|
2019-07-22 01:38:12 +02:00
|
|
|
|
|
|
|
companion object {
|
2020-07-15 04:24:43 +02:00
|
|
|
private val sessionResetTable = "loki_thread_session_reset_database"
|
|
|
|
val publicChatTable = "loki_public_chat_database"
|
2020-02-14 01:18:23 +01:00
|
|
|
val threadID = "thread_id"
|
2019-07-22 06:25:59 +02:00
|
|
|
private val sessionResetStatus = "session_reset_status"
|
2020-02-14 01:18:23 +01:00
|
|
|
val publicChat = "public_chat"
|
2021-04-21 09:00:57 +02:00
|
|
|
@JvmStatic
|
|
|
|
val createSessionResetTableCommand = "CREATE TABLE $sessionResetTable ($threadID INTEGER PRIMARY KEY, $sessionResetStatus INTEGER DEFAULT 0);"
|
|
|
|
@JvmStatic
|
|
|
|
val createPublicChatTableCommand = "CREATE TABLE $publicChatTable ($threadID INTEGER PRIMARY KEY, $publicChat TEXT);"
|
2019-07-22 01:38:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 05:12:05 +02:00
|
|
|
fun getThreadID(hexEncodedPublicKey: String): Long {
|
2019-08-07 08:48:54 +02:00
|
|
|
val address = Address.fromSerialized(hexEncodedPublicKey)
|
2019-07-22 04:09:34 +02:00
|
|
|
val recipient = Recipient.from(context, address, false)
|
2021-10-04 09:51:19 +02:00
|
|
|
return DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient)
|
2019-07-22 04:09:34 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 09:17:16 +02:00
|
|
|
fun getAllV2OpenGroups(): Map<Long, OpenGroupV2> {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
var cursor: Cursor? = null
|
|
|
|
val result = mutableMapOf<Long, OpenGroupV2>()
|
|
|
|
try {
|
|
|
|
cursor = database.rawQuery("select * from $publicChatTable", null)
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
val threadID = cursor.getLong(threadID)
|
|
|
|
val string = cursor.getString(publicChat)
|
2021-05-12 06:48:13 +02:00
|
|
|
val openGroup = OpenGroupV2.fromJSON(string)
|
2021-04-13 09:17:16 +02:00
|
|
|
if (openGroup != null) result[threadID] = openGroup
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
// do nothing
|
|
|
|
} finally {
|
|
|
|
cursor?.close()
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-04-21 09:00:57 +02:00
|
|
|
fun getOpenGroupChat(threadID: Long): OpenGroupV2? {
|
|
|
|
if (threadID < 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
val database = databaseHelper.readableDatabase
|
2021-04-27 09:29:37 +02:00
|
|
|
return database.get(publicChatTable, "${Companion.threadID} = ?", arrayOf(threadID.toString())) { cursor ->
|
2021-04-21 09:00:57 +02:00
|
|
|
val json = cursor.getString(publicChat)
|
2021-05-12 06:48:13 +02:00
|
|
|
OpenGroupV2.fromJSON(json)
|
2021-04-21 09:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 09:26:40 +02:00
|
|
|
fun setOpenGroupChat(openGroupV2: OpenGroupV2, threadID: Long) {
|
|
|
|
if (threadID < 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val contentValues = ContentValues(2)
|
|
|
|
contentValues.put(Companion.threadID, threadID)
|
|
|
|
contentValues.put(publicChat, JsonUtil.toJson(openGroupV2.toJson()))
|
|
|
|
database.insertOrUpdate(publicChatTable, contentValues, "${Companion.threadID} = ?", arrayOf(threadID.toString()))
|
|
|
|
}
|
2021-05-26 07:22:19 +02:00
|
|
|
|
|
|
|
fun removeOpenGroupChat(threadID: Long) {
|
|
|
|
if (threadID < 0) return
|
|
|
|
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
database.delete(publicChatTable,"${Companion.threadID} = ?", arrayOf(threadID.toString()))
|
|
|
|
}
|
|
|
|
|
2019-07-22 01:38:12 +02:00
|
|
|
}
|