Skip to content

Commit

Permalink
feat: update all consent entries to be returned
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 8, 2023
1 parent ed1d791 commit c25bd23
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
8 changes: 3 additions & 5 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ConsentListEntry[]> {
const consentList = await XMTPModule.refreshConsentList(clientAddress)

return consentList.map((json: string) => {
return ConsentListEntry.from(json)
})
}

export async function consentList(
clientAddress: string
): Promise<Map<string, 'allowed' | 'denied' | 'unknown'>> {
): Promise<ConsentListEntry[]> {
const consentList = await XMTPModule.consentList(clientAddress)
const result = new Map<string, 'allowed' | 'denied' | 'unknown'>()

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)
Expand All @@ -376,3 +379,4 @@ export * from './XMTP.types'
export { Query } from './lib/Query'
export { XMTPPush } from './lib/XMTPPush'
export { DecodedMessage }
export { ConsentListEntry }
24 changes: 24 additions & 0 deletions src/lib/ConsentListEntry.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 4 additions & 3 deletions src/lib/Contacts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client } from './Client'
import * as XMTPModule from '../index'
import { ConsentListEntry } from './ConsentListEntry'

Check warning on line 3 in src/lib/Contacts.ts

View workflow job for this annotation

GitHub Actions / lint

`./ConsentListEntry` import should occur before import of `../index`

export default class Contacts {
client: Client<any>
Expand All @@ -24,11 +25,11 @@ export default class Contacts {
XMTPModule.allowContacts(this.client.address, addresses)
}

refreshConsentList() {
XMTPModule.refreshConsentList(this.client.address)
async refreshConsentList(): Promise<ConsentListEntry[]> {
return await XMTPModule.refreshConsentList(this.client.address)
}

async consentList(): Promise<Map<string, 'allowed' | 'denied' | 'unknown'>> {
async consentList(): Promise<ConsentListEntry[]> {
return await XMTPModule.consentList(this.client.address)
}
}

0 comments on commit c25bd23

Please sign in to comment.