diff --git a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt index 4a2a9f2134..9c584ba0fb 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt @@ -736,10 +736,7 @@ object SnodeAPI { else json?.let(::parseSnodes) ?.takeIf { it.isNotEmpty() } ?.let { database.setSwarm(publicKey, it.toSet()) } - ?: run { - Log.d("Loki", "Invalidating swarm for: $publicKey.") - dropSnodeFromSwarmIfNeeded(snode, publicKey) - } + ?: dropSnodeFromSwarmIfNeeded(snode, publicKey).also { Log.d("Loki", "Invalidating swarm for: $publicKey.") } } 404 -> { Log.d("Loki", "404, probably no file found") diff --git a/libsignal/src/main/java/org/session/libsignal/utilities/Snode.kt b/libsignal/src/main/java/org/session/libsignal/utilities/Snode.kt index 86f3e26b64..58dcbe8a37 100644 --- a/libsignal/src/main/java/org/session/libsignal/utilities/Snode.kt +++ b/libsignal/src/main/java/org/session/libsignal/utilities/Snode.kt @@ -2,6 +2,9 @@ package org.session.libsignal.utilities import android.annotation.SuppressLint +/** + * Create a Snode from a "-" delimited String if valid, null otherwise. + */ fun Snode(string: String): Snode? { val components = string.split("-") val address = components[0] @@ -31,25 +34,16 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v data class KeySet(val ed25519Key: String, val x25519Key: String) - override fun equals(other: Any?): Boolean { - return if (other is Snode) { - address == other.address && port == other.port - } else { - false - } - } - - override fun hashCode(): Int { - return address.hashCode() xor port.hashCode() - } - - override fun toString(): String { return "$address:$port" } + override fun equals(other: Any?) = other is Snode && address == other.address && port == other.port + override fun hashCode(): Int = address.hashCode() xor port.hashCode() + override fun toString(): String = "$address:$port" companion object { private val CACHE = mutableMapOf() @SuppressLint("NotConstructor") fun Version(value: String) = CACHE.getOrElse(value) { + // internal constructor takes precedence Snode.Version(value) } @@ -62,22 +56,15 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v val ZERO = Version(0UL) private const val MASK_BITS = 16 private const val MASK = 0xFFFFUL - - private fun Sequence.foldToVersionAsULong() = take(4).foldIndexed(0UL) { i, acc, it -> - it.coerceAtMost(MASK) shl (3 - i) * MASK_BITS or acc - } } - internal constructor(parts: List): this( - parts.asSequence() - .map { it.toByte().toULong() } - .foldToVersionAsULong() - ) - internal constructor(value: String): this( value.splitToSequence(".") + .take(4) .map { it.toULongOrNull() ?: 0UL } - .foldToVersionAsULong() + .foldIndexed(0UL) { i, acc, it -> + it.coerceAtMost(MASK) shl (3 - i) * MASK_BITS or acc + } ) operator fun compareTo(other: Version): Int = value.compareTo(other.value)