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

33 lines
1.5 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_sms_friend_request_database"
2019-06-21 00:39:05 +00:00
private val messageID = "message_id"
private val isFriendRequest = "is_friend_request"
2019-06-21 00:39:05 +00:00
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $isFriendRequest INTEGER DEFAULT 0);"
}
2019-06-21 00:39:05 +00:00
fun getIsFriendRequest(messageID: Long): Boolean {
val database = databaseHelper.readableDatabase
2019-06-21 00:39:05 +00:00
return database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
2019-06-17 23:57:36 +00:00
val rawIsFriendRequest = cursor.getInt(isFriendRequest)
rawIsFriendRequest == 1
} ?: false
}
2019-06-21 00:39:05 +00:00
fun setIsFriendRequest(messageID: Long, isFriendRequest: Boolean) {
val database = databaseHelper.writableDatabase
val rawIsFriendRequest = if (isFriendRequest) 1 else 0
val contentValues = ContentValues()
2019-06-21 00:39:05 +00:00
contentValues.put(Companion.messageID, messageID)
contentValues.put(Companion.isFriendRequest, rawIsFriendRequest)
2019-06-21 00:39:05 +00:00
database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() ))
}
}