Skip to content

Commit

Permalink
feat: add function to delete database (#187)
Browse files Browse the repository at this point in the history
* feat: add function to delete database

* write a test for it
  • Loading branch information
nplasterer authored Feb 23, 2024
1 parent 8cb7090 commit 5da219a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.xmtp.android.library

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Ignore
Expand Down Expand Up @@ -124,6 +125,49 @@ class ClientTest {
assert(client.canMessageV3(listOf(client.address)))
}

@Test
fun testCanDeleteDatabase() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val fakeWallet = PrivateKeyBuilder()
val fakeWallet2 = PrivateKeyBuilder()
var client =
Client().create(
account = fakeWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)
val client2 =
Client().create(
account = fakeWallet2,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)
client.conversations.newGroup(listOf(client2.address,))
runBlocking { client.conversations.syncGroups() }
assertEquals(client.conversations.listGroups().size, 1)

client.deleteLocalDatabase()

client =
Client().create(
account = fakeWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)

runBlocking { client.conversations.syncGroups() }
assertEquals(client.conversations.listGroups().size, 0)
}

@Test
fun testCreatesAV3DevClient() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
Expand Down
32 changes: 21 additions & 11 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Client() {
var logger: XMTPLogger = XMTPLogger()
val libXMTPVersion: String = getVersionInfo()
private var libXMTPClient: FfiXmtpClient? = null
private var dbPath: String = ""

companion object {
private const val TAG = "Client"
Expand Down Expand Up @@ -160,6 +161,7 @@ class Client() {
privateKeyBundleV1: PrivateKeyBundleV1,
apiClient: ApiClient,
libXMTPClient: FfiXmtpClient? = null,
dbPath: String = "",
) : this() {
this.address = address
this.privateKeyBundleV1 = privateKeyBundleV1
Expand All @@ -168,6 +170,7 @@ class Client() {
this.libXMTPClient = libXMTPClient
this.conversations =
Conversations(client = this, libXMTPConversations = libXMTPClient?.conversations())
this.dbPath = dbPath
}

fun buildFrom(
Expand All @@ -179,7 +182,7 @@ class Client() {
val clientOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(environment = clientOptions.api.env, secure = clientOptions.api.isSecure)
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
Expand All @@ -190,13 +193,14 @@ class Client() {
address
)
}
} else null
} else Pair(null, " ")

return Client(
address = address,
privateKeyBundleV1 = bundle,
apiClient = apiClient,
libXMTPClient = v3Client
libXMTPClient = v3Client,
dbPath = dbPath
)
}

Expand Down Expand Up @@ -226,7 +230,7 @@ class Client() {
apiClient,
options
)
val libXMTPClient: FfiXmtpClient? =
val (libXMTPClient, dbPath) =
ffiXmtpClient(
options,
account,
Expand All @@ -236,7 +240,7 @@ class Client() {
account.address
)
val client =
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient)
Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient, dbPath)
client.ensureUserContactPublished()
client
} catch (e: java.lang.Exception) {
Expand All @@ -261,7 +265,7 @@ class Client() {
val newOptions = options ?: ClientOptions()
val apiClient =
GRPCApiClient(environment = newOptions.api.env, secure = newOptions.api.isSecure)
val v3Client: FfiXmtpClient? = if (isAlphaMlsEnabled(options)) {
val (v3Client, dbPath) = if (isAlphaMlsEnabled(options)) {
runBlocking {
ffiXmtpClient(
options,
Expand All @@ -272,13 +276,14 @@ class Client() {
address
)
}
} else null
} else Pair(null, "")

return Client(
address = address,
privateKeyBundleV1 = v1Bundle,
apiClient = apiClient,
libXMTPClient = v3Client
libXMTPClient = v3Client,
dbPath = dbPath
)
}

Expand All @@ -293,12 +298,13 @@ class Client() {
privateKeyBundleV1: PrivateKeyBundleV1,
legacyIdentitySource: LegacyIdentitySource,
accountAddress: String,
): FfiXmtpClient? {
): Pair<FfiXmtpClient?, String> {
var dbPath = ""
val v3Client: FfiXmtpClient? =
if (isAlphaMlsEnabled(options)) {
val alias = "xmtp-${options!!.api.env}-${accountAddress.lowercase()}"

val dbPath = if (options.dbPath == null) {
dbPath = if (options.dbPath == null) {
val dbDir = File(appContext?.filesDir?.absolutePath, "xmtp_db")
dbDir.mkdir()
dbDir.absolutePath + "/$alias.db3"
Expand Down Expand Up @@ -364,7 +370,7 @@ class Client() {
}
}
Log.i(TAG, "LibXMTP $libXMTPVersion")
return v3Client
return Pair(v3Client, dbPath)
}

/**
Expand Down Expand Up @@ -586,6 +592,10 @@ class Client() {
return !statuses.contains(false)
}

fun deleteLocalDatabase() {
File(dbPath).delete()
}

val privateKeyBundle: PrivateKeyBundle
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)

Expand Down

0 comments on commit 5da219a

Please sign in to comment.