Skip to content

Commit

Permalink
unify the error logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Sep 18, 2024
1 parent e881346 commit 278d296
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -562,25 +562,21 @@ class Client() {
}

fun findGroup(groupId: String): Group? {
v3Client?.let {
try {
return Group(this, it.group(groupId.hexToByteArray()))
} catch (e: Exception) {
return null
}
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
try {
return Group(this, client.group(groupId.hexToByteArray()))
} catch (e: Exception) {
return null
}
throw XMTPException("Error no V3 client initialized")
}

fun findMessage(messageId: String): MessageV3? {
v3Client?.let {
try {
return MessageV3(this, it.message(messageId.hexToByteArray()))
} catch (e: Exception) {
return null
}
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
return try {
MessageV3(this, client.message(messageId.hexToByteArray()))
} catch (e: Exception) {
null
}
throw XMTPException("Error no V3 client initialized")
}

suspend fun publish(envelopes: List<Envelope>) {
Expand Down Expand Up @@ -672,17 +668,13 @@ class Client() {
}

suspend fun canMessageV3(addresses: List<String>): Map<String, Boolean> {
v3Client?.let {
return it.canMessage(addresses)
}
throw XMTPException("Error no V3 client initialized")
return v3Client?.canMessage(addresses)
?: throw XMTPException("Error no V3 client initialized")
}

suspend fun inboxIdFromAddress(address: String): String? {
v3Client?.let {
return it.findInboxId(address.lowercase())
}
throw XMTPException("Error no V3 client initialized")
return v3Client?.findInboxId(address.lowercase())
?: throw XMTPException("Error no V3 client initialized")
}

fun deleteLocalDatabase() {
Expand All @@ -706,21 +698,17 @@ class Client() {
}

suspend fun revokeAllOtherInstallations(signingKey: SigningKey) {
if (v3Client == null) throw XMTPException("Error no V3 client initialized")
v3Client?.let { client ->
val signatureRequest = client.revokeAllOtherInstallations()
signingKey.sign(signatureRequest.signatureText())?.let {
signatureRequest.addEcdsaSignature(it.rawData)
client.applySignatureRequest(signatureRequest)
}
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
val signatureRequest = client.revokeAllOtherInstallations()
signingKey.sign(signatureRequest.signatureText())?.let {
signatureRequest.addEcdsaSignature(it.rawData)
client.applySignatureRequest(signatureRequest)
}
}

suspend fun inboxState(refreshFromNetwork: Boolean): InboxState {
v3Client?.let {
return InboxState(it.inboxState(refreshFromNetwork))
}
throw XMTPException("Error no V3 client initialized")
val client = v3Client ?: throw XMTPException("Error no V3 client initialized")
return InboxState(client.inboxState(refreshFromNetwork))
}

val privateKeyBundle: PrivateKeyBundle
Expand Down

0 comments on commit 278d296

Please sign in to comment.