Skip to content

Commit

Permalink
update the ios and android bindings to line up more closely
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 11, 2024
1 parent 1da2c97 commit fc63b39
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 179 deletions.
186 changes: 93 additions & 93 deletions android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ class XMTPModule : Module() {
}
}

Function("preAuthenticateToInboxCallbackCompleted") {
logV("preAuthenticateToInboxCallbackCompleted")
preAuthenticateToInboxCallbackDeferred?.complete(Unit)
}

//
// Auth functions
//
Expand All @@ -289,6 +294,23 @@ class XMTPModule : Module() {
signer?.handleSCW(id = requestID, signature = signature)
}

AsyncFunction("createRandom") Coroutine { hasPreAuthenticateToInboxCallback: Boolean?, dbEncryptionKey: List<Int>, authParams: String ->
withContext(Dispatchers.IO) {
logV("createRandom")
val privateKey = PrivateKeyBuilder()
val options = clientOptions(
dbEncryptionKey,
authParams,
hasPreAuthenticateToInboxCallback,
)
val randomClient = Client().create(account = privateKey, options = options)

ContentJson.Companion
clients[randomClient.inboxId] = randomClient
ClientWrapper.encodeToObj(randomClient)
}
}

AsyncFunction("create") Coroutine { address: String, hasAuthInboxCallback: Boolean?, dbEncryptionKey: List<Int>, authParams: String ->
withContext(Dispatchers.IO) {
logV("create")
Expand All @@ -310,7 +332,7 @@ class XMTPModule : Module() {
clients[client.inboxId] = client
ContentJson.Companion
signer = null
sendEvent("authedV3", ClientWrapper.encodeToObj(client))
sendEvent("authed", ClientWrapper.encodeToObj(client))
}
}

Expand All @@ -328,23 +350,6 @@ class XMTPModule : Module() {
}
}

AsyncFunction("createRandom") Coroutine { hasPreAuthenticateToInboxCallback: Boolean?, dbEncryptionKey: List<Int>, authParams: String ->
withContext(Dispatchers.IO) {
logV("createRandom")
val privateKey = PrivateKeyBuilder()
val options = clientOptions(
dbEncryptionKey,
authParams,
hasPreAuthenticateToInboxCallback,
)
val randomClient = Client().create(account = privateKey, options = options)

ContentJson.Companion
clients[randomClient.inboxId] = randomClient
ClientWrapper.encodeToObj(randomClient)
}
}

AsyncFunction("dropClient") Coroutine { inboxId: String ->
withContext(Dispatchers.IO) {
logV("dropClient")
Expand Down Expand Up @@ -375,7 +380,7 @@ class XMTPModule : Module() {
}
}

AsyncFunction("encryptAttachment") { fileJson: String ->
AsyncFunction("encryptAttachment") { inboxId: String, fileJson: String ->
logV("encryptAttachment")
val file = DecryptedLocalAttachment.fromJson(fileJson)
val uri = Uri.parse(file.fileUri)
Expand All @@ -401,7 +406,7 @@ class XMTPModule : Module() {
).toJson()
}

AsyncFunction("decryptAttachment") { encryptedFileJson: String ->
AsyncFunction("decryptAttachment") { inboxId: String, encryptedFileJson: String ->
logV("decryptAttachment")
val encryptedFile = EncryptedLocalAttachment.fromJson(encryptedFileJson)
val encryptedData = appContext.reactContext?.contentResolver
Expand Down Expand Up @@ -440,6 +445,19 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listDms") Coroutine { inboxId: String, groupParams: String?, sortOrder: String?, limit: Int? ->
withContext(Dispatchers.IO) {
logV("listDms")
val client = clients[inboxId] ?: throw XMTPException("No client")
val params = ConversationParamsWrapper.conversationParamsFromJson(groupParams ?: "")
val order = getConversationSortOrder(sortOrder ?: "")
val dms = client.conversations.listDms(order = order, limit = limit)
dms.map { dm ->
DmWrapper.encode(client, dm, params)
}
}
}

AsyncFunction("listConversations") Coroutine { inboxId: String, conversationParams: String?, sortOrder: String?, limit: Int? ->
withContext(Dispatchers.IO) {
logV("listConversations")
Expand All @@ -455,19 +473,6 @@ class XMTPModule : Module() {
}
}

AsyncFunction("listDms") Coroutine { inboxId: String, groupParams: String?, sortOrder: String?, limit: Int? ->
withContext(Dispatchers.IO) {
logV("listDms")
val client = clients[inboxId] ?: throw XMTPException("No client")
val params = ConversationParamsWrapper.conversationParamsFromJson(groupParams ?: "")
val order = getConversationSortOrder(sortOrder ?: "")
val dms = client.conversations.listDms(order = order, limit = limit)
dms.map { dm ->
DmWrapper.encode(client, dm, params)
}
}
}

AsyncFunction("conversationMessages") Coroutine { inboxId: String, conversationId: String, limit: Int?, beforeNs: Long?, afterNs: Long?, direction: String? ->
withContext(Dispatchers.IO) {
logV("conversationMessages")
Expand Down Expand Up @@ -577,6 +582,15 @@ class XMTPModule : Module() {
}
}

AsyncFunction("findOrCreateDm") Coroutine { inboxId: String, peerAddress: String ->
withContext(Dispatchers.IO) {
logV("findOrCreateDm")
val client = clients[inboxId] ?: throw XMTPException("No client")
val dm = client.conversations.findOrCreateDm(peerAddress)
DmWrapper.encode(client, dm)
}
}

AsyncFunction("createGroup") Coroutine { inboxId: String, peerAddresses: List<String>, permission: String, groupOptionsJson: String ->
withContext(Dispatchers.IO) {
logV("createGroup")
Expand All @@ -599,15 +613,6 @@ class XMTPModule : Module() {
}
}

AsyncFunction("findOrCreateDm") Coroutine { inboxId: String, peerAddress: String ->
withContext(Dispatchers.IO) {
logV("findOrCreateDm")
val client = clients[inboxId] ?: throw XMTPException("No client")
val dm = client.conversations.findOrCreateDm(peerAddress)
DmWrapper.encode(client, dm)
}
}

AsyncFunction("createGroupCustomPermissions") Coroutine { inboxId: String, peerAddresses: List<String>, permissionPolicySetJson: String, groupOptionsJson: String ->
withContext(Dispatchers.IO) {
logV("createGroup")
Expand Down Expand Up @@ -1038,29 +1043,6 @@ class XMTPModule : Module() {
}
}

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

Function("subscribePushTopics") { topics: List<String> ->
logV("subscribePushTopics")
if (topics.isNotEmpty()) {
if (xmtpPush == null) {
throw XMTPException("Push server not registered")
}

val subscriptions = topics.map {
Service.Subscription.newBuilder().also { sub ->
sub.topic = it
}.build()
}

xmtpPush?.subscribeWithMetadata(subscriptions)
}
}

AsyncFunction("setConsentState") Coroutine { inboxId: String, value: String, entryType: String, consentType: String ->
withContext(Dispatchers.IO) {
val client = clients[inboxId] ?: throw XMTPException("No client")
Expand Down Expand Up @@ -1106,11 +1088,6 @@ class XMTPModule : Module() {
}
}

Function("preAuthenticateToInboxCallbackCompleted") {
logV("preAuthenticateToInboxCallbackCompleted")
preAuthenticateToInboxCallbackDeferred?.complete(Unit)
}

AsyncFunction("updateConversationConsent") Coroutine { inboxId: String, conversationId: String, state: String ->
withContext(Dispatchers.IO) {
logV("updateConversationConsent")
Expand All @@ -1122,24 +1099,6 @@ class XMTPModule : Module() {
}
}

AsyncFunction("exportNativeLogs") Coroutine { ->
withContext(Dispatchers.IO) {
try {
val process = Runtime.getRuntime().exec("logcat -d")
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream))

val log = StringBuilder()
var line: String?
while (bufferedReader.readLine().also { line = it } != null) {
log.append(line).append("\n")
}
log.toString()
} catch (e: Exception) {
e.message
}
}
}

Function("subscribeToConversations") { inboxId: String, type: String ->
logV("subscribeToConversations")

Expand All @@ -1161,16 +1120,16 @@ class XMTPModule : Module() {
}
}

Function("unsubscribeFromAllMessages") { inboxId: String ->
logV("unsubscribeFromAllMessages")
subscriptions[getMessagesKey(inboxId)]?.cancel()
}

Function("unsubscribeFromConversations") { inboxId: String ->
logV("unsubscribeFromConversations")
subscriptions[getConversationsKey(inboxId)]?.cancel()
}

Function("unsubscribeFromAllMessages") { inboxId: String ->
logV("unsubscribeFromAllMessages")
subscriptions[getMessagesKey(inboxId)]?.cancel()
}

AsyncFunction("unsubscribeFromMessages") Coroutine { inboxId: String, id: String ->
withContext(Dispatchers.IO) {
logV("unsubscribeFromMessages")
Expand All @@ -1180,6 +1139,47 @@ class XMTPModule : Module() {
)
}
}

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

Function("subscribePushTopics") { topics: List<String> ->
logV("subscribePushTopics")
if (topics.isNotEmpty()) {
if (xmtpPush == null) {
throw XMTPException("Push server not registered")
}

val subscriptions = topics.map {
Service.Subscription.newBuilder().also { sub ->
sub.topic = it
}.build()
}

xmtpPush?.subscribeWithMetadata(subscriptions)
}
}

AsyncFunction("exportNativeLogs") Coroutine { ->
withContext(Dispatchers.IO) {
try {
val process = Runtime.getRuntime().exec("logcat -d")
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream))

val log = StringBuilder()
var line: String?
while (bufferedReader.readLine().also { line = it } != null) {
log.append(line).append("\n")
}
log.toString()
} catch (e: Exception) {
e.message
}
}
}
}

//
Expand Down
Loading

0 comments on commit fc63b39

Please sign in to comment.