feat: add in TTL extension subrequest and builder, enable extending TTLs for all latest config messages in poll as subrequest

This commit is contained in:
0x330a 2023-05-11 12:21:14 +10:00
parent f747f8e863
commit db18b156a3
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
2 changed files with 49 additions and 7 deletions

View File

@ -34,6 +34,7 @@ import org.session.libsignal.utilities.Snode
import java.security.SecureRandom import java.security.SecureRandom
import java.util.Timer import java.util.Timer
import java.util.TimerTask import java.util.TimerTask
import kotlin.time.Duration.Companion.days
private class PromiseCanceledException : Exception("Promise canceled.") private class PromiseCanceledException : Exception("Promise canceled.")
@ -176,7 +177,9 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
requestSparseArray[personalMessages.namespace!!] = personalMessages requestSparseArray[personalMessages.namespace!!] = personalMessages
} }
// get the latest convo info volatile // get the latest convo info volatile
val hashesToExtend = mutableSetOf<String>()
configFactory.getUserConfigs().mapNotNull { config -> configFactory.getUserConfigs().mapNotNull { config ->
hashesToExtend += config.currentHashes()
SnodeAPI.buildAuthenticatedRetrieveBatchRequest( SnodeAPI.buildAuthenticatedRetrieveBatchRequest(
snode, userPublicKey, snode, userPublicKey,
config.configNamespace() config.configNamespace()
@ -186,7 +189,19 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
requestSparseArray[request.namespace!!] = request requestSparseArray[request.namespace!!] = request
} }
val requests = requestSparseArray.valueIterator().asSequence().toList() val requests =
requestSparseArray.valueIterator().asSequence().toMutableList()
if (hashesToExtend.isNotEmpty()) {
SnodeAPI.buildAuthenticatedAlterTtlBatchRequest(
messageHashes = hashesToExtend.toList(),
publicKey = userPublicKey,
newExpiry = SnodeAPI.nowWithOffset + 14.days.inWholeMilliseconds,
extend = true
)?.let { extensionRequest ->
requests += extensionRequest
}
}
SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses -> SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses ->
isCaughtUp = true isCaughtUp = true

View File

@ -493,6 +493,20 @@ object SnodeAPI {
) )
} }
fun buildAuthenticatedAlterTtlBatchRequest(
messageHashes: List<String>,
newExpiry: Long,
publicKey: String,
shorten: Boolean = false,
extend: Boolean = false): SnodeBatchRequestInfo? {
val params = buildAlterTtlParams(messageHashes, newExpiry, publicKey, extend, shorten) ?: return null
return SnodeBatchRequestInfo(
Snode.Method.Expire.rawValue,
params,
null
)
}
fun getRawBatchResponse(snode: Snode, publicKey: String, requests: List<SnodeBatchRequestInfo>, sequence: Boolean = false): RawResponsePromise { fun getRawBatchResponse(snode: Snode, publicKey: String, requests: List<SnodeBatchRequestInfo>, sequence: Boolean = false): RawResponsePromise {
val parameters = mutableMapOf<String, Any>( val parameters = mutableMapOf<String, Any>(
"requests" to requests "requests" to requests
@ -533,8 +547,24 @@ object SnodeAPI {
} }
fun alterTtl(messageHashes: List<String>, newExpiry: Long, publicKey: String, extend: Boolean = false, shorten: Boolean = false): RawResponsePromise { fun alterTtl(messageHashes: List<String>, newExpiry: Long, publicKey: String, extend: Boolean = false, shorten: Boolean = false): RawResponsePromise {
val userEd25519KeyPair = MessagingModuleConfiguration.shared.getUserED25519KeyPair() ?: return Promise.ofFail(NullPointerException("No user key pair"))
return retryIfNeeded(maxRetryCount) { return retryIfNeeded(maxRetryCount) {
val params = buildAlterTtlParams(messageHashes, newExpiry, publicKey, extend, shorten)
?: return@retryIfNeeded Promise.ofFail(
Exception("Couldn't build signed params for alterTtl request for newExpiry=$newExpiry, extend=$extend, shorten=$shorten")
)
getSingleTargetSnode(publicKey).bind { snode ->
invoke(Snode.Method.Expire, snode, params, publicKey)
}
}
}
private fun buildAlterTtlParams( // TODO: in future this will probably need to use the closed group subkeys / admin keys for group swarms
messageHashes: List<String>,
newExpiry: Long,
publicKey: String,
extend: Boolean = false,
shorten: Boolean = false): Map<String, Any>? {
val userEd25519KeyPair = MessagingModuleConfiguration.shared.getUserED25519KeyPair() ?: return null
val params = mutableMapOf( val params = mutableMapOf(
"expiry" to newExpiry, "expiry" to newExpiry,
"messages" to messageHashes, "messages" to messageHashes,
@ -559,16 +589,13 @@ object SnodeAPI {
) )
} catch (e: Exception) { } catch (e: Exception) {
Log.e("Loki", "Signing data failed with user secret key", e) Log.e("Loki", "Signing data failed with user secret key", e)
return@retryIfNeeded Promise.ofFail(e) return null
} }
params["pubkey"] = publicKey params["pubkey"] = publicKey
params["pubkey_ed25519"] = ed25519PublicKey params["pubkey_ed25519"] = ed25519PublicKey
params["signature"] = Base64.encodeBytes(signature) params["signature"] = Base64.encodeBytes(signature)
getSingleTargetSnode(publicKey).bind { snode -> return params
invoke(Snode.Method.Expire, snode, params, publicKey)
}
}
} }
fun getMessages(publicKey: String): MessageListPromise { fun getMessages(publicKey: String): MessageListPromise {