session-android/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt

44 lines
2.0 KiB
Kotlin
Raw Normal View History

package org.thoughtcrime.securesms.loki
import android.content.ContentValues
import android.content.Context
import org.thoughtcrime.securesms.database.Database
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
2019-06-17 23:57:36 +00:00
class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
companion object {
private val tableName = "loki_message_friend_request_database"
2019-06-21 00:39:05 +00:00
private val messageID = "message_id"
private val friendRequestStatus = "friend_request_status"
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);"
}
fun friendRequestStatus(messageID: Long): LokiMessageFriendRequestStatus {
val database = databaseHelper.readableDatabase
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
}
}
fun setFriendRequestStatus(messageID: Long, friendRequestStatus: LokiMessageFriendRequestStatus) {
val database = databaseHelper.writableDatabase
val contentValues = ContentValues(2)
2019-06-21 00:39:05 +00:00
contentValues.put(Companion.messageID, messageID)
contentValues.put(Companion.friendRequestStatus, friendRequestStatus.rawValue)
2019-06-21 00:39:05 +00:00
database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() ))
}
fun isFriendRequest(messageID: Long): Boolean {
return friendRequestStatus(messageID) != LokiMessageFriendRequestStatus.NONE
}
fun hasFriendRequestStatusMessage(messageID: Long): Boolean {
return isFriendRequest(messageID) && friendRequestStatus(messageID) != LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED
}
}