clean and rename

This commit is contained in:
Ryan ZHAO 2020-12-10 15:32:38 +11:00
parent 4c1df33059
commit 92b43b0fcf
4 changed files with 17 additions and 41 deletions

View File

@ -1,7 +1,7 @@
package org.session.libsession.messaging.messages.control.unused package org.session.libsession.messaging.messages.control.unused
import com.google.protobuf.ByteString import com.google.protobuf.ByteString
import org.session.libsession.messaging.Configuration import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.messaging.messages.control.ControlMessage import org.session.libsession.messaging.messages.control.ControlMessage
import org.session.libsignal.libsignal.IdentityKey import org.session.libsignal.libsignal.IdentityKey
import org.session.libsignal.libsignal.ecc.DjbECPublicKey import org.session.libsignal.libsignal.ecc.DjbECPublicKey
@ -21,7 +21,7 @@ class SessionRequest() : ControlMessage() {
if (proto.nullMessage == null) return null if (proto.nullMessage == null) return null
val preKeyBundleProto = proto.preKeyBundleMessage ?: return null val preKeyBundleProto = proto.preKeyBundleMessage ?: return null
var registrationID: Int = 0 var registrationID: Int = 0
registrationID = Configuration.shared.storage.getOrGenerateRegistrationID() //TODO no implementation for getOrGenerateRegistrationID yet registrationID = MessagingConfiguration.shared.storage.getOrGenerateRegistrationID() //TODO no implementation for getOrGenerateRegistrationID yet
//TODO just confirm if the above code does the equivalent to swift below: //TODO just confirm if the above code does the equivalent to swift below:
/*iOS code: Configuration.shared.storage.with { transaction in /*iOS code: Configuration.shared.storage.with { transaction in
registrationID = Configuration.shared.storage.getOrGenerateRegistrationID(using: transaction) registrationID = Configuration.shared.storage.getOrGenerateRegistrationID(using: transaction)

View File

@ -1,6 +1,6 @@
package org.session.libsession.messaging.sending_receiving package org.session.libsession.messaging.sending_receiving
import org.session.libsession.messaging.Configuration import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.messaging.messages.Message import org.session.libsession.messaging.messages.Message
import org.session.libsession.messaging.messages.control.ClosedGroupUpdate import org.session.libsession.messaging.messages.control.ClosedGroupUpdate
import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate import org.session.libsession.messaging.messages.control.ExpirationTimerUpdate
@ -39,7 +39,7 @@ object MessageReceiver {
} }
internal fun parse(data: ByteArray, openGroupServerID: Long?): Pair<Message, SignalServiceProtos.Content> { internal fun parse(data: ByteArray, openGroupServerID: Long?): Pair<Message, SignalServiceProtos.Content> {
val storage = Configuration.shared.storage val storage = MessagingConfiguration.shared.storage
val userPublicKey = storage.getUserPublicKey() val userPublicKey = storage.getUserPublicKey()
val isOpenGroupMessage = openGroupServerID != null val isOpenGroupMessage = openGroupServerID != null
// Parse the envelope // Parse the envelope

View File

@ -9,7 +9,7 @@ import okhttp3.MultipartBody
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody import okhttp3.RequestBody
import org.session.libsession.messaging.Configuration import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.snode.OnionRequestAPI import org.session.libsession.snode.OnionRequestAPI
import org.session.libsession.snode.SnodeAPI import org.session.libsession.snode.SnodeAPI
import org.session.libsession.messaging.fileserver.FileServerAPI import org.session.libsession.messaging.fileserver.FileServerAPI
@ -57,7 +57,7 @@ open class DotNetAPI {
public data class UploadResult(val id: Long, val url: String, val digest: ByteArray?) public data class UploadResult(val id: Long, val url: String, val digest: ByteArray?)
public fun getAuthToken(server: String): Promise<String, Exception> { public fun getAuthToken(server: String): Promise<String, Exception> {
val storage = Configuration.shared.storage val storage = MessagingConfiguration.shared.storage
val token = storage.getAuthToken(server) val token = storage.getAuthToken(server)
if (token != null) { return Promise.of(token) } if (token != null) { return Promise.of(token) }
// Avoid multiple token requests to the server by caching // Avoid multiple token requests to the server by caching
@ -76,7 +76,7 @@ open class DotNetAPI {
private fun requestNewAuthToken(server: String): Promise<String, Exception> { private fun requestNewAuthToken(server: String): Promise<String, Exception> {
Log.d("Loki", "Requesting auth token for server: $server.") Log.d("Loki", "Requesting auth token for server: $server.")
val userKeyPair = Configuration.shared.storage.getUserKeyPair() ?: throw Error.Generic val userKeyPair = MessagingConfiguration.shared.storage.getUserKeyPair() ?: throw Error.Generic
val parameters: Map<String, Any> = mapOf( "pubKey" to userKeyPair.hexEncodedPublicKey ) val parameters: Map<String, Any> = mapOf( "pubKey" to userKeyPair.hexEncodedPublicKey )
return execute(HTTPVerb.GET, server, "loki/v1/get_challenge", false, parameters).map(SnodeAPI.sharedContext) { json -> return execute(HTTPVerb.GET, server, "loki/v1/get_challenge", false, parameters).map(SnodeAPI.sharedContext) { json ->
try { try {
@ -102,7 +102,7 @@ open class DotNetAPI {
private fun submitAuthToken(token: String, server: String): Promise<String, Exception> { private fun submitAuthToken(token: String, server: String): Promise<String, Exception> {
Log.d("Loki", "Submitting auth token for server: $server.") Log.d("Loki", "Submitting auth token for server: $server.")
val userPublicKey = Configuration.shared.storage.getUserPublicKey() ?: throw Error.Generic val userPublicKey = MessagingConfiguration.shared.storage.getUserPublicKey() ?: throw Error.Generic
val parameters = mapOf( "pubKey" to userPublicKey, "token" to token ) val parameters = mapOf( "pubKey" to userPublicKey, "token" to token )
return execute(HTTPVerb.POST, server, "loki/v1/submit_challenge", false, parameters, isJSONRequired = false).map { token } return execute(HTTPVerb.POST, server, "loki/v1/submit_challenge", false, parameters, isJSONRequired = false).map { token }
} }
@ -141,7 +141,7 @@ open class DotNetAPI {
if (exception is HTTP.HTTPRequestFailedException) { if (exception is HTTP.HTTPRequestFailedException) {
val statusCode = exception.statusCode val statusCode = exception.statusCode
if (statusCode == 401 || statusCode == 403) { if (statusCode == 401 || statusCode == 403) {
Configuration.shared.storage.setAuthToken(server, null) MessagingConfiguration.shared.storage.setAuthToken(server, null)
throw Error.TokenExpired throw Error.TokenExpired
} }
} }
@ -256,7 +256,7 @@ open class DotNetAPI {
if (exception is HTTP.HTTPRequestFailedException) { if (exception is HTTP.HTTPRequestFailedException) {
val statusCode = exception.statusCode val statusCode = exception.statusCode
if (statusCode == 401 || statusCode == 403) { if (statusCode == 401 || statusCode == 403) {
Configuration.shared.storage.setAuthToken(server, null) MessagingConfiguration.shared.storage.setAuthToken(server, null)
} }
throw NonSuccessfulResponseCodeException("Request returned with status code ${exception.statusCode}.") throw NonSuccessfulResponseCodeException("Request returned with status code ${exception.statusCode}.")
} }

View File

@ -2,17 +2,13 @@
package org.session.libsession.snode package org.session.libsession.snode
import nl.komponents.kovenant.Kovenant import nl.komponents.kovenant.*
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.functional.bind import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.functional.map import nl.komponents.kovenant.functional.map
import nl.komponents.kovenant.task
import org.session.libsession.snode.utilities.getRandomElement import org.session.libsession.snode.utilities.getRandomElement
import org.session.libsignal.libsignal.logging.Log import org.session.libsignal.libsignal.logging.Log
import org.session.libsignal.service.internal.push.SignalServiceProtos
import org.session.libsignal.service.internal.util.Base64 import org.session.libsignal.service.internal.util.Base64
import org.session.libsignal.service.loki.api.MessageWrapper import org.session.libsignal.service.loki.api.MessageWrapper
import org.session.libsignal.service.loki.api.utilities.HTTP import org.session.libsignal.service.loki.api.utilities.HTTP
@ -24,8 +20,8 @@ import org.session.libsignal.service.internal.push.SignalServiceProtos.Envelope
import java.security.SecureRandom import java.security.SecureRandom
object SnodeAPI { object SnodeAPI {
val database = Configuration.shared.storage val database = SnodeConfiguration.shared.storage
val broadcaster = Configuration.shared.broadcaster val broadcaster = SnodeConfiguration.shared.broadcaster
val sharedContext = Kovenant.createContext("LokiAPISharedContext") val sharedContext = Kovenant.createContext("LokiAPISharedContext")
val messageSendingContext = Kovenant.createContext("LokiAPIMessageSendingContext") val messageSendingContext = Kovenant.createContext("LokiAPIMessageSendingContext")
val messagePollingContext = Kovenant.createContext("LokiAPIMessagePollingContext") val messagePollingContext = Kovenant.createContext("LokiAPIMessagePollingContext")
@ -245,14 +241,13 @@ object SnodeAPI {
} }
} }
private fun parseRawMessagesResponse(rawResponse: RawResponse, snode: Snode, publicKey: String): List<Envelope> { fun parseRawMessagesResponse(rawResponse: RawResponse, snode: Snode, publicKey: String): List<*> {
val messages = rawResponse["messages"] as? List<*> val messages = rawResponse["messages"] as? List<*>
return if (messages != null) { return if (messages != null) {
updateLastMessageHashValueIfPossible(snode, publicKey, messages) updateLastMessageHashValueIfPossible(snode, publicKey, messages)
val newRawMessages = removeDuplicates(publicKey, messages) removeDuplicates(publicKey, messages)
parseEnvelopes(newRawMessages)
} else { } else {
listOf() listOf<Map<*,*>>()
} }
} }
@ -284,25 +279,6 @@ object SnodeAPI {
} }
} }
private fun parseEnvelopes(rawMessages: List<*>): List<Envelope> {
return rawMessages.mapNotNull { rawMessage ->
val rawMessageAsJSON = rawMessage as? Map<*, *>
val base64EncodedData = rawMessageAsJSON?.get("data") as? String
val data = base64EncodedData?.let { Base64.decode(it) }
if (data != null) {
try {
MessageWrapper.unwrap(data)
} catch (e: Exception) {
Log.d("Loki", "Failed to unwrap data for message: ${rawMessage.prettifiedDescription()}.")
null
}
} else {
Log.d("Loki", "Failed to decode data for message: ${rawMessage?.prettifiedDescription()}.")
null
}
}
}
// Error Handling // Error Handling
internal fun handleSnodeError(statusCode: Int, json: Map<*, *>?, snode: Snode, publicKey: String? = null): Exception? { internal fun handleSnodeError(statusCode: Int, json: Map<*, *>?, snode: Snode, publicKey: String? = null): Exception? {
fun handleBadSnode() { fun handleBadSnode() {
@ -366,5 +342,5 @@ object SnodeAPI {
// Type Aliases // Type Aliases
typealias RawResponse = Map<*, *> typealias RawResponse = Map<*, *>
typealias MessageListPromise = Promise<List<Envelope>, Exception> typealias MessageListPromise = Promise<List<*>, Exception>
typealias RawResponsePromise = Promise<RawResponse, Exception> typealias RawResponsePromise = Promise<RawResponse, Exception>