Skip to content

Commit

Permalink
Delete database follows ups (#266)
Browse files Browse the repository at this point in the history
* hook up the setting and storing of the dbPath

* write a failing test

* get the test passing
  • Loading branch information
nplasterer authored Feb 27, 2024
1 parent 8eb23f5 commit c851638
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
32 changes: 12 additions & 20 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public final class Client {
let apiClient: ApiClient
let v3Client: LibXMTP.FfiXmtpClient?
public let libXMTPVersion: String = getVersionInfo()
let dbPath: String = ""
let dbPath: String

/// Access ``Conversations`` for this Client.
public lazy var conversations: Conversations = .init(client: self)
Expand Down Expand Up @@ -130,7 +130,7 @@ public final class Client {
source: LegacyIdentitySource,
privateKeyBundleV1: PrivateKeyBundleV1,
signingKey: SigningKey?
) async throws -> FfiXmtpClient? {
) async throws -> (FfiXmtpClient?, String) {
if options?.mlsAlpha == true, options?.api.env.supportsMLS == true {
let dbURL = options?.mlsDbPath ?? URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3").path

Expand Down Expand Up @@ -172,24 +172,24 @@ public final class Client {

print("LibXMTP \(getVersionInfo())")

return v3Client
return (v3Client, dbURL)
} else {
return nil
return (nil, "")
}
}

static func create(account: SigningKey, apiClient: ApiClient, options: ClientOptions? = nil) async throws -> Client {
let (privateKeyBundleV1, source) = try await loadOrCreateKeys(for: account, apiClient: apiClient, options: options)

let v3Client = try await initV3Client(
let (v3Client, dbPath) = try await initV3Client(
address: account.address,
options: options,
source: source,
privateKeyBundleV1: privateKeyBundleV1,
signingKey: account
)

let client = try Client(address: account.address, privateKeyBundleV1: privateKeyBundleV1, apiClient: apiClient, v3Client: v3Client)
let client = try Client(address: account.address, privateKeyBundleV1: privateKeyBundleV1, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath)
try await client.ensureUserContactPublished()

for codec in (options?.codecs ?? []) {
Expand Down Expand Up @@ -273,7 +273,7 @@ public final class Client {
let address = try v1Bundle.identityKey.publicKey.recoverWalletSignerPublicKey().walletAddress
let options = options ?? ClientOptions()

let v3Client = try await initV3Client(
let (v3Client, dbPath) = try await initV3Client(
address: address,
options: options,
source: .static,
Expand All @@ -288,7 +288,7 @@ public final class Client {
rustClient: client
)

let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client)
let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client, dbPath: dbPath)

for codec in options.codecs {
result.register(codec: codec)
Expand All @@ -297,11 +297,12 @@ public final class Client {
return result
}

init(address: String, privateKeyBundleV1: PrivateKeyBundleV1, apiClient: ApiClient, v3Client: LibXMTP.FfiXmtpClient?) throws {
init(address: String, privateKeyBundleV1: PrivateKeyBundleV1, apiClient: ApiClient, v3Client: LibXMTP.FfiXmtpClient?, dbPath: String = "") throws {
self.address = address
self.privateKeyBundleV1 = privateKeyBundleV1
self.apiClient = apiClient
self.v3Client = v3Client
self.dbPath = dbPath
}

public var privateKeyBundle: PrivateKeyBundle {
Expand Down Expand Up @@ -451,18 +452,9 @@ public final class Client {
return subscribe(topics: topics.map(\.description))
}

public func deleteLocalDatabase() {
public func deleteLocalDatabase() throws {
let fm = FileManager.default
let url = URL(string: dbPath)
if (url != nil) {
do {
// swiftlint: disable force_unwrapping
try fm.removeItem(at: url!)
// swiftlint: enable force_unwrapping
} catch {
print("Error deleting file: \(dbPath)")
}
}
try fm.removeItem(atPath: dbPath)
}

func getUserContact(peerAddress: String) async throws -> ContactBundle? {
Expand Down
40 changes: 40 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,46 @@ class ClientTests: XCTestCase {
)
)
}

func testCanDeleteDatabase() async throws {
let bo = try PrivateKey.generate()
let alix = try PrivateKey.generate()
var boClient = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
)
)

let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
)
)

_ = try await boClient.conversations.newGroup(with: [alixClient.address])
try await boClient.conversations.sync()

var groupCount = try await boClient.conversations.groups().count
XCTAssertEqual(groupCount, 1)

try boClient.deleteLocalDatabase()

boClient = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
)
)

try await boClient.conversations.sync()
groupCount = try await boClient.conversations.groups().count
XCTAssertEqual(groupCount, 0)
}

func testCanMessage() async throws {
let fixtures = await fixtures()
Expand Down

0 comments on commit c851638

Please sign in to comment.