From c25bd2360e7c093c601e2b7ab181a1d5ef350698 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 7 Dec 2023 16:58:39 -0800 Subject: [PATCH] feat: update all consent entries to be returned --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 3 ++- example/src/tests.ts | 8 +++---- ios/XMTPModule.swift | 8 +++++-- src/index.ts | 22 ++++++++++------- src/lib/ConsentListEntry.ts | 24 +++++++++++++++++++ src/lib/Contacts.ts | 7 +++--- 6 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 src/lib/ConsentListEntry.ts diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index fe01b654f..b9bf5da58 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -548,7 +548,8 @@ class XMTPModule : Module() { AsyncFunction("refreshConsentList") { clientAddress: String -> val client = clients[clientAddress] ?: throw XMTPException("No client") - client.contacts.refreshConsentList() + val consentList = client.contacts.refreshConsentList() + consentList.entries.map { ConsentWrapper.encode(it.value) } } AsyncFunction("conversationConsentState") { clientAddress: String, conversationTopic: String -> diff --git a/example/src/tests.ts b/example/src/tests.ts index 5995bf5f5..4d2c97122 100644 --- a/example/src/tests.ts +++ b/example/src/tests.ts @@ -675,13 +675,11 @@ test('canManagePreferences', async () => { const boConsentList = await bo.contacts.consentList() await delayToPropogate() - if (boConsentList.size !== 1) { - throw new Error(`consent list for bo should 1 not ${boConsentList.size}`) + if (boConsentList.length !== 1) { + throw new Error(`consent list for bo should 1 not ${boConsentList.length}`) } - const boConsentListState = boConsentList.get( - `address-${alixConversation.peerAddress.toLowerCase()}` - ) + const boConsentListState = boConsentList[0].permissionType if (boConsentListState !== 'denied') { throw new Error( diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 280e1201f..7a086f37d 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -501,11 +501,15 @@ public class XMTPModule: Module { try await client.contacts.allow(addresses: addresses) } - AsyncFunction("refreshConsentList") { (clientAddress: String) in + AsyncFunction("refreshConsentList") { (clientAddress: String) -> [String] in guard let client = await clientsManager.getClient(key: clientAddress) else { throw Error.noClient } - try await client.contacts.refreshConsentList() + let consentList = try await client.contacts.refreshConsentList() + + return try consentList.entries.compactMap { entry in + try ConsentWrapper.encode(entry.value) + } } AsyncFunction("conversationConsentState") { (clientAddress: String, conversationTopic: String) -> String in diff --git a/src/index.ts b/src/index.ts index a98507fa4..477126086 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { EventEmitter, NativeModulesProxy } from 'expo-modules-core' import { Client } from '.' import { ConversationContext } from './XMTP.types' import XMTPModule from './XMTPModule' +import { ConsentListEntry } from './lib/ConsentListEntry' import { DecryptedLocalAttachment, EncryptedLocalAttachment, @@ -349,22 +350,24 @@ export function allowContacts(clientAddress: string, addresses: string[]) { XMTPModule.allowContacts(clientAddress, addresses) } -export function refreshConsentList(clientAddress: string) { - XMTPModule.refreshConsentList(clientAddress) +export async function refreshConsentList( + clientAddress: string +): Promise { + const consentList = await XMTPModule.refreshConsentList(clientAddress) + + return consentList.map((json: string) => { + return ConsentListEntry.from(json) + }) } export async function consentList( clientAddress: string -): Promise> { +): Promise { const consentList = await XMTPModule.consentList(clientAddress) - const result = new Map() - consentList.forEach((item) => { - const [key, value] = item.split(':').map((str: string) => str.trim()) - result.set(key.toLowerCase(), value) + return consentList.map((json: string) => { + return ConsentListEntry.from(json) }) - - return result } export const emitter = new EventEmitter(XMTPModule ?? NativeModulesProxy.XMTP) @@ -376,3 +379,4 @@ export * from './XMTP.types' export { Query } from './lib/Query' export { XMTPPush } from './lib/XMTPPush' export { DecodedMessage } +export { ConsentListEntry } diff --git a/src/lib/ConsentListEntry.ts b/src/lib/ConsentListEntry.ts new file mode 100644 index 000000000..9adb1392c --- /dev/null +++ b/src/lib/ConsentListEntry.ts @@ -0,0 +1,24 @@ +export type ConsentState = 'allowed' | 'denied' | 'unknown' + +export type ConsentListEntryType = 'address' + +export class ConsentListEntry { + value: string + entryType: ConsentListEntryType + permissionType: ConsentState + + constructor( + value: string, + entryType: ConsentListEntryType, + permissionType: ConsentState + ) { + this.value = value + this.entryType = entryType + this.permissionType = permissionType + } + + static from(json: string): ConsentListEntry { + const entry = JSON.parse(json) + return new ConsentListEntry(entry.value, entry.type, entry.state) + } +} diff --git a/src/lib/Contacts.ts b/src/lib/Contacts.ts index c4e809849..19c4c302f 100644 --- a/src/lib/Contacts.ts +++ b/src/lib/Contacts.ts @@ -1,5 +1,6 @@ import { Client } from './Client' import * as XMTPModule from '../index' +import { ConsentListEntry } from './ConsentListEntry' export default class Contacts { client: Client @@ -24,11 +25,11 @@ export default class Contacts { XMTPModule.allowContacts(this.client.address, addresses) } - refreshConsentList() { - XMTPModule.refreshConsentList(this.client.address) + async refreshConsentList(): Promise { + return await XMTPModule.refreshConsentList(this.client.address) } - async consentList(): Promise> { + async consentList(): Promise { return await XMTPModule.consentList(this.client.address) } }