Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to delete local database #287

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.facebook.react:react-native:0.71.3'
implementation "com.daveanthonythomas.moshipack:moshipack:1.0.1"
implementation "org.xmtp:android:0.7.17"
implementation "org.xmtp:android:0.8.0"
// xmtp-android local testing setup below (comment org.xmtp:android above)
// implementation files('<PATH TO XMTP-ANDROID>/xmtp-android/library/build/outputs/aar/library-debug.aar')
// implementation 'com.google.crypto.tink:tink-android:1.7.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ class XMTPModule : Module() {
client?.address ?: "No Client."
}

AsyncFunction("deleteLocalDatabase") { clientAddress: String ->
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.deleteLocalDatabase()
}

//
// Auth functions
//
Expand Down Expand Up @@ -1014,15 +1019,16 @@ class XMTPModule : Module() {
subscriptions[getMessagesKey(clientAddress)]?.cancel()
subscriptions[getMessagesKey(clientAddress)] = CoroutineScope(Dispatchers.IO).launch {
try {
client.conversations.streamAllDecryptedMessages(includeGroups = includeGroups).collect { message ->
sendEvent(
"message",
mapOf(
"clientAddress" to clientAddress,
"message" to DecodedMessageWrapper.encodeMap(message),
client.conversations.streamAllDecryptedMessages(includeGroups = includeGroups)
.collect { message ->
sendEvent(
"message",
mapOf(
"clientAddress" to clientAddress,
"message" to DecodedMessageWrapper.encodeMap(message),
)
)
)
}
}
} catch (e: Exception) {
Log.e("XMTPModule", "Error in all messages subscription: $e")
subscriptions[getMessagesKey(clientAddress)]?.cancel()
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ PODS:
- GenericJSON (~> 2.0)
- Logging (~> 1.0.0)
- secp256k1.swift (~> 0.1)
- XMTP (0.8.13):
- XMTP (0.8.14):
- Connect-Swift (= 0.3.0)
- GzipSwift
- LibXMTP (= 0.4.2-beta3)
Expand All @@ -455,7 +455,7 @@ PODS:
- ExpoModulesCore
- MessagePacker
- secp256k1.swift
- XMTP (= 0.8.13)
- XMTP (= 0.8.14)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -756,8 +756,8 @@ SPEC CHECKSUMS:
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
SwiftProtobuf: b02b5075dcf60c9f5f403000b3b0c202a11b6ae1
web3.swift: 2263d1e12e121b2c42ffb63a5a7beb1acaf33959
XMTP: 0424f5521008373eaa0a8cdba9d4244ba13700f2
XMTPReactNative: cba9ca301d6ae82f649702e5befb3551f98d679f
XMTP: f716708390140ef0542425898888e2346e04174e
XMTPReactNative: 145687cf515815fcc26f89e4347c44814708981b
Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9

PODFILE CHECKSUM: 95d6ace79946933ecf80684613842ee553dd76a2
Expand Down
35 changes: 35 additions & 0 deletions example/src/tests/groupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,41 @@
return true
})

test('can delete a local database', async () => {
let client = await Client.createRandom({
env: 'local',
appVersion: 'Testing/0.0.0',
enableAlphaMls: true,
})

const anotherClient = await Client.createRandom({
env: 'local',
appVersion: 'Testing/0.0.0',
enableAlphaMls: true,
})

await client.conversations.newGroup([anotherClient.address])
await client.conversations.syncGroups()
assert(
(await client.conversations.listGroups()).length === 1,
`should have a group size of 1 but was ${(await client.conversations.listGroups()).length}`
)

await client.deleteLocalDatabase()
client = await Client.createRandom({
env: 'local',
appVersion: 'Testing/0.0.0',
enableAlphaMls: true,
})
await client.conversations.syncGroups()
assert(
(await client.conversations.listGroups()).length === 0,
`should have a group size of 0 but was ${(await client.conversations.listGroups()).length}`
)

return true
})

test('can make a MLS V3 client with encryption key and database path', async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const dbDirPath = `${RNFS.DocumentDirectoryPath}/xmtp_db`
Expand Down Expand Up @@ -138,7 +173,7 @@
appVersion: 'Testing/0.0.0',
enableAlphaMls: true,
})
} catch (error: any) {

Check warning on line 176 in example/src/tests/groupTests.ts

View workflow job for this annotation

GitHub Actions / lint

'error' is defined but never used
return true
}
throw new Error(
Expand Down
7 changes: 7 additions & 0 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public class XMTPModule: Module {
}
}

AsyncFunction("deleteLocalDatabase") { (clientAddress: String) in
guard let client = await clientsManager.getClient(key: clientAddress) else {
throw Error.noClient
}
try client.deleteLocalDatabase()
}

//
// Auth functions
//
Expand Down
2 changes: 1 addition & 1 deletion ios/XMTPReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ Pod::Spec.new do |s|
s.source_files = "**/*.{h,m,swift}"
s.dependency 'secp256k1.swift'
s.dependency "MessagePacker"
s.dependency "XMTP", "= 0.8.13"
s.dependency "XMTP", "= 0.8.14"
end
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function address(): string {
return XMTPModule.address()
}

export async function deleteLocalDatabase(address: string) {
return XMTPModule.deleteLocalDatabase(address)
}

export async function auth(
address: string,
environment: 'local' | 'dev' | 'production',
Expand Down
17 changes: 11 additions & 6 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { DecodedMessage } from '../index'

declare const Buffer

export type GetMessageContentTypeFromClient<C> = C extends Client<infer T>
? T
: never
export type GetMessageContentTypeFromClient<C> =
C extends Client<infer T> ? T : never

export type ExtractDecodedType<C> = C extends XMTPModule.ContentCodec<infer T>
? T
: never
export type ExtractDecodedType<C> =
C extends XMTPModule.ContentCodec<infer T> ? T : never

export class Client<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
Expand Down Expand Up @@ -197,6 +195,13 @@ export class Client<
return await XMTPModule.canMessage(this.address, peerAddress)
}

/**
* Deletes the local database. This cannot be undone and these stored messages will not be refetched from the network.
*/
async deleteLocalDatabase() {
return await XMTPModule.deleteLocalDatabase(this.address)
}

/**
* Determines whether the current user can send messages to the specified peers over groups.
*
Expand Down
Loading