Keep track of first message ID

This commit is contained in:
Niels Andriesse 2019-08-26 13:55:45 +10:00
parent c5e4a2255c
commit a2a74fddcd
3 changed files with 44 additions and 4 deletions

View File

@ -33,6 +33,16 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
private val serverURL = "server_url"
private val token = "token"
@JvmStatic val createGroupChatAuthTokenTableCommand = "CREATE TABLE $groupChatAuthTokenTable ($serverURL TEXT PRIMARY KEY, $token TEXT);"
// Last message server ID cache
private val lastMessageServerIDCache = "loki_api_last_message_server_id_cache"
private val lastMessageServerIDCacheGroupID = "group_id"
private val lastMessageServerID = "last_message_server_id"
@JvmStatic val createLastMessageServerIDTableCommand = "CREATE TABLE $lastMessageServerIDCache ($lastMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $lastMessageServerID INTEGER);"
// First message server ID cache
private val firstMessageServerIDCache = "loki_api_first_message_server_id_cache"
private val firstMessageServerIDCacheGroupID = "group_id"
private val firstMessageServerID = "first_message_server_id"
@JvmStatic val createFirstMessageServerIDTableCommand = "CREATE TABLE $firstMessageServerIDCache ($firstMessageServerIDCacheGroupID INTEGER PRIMARY KEY, $firstMessageServerID INTEGER);"
}
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
@ -90,15 +100,41 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
}
}
override fun setGroupChatAuthToken(token: String?, serverURL: String) {
override fun setGroupChatAuthToken(serverURL: String, newValue: String?) {
val database = databaseHelper.writableDatabase
if (token != null) {
val row = wrap(mapOf(Companion.serverURL to serverURL, Companion.token to token!!))
if (newValue != null) {
val row = wrap(mapOf(Companion.serverURL to serverURL, token to newValue!!))
database.insertOrUpdate(groupChatAuthTokenTable, row, "${Companion.serverURL} = ?", wrap(serverURL))
} else {
database.delete(groupChatAuthTokenTable, "${Companion.serverURL} = ?", wrap(serverURL))
}
}
override fun getLastMessageServerID(groupID: Long): Long? {
val database = databaseHelper.readableDatabase
return database.get(lastMessageServerIDCache, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor ->
cursor.getInt(lastMessageServerID)
}?.toLong()
}
override fun setLastMessageServerID(groupID: Long, newValue: Long) {
val database = databaseHelper.writableDatabase
val row = wrap(mapOf( lastMessageServerIDCacheGroupID to groupID.toString(), lastMessageServerID to newValue.toString() ))
database.insertOrUpdate(lastMessageServerIDCache, row, "$lastMessageServerIDCacheGroupID = ?", wrap(groupID.toString()))
}
override fun getFirstMessageServerID(groupID: Long): Long? {
val database = databaseHelper.readableDatabase
return database.get(firstMessageServerIDCache, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString())) { cursor ->
cursor.getInt(firstMessageServerID)
}?.toLong()
}
override fun setFirstMessageServerID(groupID: Long, newValue: Long) {
val database = databaseHelper.writableDatabase
val row = wrap(mapOf( firstMessageServerIDCacheGroupID to groupID.toString(), firstMessageServerID to newValue.toString() ))
database.insertOrUpdate(firstMessageServerIDCache, row, "$firstMessageServerIDCacheGroupID = ?", wrap(groupID.toString()))
}
}
// region Convenience

View File

@ -77,7 +77,7 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
}
private fun pollForDeletedMessages() {
api.getDeletedMessageIDs(groupID, 0).success { deletedMessageServerIDs ->
api.getDeletedMessageServerIDs(groupID).success { deletedMessageServerIDs ->
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) }
val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context)

View File

@ -8,13 +8,17 @@ import android.widget.Toast
import network.loki.messenger.R
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.conversation.ConversationActivity
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.logging.Log
import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.qr.ScanListener
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.util.DynamicTheme
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
class NewConversationActivity : PassphraseRequiredActionBarActivity(), ScanListener {