Merge pull request #15 from loki-project/group-chat-message-deletion

Group Chat Message Deletion
This commit is contained in:
gmbnt 2019-08-26 14:34:38 +10:00 committed by GitHub
commit 488bf56aec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 15 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

@ -18,35 +18,51 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
private val handler = Handler()
private var hasStarted = false
private val task = object : Runnable {
private val api: LokiGroupChatAPI
get() = {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
}()
private val pollForNewMessagesTask = object : Runnable {
override fun run() {
poll()
handler.postDelayed(this, pollInterval)
pollForNewMessages()
handler.postDelayed(this, pollForNewMessagesInterval)
}
}
private val pollForDeletedMessagesTask = object : Runnable {
override fun run() {
pollForDeletedMessages()
handler.postDelayed(this, pollForDeletedMessagesInterval)
}
}
companion object {
private val pollInterval: Long = 4 * 1000
private val pollForNewMessagesInterval: Long = 4 * 1000
private val pollForDeletedMessagesInterval: Long = 120 * 1000
}
fun startIfNeeded() {
if (hasStarted) return
task.run()
pollForNewMessagesTask.run()
pollForDeletedMessagesTask.run()
hasStarted = true
}
fun stop() {
handler.removeCallbacks(task)
handler.removeCallbacks(pollForNewMessagesTask)
handler.removeCallbacks(pollForDeletedMessagesTask)
hasStarted = false
}
private fun poll() {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase).getMessages(groupID).success { messages ->
private fun pollForNewMessages() {
api.getMessages(groupID).success { messages ->
messages.reversed().map { message ->
val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray()
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
@ -59,4 +75,19 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
Log.d("Loki", "Failed to get messages for group chat with ID: $groupID.")
}
}
private fun pollForDeletedMessages() {
api.getDeletedMessageServerIDs(groupID).success { deletedMessageServerIDs ->
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) }
val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context)
val mmsMessageDatabase = DatabaseFactory.getMmsDatabase(context)
deletedMessageIDs.forEach {
smsMessageDatabase.deleteMessage(it)
mmsMessageDatabase.delete(it)
}
}.fail {
Log.d("Loki", "Failed to get deleted messages for group chat with ID: $groupID.")
}
}
}

View File

@ -18,6 +18,13 @@ class LokiMessageDatabase(context: Context, helper: SQLCipherOpenHelper) : Datab
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);"
}
fun getMessageID(serverID: Long): Long? {
val database = databaseHelper.readableDatabase
return database.get(tableName, "${Companion.serverID} = ?", arrayOf( serverID.toString() )) { cursor ->
cursor.getInt(messageID)
}?.toLong()
}
override fun setServerID(messageID: Long, serverID: Long) {
val database = databaseHelper.writableDatabase
val contentValues = ContentValues(2)

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 {