mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-03 15:55:41 +00:00
Fix group chat restoration
This commit is contained in:
parent
4efe8fb040
commit
b72e5c4c6a
@ -4,8 +4,12 @@ import android.content.Context
|
|||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
||||||
|
import org.thoughtcrime.securesms.database.Address
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
||||||
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage
|
||||||
|
import org.thoughtcrime.securesms.util.GroupUtil
|
||||||
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
||||||
import org.whispersystems.libsignal.util.guava.Optional
|
import org.whispersystems.libsignal.util.guava.Optional
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceContent
|
import org.whispersystems.signalservice.api.messages.SignalServiceContent
|
||||||
@ -14,20 +18,25 @@ import org.whispersystems.signalservice.api.messages.SignalServiceGroup
|
|||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
||||||
import org.whispersystems.signalservice.loki.api.LokiGroupChat
|
import org.whispersystems.signalservice.loki.api.LokiGroupChat
|
||||||
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
|
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiGroupMessage
|
||||||
|
|
||||||
class LokiGroupChatPoller(private val context: Context, private val group: LokiGroupChat) {
|
class LokiGroupChatPoller(private val context: Context, private val group: LokiGroupChat) {
|
||||||
private val handler = Handler()
|
private val handler = Handler()
|
||||||
private var hasStarted = false
|
private var hasStarted = false
|
||||||
|
|
||||||
|
// region Convenience
|
||||||
|
private val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
|
|
||||||
private val api: LokiGroupChatAPI
|
private val api: LokiGroupChatAPI
|
||||||
get() = {
|
get() = {
|
||||||
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
|
|
||||||
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
|
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
|
||||||
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
|
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
|
||||||
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
|
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
|
||||||
}()
|
}()
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region Tasks
|
||||||
private val pollForNewMessagesTask = object : Runnable {
|
private val pollForNewMessagesTask = object : Runnable {
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
@ -51,13 +60,17 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
handler.postDelayed(this, pollForModerationPermissionInterval)
|
handler.postDelayed(this, pollForModerationPermissionInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region Settings
|
||||||
companion object {
|
companion object {
|
||||||
private val pollForNewMessagesInterval: Long = 4 * 1000
|
private val pollForNewMessagesInterval: Long = 4 * 1000
|
||||||
private val pollForDeletedMessagesInterval: Long = 20 * 1000
|
private val pollForDeletedMessagesInterval: Long = 20 * 1000
|
||||||
private val pollForModerationPermissionInterval: Long = 10 * 60 * 1000
|
private val pollForModerationPermissionInterval: Long = 10 * 60 * 1000
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region Lifecycle
|
||||||
fun startIfNeeded() {
|
fun startIfNeeded() {
|
||||||
if (hasStarted) return
|
if (hasStarted) return
|
||||||
pollForNewMessagesTask.run()
|
pollForNewMessagesTask.run()
|
||||||
@ -72,10 +85,11 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
handler.removeCallbacks(pollForModerationPermissionTask)
|
handler.removeCallbacks(pollForModerationPermissionTask)
|
||||||
hasStarted = false
|
hasStarted = false
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region Polling
|
||||||
private fun pollForNewMessages() {
|
private fun pollForNewMessages() {
|
||||||
api.getMessages(group.serverID, group.server).success { messages ->
|
fun processIncomingMessage(message: LokiGroupMessage) {
|
||||||
messages.reversed().map { message ->
|
|
||||||
val id = group.id.toByteArray()
|
val id = group.id.toByteArray()
|
||||||
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
|
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
|
||||||
val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body)
|
val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body)
|
||||||
@ -83,6 +97,29 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
val x3 = SignalServiceContent(x2, senderDisplayName, SignalServiceAddress.DEFAULT_DEVICE_ID, message.timestamp, false)
|
val x3 = SignalServiceContent(x2, senderDisplayName, SignalServiceAddress.DEFAULT_DEVICE_ID, message.timestamp, false)
|
||||||
PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.of(message.serverID))
|
PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.of(message.serverID))
|
||||||
}
|
}
|
||||||
|
fun processOutgoingMessage(message: LokiGroupMessage) {
|
||||||
|
val messageServerID = message.serverID ?: return
|
||||||
|
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
|
||||||
|
val isDuplicate = lokiMessageDatabase.getMessageID(messageServerID) != null
|
||||||
|
if (isDuplicate) { return }
|
||||||
|
val id = group.id.toByteArray()
|
||||||
|
val smsDatabase = DatabaseFactory.getSmsDatabase(context)
|
||||||
|
val recipient = Recipient.from(context, Address.fromSerialized(GroupUtil.getEncodedId(id, false)), false)
|
||||||
|
val signalMessage = OutgoingTextMessage(recipient, message.body, 0, 0)
|
||||||
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient)
|
||||||
|
val messageID = smsDatabase.insertMessageOutbox(threadID, signalMessage, false, System.currentTimeMillis(), null)
|
||||||
|
smsDatabase.markAsSent(messageID, true)
|
||||||
|
smsDatabase.markUnidentified(messageID, false)
|
||||||
|
lokiMessageDatabase.setServerID(messageID, messageServerID)
|
||||||
|
}
|
||||||
|
api.getMessages(group.serverID, group.server).success { messages ->
|
||||||
|
messages.reversed().forEach { message ->
|
||||||
|
if (message.hexEncodedPublicKey != userHexEncodedPublicKey) {
|
||||||
|
processIncomingMessage(message)
|
||||||
|
} else {
|
||||||
|
processOutgoingMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
}.fail {
|
}.fail {
|
||||||
Log.d("Loki", "Failed to get messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
|
Log.d("Loki", "Failed to get messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
|
||||||
}
|
}
|
||||||
@ -109,4 +146,5 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
lokiAPIDatabase.setIsModerator(group.serverID, group.server, isModerator)
|
lokiAPIDatabase.setIsModerator(group.serverID, group.server, isModerator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
}
|
}
|
@ -18,6 +18,8 @@ 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);"
|
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Move the server ID stuff to LokiAPIDatabase?
|
||||||
|
|
||||||
fun getServerID(messageID: Long): Long? {
|
fun getServerID(messageID: Long): Long? {
|
||||||
val database = databaseHelper.readableDatabase
|
val database = databaseHelper.readableDatabase
|
||||||
return database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
|
return database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user