2019-06-17 00:54:38 +00:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
|
|
|
import android.content.ContentValues
|
|
|
|
import android.content.Context
|
|
|
|
import org.thoughtcrime.securesms.database.Database
|
2019-06-24 04:10:09 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2019-06-17 00:54:38 +00:00
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
2019-06-24 04:10:09 +00:00
|
|
|
import org.whispersystems.signalservice.loki.messaging.LokiMessageDatabaseProtocol
|
|
|
|
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus
|
2019-06-17 00:54:38 +00:00
|
|
|
|
2019-08-09 02:00:01 +00:00
|
|
|
class LokiMessageDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiMessageDatabaseProtocol {
|
2019-06-17 00:54:38 +00:00
|
|
|
|
|
|
|
companion object {
|
2019-06-24 01:22:53 +00:00
|
|
|
private val tableName = "loki_message_friend_request_database"
|
2019-06-21 00:39:05 +00:00
|
|
|
private val messageID = "message_id"
|
2019-08-09 02:00:01 +00:00
|
|
|
private val serverID = "server_id"
|
2019-06-24 01:22:53 +00:00
|
|
|
private val friendRequestStatus = "friend_request_status"
|
2019-08-09 02:00:01 +00:00
|
|
|
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);"
|
|
|
|
}
|
|
|
|
|
2019-09-03 04:48:07 +00:00
|
|
|
fun getServerID(messageID: Long): Long? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
|
|
|
|
cursor.getInt(Companion.serverID)
|
|
|
|
}?.toLong()
|
|
|
|
}
|
|
|
|
|
2019-08-26 03:03:38 +00:00
|
|
|
fun getMessageID(serverID: Long): Long? {
|
|
|
|
val database = databaseHelper.readableDatabase
|
|
|
|
return database.get(tableName, "${Companion.serverID} = ?", arrayOf( serverID.toString() )) { cursor ->
|
|
|
|
cursor.getInt(messageID)
|
|
|
|
}?.toLong()
|
|
|
|
}
|
|
|
|
|
2019-08-09 02:00:01 +00:00
|
|
|
override fun setServerID(messageID: Long, serverID: Long) {
|
|
|
|
val database = databaseHelper.writableDatabase
|
|
|
|
val contentValues = ContentValues(2)
|
|
|
|
contentValues.put(Companion.messageID, messageID)
|
|
|
|
contentValues.put(Companion.serverID, serverID)
|
|
|
|
database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() ))
|
2019-06-17 00:54:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:04:36 +00:00
|
|
|
fun getFriendRequestStatus(messageID: Long): LokiMessageFriendRequestStatus {
|
2019-06-17 00:54:38 +00:00
|
|
|
val database = databaseHelper.readableDatabase
|
2019-06-24 01:22:53 +00:00
|
|
|
val result = database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
|
|
|
|
cursor.getInt(friendRequestStatus)
|
|
|
|
}
|
|
|
|
return if (result != null) {
|
|
|
|
LokiMessageFriendRequestStatus.values().first { it.rawValue == result }
|
|
|
|
} else {
|
|
|
|
LokiMessageFriendRequestStatus.NONE
|
|
|
|
}
|
2019-06-17 00:54:38 +00:00
|
|
|
}
|
2019-06-17 01:57:40 +00:00
|
|
|
|
2019-06-24 04:10:09 +00:00
|
|
|
override fun setFriendRequestStatus(messageID: Long, friendRequestStatus: LokiMessageFriendRequestStatus) {
|
2019-06-17 01:57:40 +00:00
|
|
|
val database = databaseHelper.writableDatabase
|
2019-06-24 01:22:53 +00:00
|
|
|
val contentValues = ContentValues(2)
|
2019-06-21 00:39:05 +00:00
|
|
|
contentValues.put(Companion.messageID, messageID)
|
2019-06-24 01:22:53 +00:00
|
|
|
contentValues.put(Companion.friendRequestStatus, friendRequestStatus.rawValue)
|
2019-06-21 00:39:05 +00:00
|
|
|
database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() ))
|
2019-06-24 04:10:09 +00:00
|
|
|
val threadID = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageID)
|
|
|
|
notifyConversationListeners(threadID)
|
2019-06-17 01:57:40 +00:00
|
|
|
}
|
2019-06-24 01:22:53 +00:00
|
|
|
|
|
|
|
fun isFriendRequest(messageID: Long): Boolean {
|
2019-06-24 04:10:09 +00:00
|
|
|
return getFriendRequestStatus(messageID) != LokiMessageFriendRequestStatus.NONE
|
2019-06-24 01:22:53 +00:00
|
|
|
}
|
2019-06-17 00:54:38 +00:00
|
|
|
}
|