mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-28 04:25:18 +00:00
Merge pull request #15 from loki-project/group-chat-message-deletion
Group Chat Message Deletion
This commit is contained in:
commit
488bf56aec
@ -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
|
||||
|
@ -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 {
|
||||
|
||||
override fun run() {
|
||||
poll()
|
||||
handler.postDelayed(this, pollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val pollInterval: Long = 4 * 1000
|
||||
}
|
||||
|
||||
fun startIfNeeded() {
|
||||
if (hasStarted) return
|
||||
task.run()
|
||||
hasStarted = true
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
handler.removeCallbacks(task)
|
||||
hasStarted = false
|
||||
}
|
||||
|
||||
private fun poll() {
|
||||
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).getMessages(groupID).success { messages ->
|
||||
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
|
||||
}()
|
||||
|
||||
private val pollForNewMessagesTask = object : Runnable {
|
||||
|
||||
override fun run() {
|
||||
pollForNewMessages()
|
||||
handler.postDelayed(this, pollForNewMessagesInterval)
|
||||
}
|
||||
}
|
||||
|
||||
private val pollForDeletedMessagesTask = object : Runnable {
|
||||
|
||||
override fun run() {
|
||||
pollForDeletedMessages()
|
||||
handler.postDelayed(this, pollForDeletedMessagesInterval)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val pollForNewMessagesInterval: Long = 4 * 1000
|
||||
private val pollForDeletedMessagesInterval: Long = 120 * 1000
|
||||
}
|
||||
|
||||
fun startIfNeeded() {
|
||||
if (hasStarted) return
|
||||
pollForNewMessagesTask.run()
|
||||
pollForDeletedMessagesTask.run()
|
||||
hasStarted = true
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
handler.removeCallbacks(pollForNewMessagesTask)
|
||||
handler.removeCallbacks(pollForDeletedMessagesTask)
|
||||
hasStarted = false
|
||||
}
|
||||
|
||||
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.")
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user