Skip to content

Commit

Permalink
support the android side of batches
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jun 2, 2023
1 parent 428fffa commit 7e32070
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 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'
}
50 changes: 21 additions & 29 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 @@ -151,29 +151,21 @@ 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).
AsyncFunction("loadMessages") { clientAddress: String, topics: List<String>, limit: Int?, before: Long?, after: Long? ->
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).flatMap {
it.messages(limit = limit, before = beforeDate, after = 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 +180,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 +193,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 +207,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 +216,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 +232,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 7e32070

Please sign in to comment.