Skip to content

Commit

Permalink
Merge pull request #50 from xmtp/np/android-batch-query
Browse files Browse the repository at this point in the history
feat: Android use batchQuerys for loadMessages
  • Loading branch information
nplasterer authored Jun 2, 2023
2 parents 428fffa + c881ac5 commit 27c5b16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ repositories {
dependencies {
implementation project(':expo-modules-core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
implementation "org.xmtp:android:0.1.6"
implementation "org.xmtp:android:0.1.7"
implementation 'com.google.code.gson:gson:2.10.1'
}
47 changes: 19 additions & 28 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class XMTPModule : Module() {
Events("sign", "authed", "conversation", "message")

Function("address") { clientAddress: String ->
logV( "address");
logV("address")
val client = clients[clientAddress]
client?.address ?: "No Client."
}
Expand All @@ -106,7 +106,7 @@ class XMTPModule : Module() {
// Auth functions
//
AsyncFunction("auth") { address: String, environment: String ->
logV( "auth");
logV("auth")
val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address)
signer = reactSigner
val options =
Expand All @@ -117,13 +117,13 @@ class XMTPModule : Module() {
}

Function("receiveSignature") { requestID: String, signature: String ->
logV( "receiveSignature");
logV("receiveSignature")
signer?.handle(id = requestID, signature = signature)
}

// Generate a random wallet and set the client to that
AsyncFunction("createRandom") { environment: String ->
logV( "createRandom");
logV("createRandom")
val privateKey = PrivateKeyBuilder()
val options =
ClientOptions(api = apiEnvironments[environment] ?: apiEnvironments["dev"]!!)
Expand All @@ -135,14 +135,14 @@ class XMTPModule : Module() {
//
// Client API
AsyncFunction("canMessage") { clientAddress: String, peerAddress: String ->
logV( "canMessage");
logV("canMessage")
val client = clients[clientAddress] ?: throw XMTPException("No client")

client.canMessage(peerAddress)
}

AsyncFunction("listConversations") { clientAddress: String ->
logV( "listConversations");
logV("listConversations")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val conversationList = client.conversations.list()
conversationList.map { conversation ->
Expand All @@ -152,28 +152,19 @@ class XMTPModule : Module() {
}

AsyncFunction("loadMessages") { clientAddress: String, topics: List<String>, conversationIDs: List<String?>, limit: Int?, before: Long?, after: Long? ->
logV( "loadMessages");
// TODO: use batchQuery instead of one-at-a-time (once Android SDK supports it).
logV("loadMessages")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val beforeDate = if (before != null) Date(before) else null
val afterDate = if (after != null) Date(after) else null
topics.zip(conversationIDs) { topic, conversationID ->
findConversation(
clientAddress = clientAddress,
topic = topic,
conversationId = conversationID
)
?: throw XMTPException("no conversation found for $topic")

client.conversations.listBatchMessages(topics, limit, beforeDate, afterDate).map {
DecodedMessageWrapper.encode(it)
}
.map {
it.messages(limit = limit, before = beforeDate, after = afterDate)
.map { DecodedMessageWrapper.encode(it) }
}
.flatten()
}

// TODO: Support content types
AsyncFunction("sendMessage") { clientAddress: String, conversationTopic: String, conversationID: String?, content: String ->
logV( "sendMessage");
logV("sendMessage")
val conversation =
findConversation(
clientAddress = clientAddress,
Expand All @@ -188,7 +179,7 @@ class XMTPModule : Module() {
}

AsyncFunction("createConversation") { clientAddress: String, peerAddress: String, conversationID: String? ->
logV( "createConversation");
logV("createConversation")
val client = clients[clientAddress] ?: throw XMTPException("No client")

val conversation = client.conversations.newConversation(
Expand All @@ -201,12 +192,12 @@ class XMTPModule : Module() {
}

Function("subscribeToConversations") { clientAddress: String ->
logV( "subscribeToConversations");
logV("subscribeToConversations")
subscribeToConversations(clientAddress = clientAddress)
}

AsyncFunction("subscribeToMessages") { clientAddress: String, topic: String, conversationID: String? ->
logV( "subscribeToMessages");
logV("subscribeToMessages")
subscribeToMessages(
clientAddress = clientAddress,
topic = topic,
Expand All @@ -215,7 +206,7 @@ class XMTPModule : Module() {
}

AsyncFunction("unsubscribeFromMessages") { clientAddress: String, topic: String, conversationID: String? ->
logV( "unsubscribeFromMessages");
logV("unsubscribeFromMessages")
unsubscribeFromMessages(
clientAddress = clientAddress,
topic = topic,
Expand All @@ -224,13 +215,13 @@ class XMTPModule : Module() {
}

Function("registerPushToken") { pushServer: String, token: String ->
logV( "registerPushToken");
logV("registerPushToken")
xmtpPush = XMTPPush(appContext.reactContext!!, pushServer)
xmtpPush?.register(token)
}

Function("subscribePushTopics") { topics: List<String> ->
logV( "subscribePushTopics");
logV("subscribePushTopics")
if (topics.isNotEmpty()) {
if (xmtpPush == null) {
throw XMTPException("Push server not registered")
Expand All @@ -240,7 +231,7 @@ class XMTPModule : Module() {
}

AsyncFunction("decodeMessage") { clientAddress: String, topic: String, encryptedMessage: String, conversationID: String? ->
logV( "decodeMessage");
logV("decodeMessage")
val encryptedMessageData = Base64.decode(encryptedMessage, Base64.NO_WRAP)
val envelope = EnvelopeBuilder.buildFromString(topic, Date(), encryptedMessageData)
val conversation =
Expand Down

0 comments on commit 27c5b16

Please sign in to comment.