Skip to content

Commit

Permalink
fix: add consent streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 25, 2024
1 parent fefbcdb commit f0eb4a7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/lib/ConsentRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ export type ConsentType = 'address' | 'conversation_id' | 'inbox_id'
export class ConsentRecord {
value: string
entryType: ConsentType
permissionType: ConsentState
state: ConsentState

constructor(
value: string,
entryType: ConsentType,
permissionType: ConsentState
) {
constructor(value: string, entryType: ConsentType, state: ConsentState) {
this.value = value
this.entryType = entryType
this.permissionType = permissionType
this.state = state
}

static from(json: string): ConsentRecord {
Expand Down
51 changes: 46 additions & 5 deletions src/lib/PrivatePreferences.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Address, Client, InboxId } from './Client'
import { ConsentListEntry, ConsentState } from './ConsentRecord'
import { ConsentRecord, ConsentState } from './ConsentRecord'
import { EventTypes } from './types/EventTypes'
import * as XMTPModule from '../index'
import { ConversationId } from '../index'
import { getAddress } from '../utils/address'

export default class PrivatePreferences {
client: Client<any>
private subscriptions: { [key: string]: { remove: () => void } } = {}

constructor(client: Client<any>) {
this.client = client
Expand All @@ -31,16 +33,55 @@ export default class PrivatePreferences {
)
}

async setConsentState(consentEntry: ConsentListEntry): Promise<void> {
async setConsentState(consentRecord: ConsentRecord): Promise<void> {
return await XMTPModule.setConsentState(
this.client.inboxId,
consentEntry.value,
consentEntry.entryType,
consentEntry.permissionType
consentRecord.value,
consentRecord.entryType,
consentRecord.permissionType

Check failure on line 41 in src/lib/PrivatePreferences.ts

View workflow job for this annotation

GitHub Actions / test

Property 'permissionType' does not exist on type 'ConsentRecord'.

Check failure on line 41 in src/lib/PrivatePreferences.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'permissionType' does not exist on type 'ConsentRecord'.

Check failure on line 41 in src/lib/PrivatePreferences.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'permissionType' does not exist on type 'ConsentRecord'.
)
}

async syncConsent(): Promise<void> {
return await XMTPModule.syncConsent(this.client.inboxId)
}

/**
* This method streams consent.
* @returns {Promise<ConsentRecord[]>} A Promise that resolves to an array of ConsentRecord objects.
*/
async streamConsent(
callback: (consent: ConsentRecord) => Promise<void>
): Promise<void> {
XMTPModule.subscribeToConsent(this.client.inboxId)
const subscription = XMTPModule.emitter.addListener(
EventTypes.Consent,
async ({
inboxId,
consent,
}: {
inboxId: string
consent: ConsentRecord
}) => {
if (inboxId !== this.client.inboxId) {
return
}
return await callback(
new ConsentRecord(consent.value, consent.entryType, consent.state)
)
}
)
this.subscriptions[EventTypes.Consent] = subscription
}

/**
* Cancels the stream for new consent records.
*/
cancelStreamConsent() {
if (this.subscriptions[EventTypes.Consent]) {
this.subscriptions[EventTypes.Consent].remove()
delete this.subscriptions[EventTypes.Consent]
}
XMTPModule.unsubscribeFromConsent(this.client.inboxId)
}
}
4 changes: 4 additions & 0 deletions src/lib/types/EventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export enum EventTypes {
* A new message is sent to a specific conversation
*/
ConversationMessage = 'conversationMessage',
/**
* A inboxId or conversation has been approved or denied
*/
Consent = 'consent',
}

0 comments on commit f0eb4a7

Please sign in to comment.