diff --git a/src/lib/ConsentRecord.ts b/src/lib/ConsentRecord.ts index ac93b1bee..607b8b343 100644 --- a/src/lib/ConsentRecord.ts +++ b/src/lib/ConsentRecord.ts @@ -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 { diff --git a/src/lib/PrivatePreferences.ts b/src/lib/PrivatePreferences.ts index b019fb480..db881b791 100644 --- a/src/lib/PrivatePreferences.ts +++ b/src/lib/PrivatePreferences.ts @@ -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 + private subscriptions: { [key: string]: { remove: () => void } } = {} constructor(client: Client) { this.client = client @@ -31,16 +33,55 @@ export default class PrivatePreferences { ) } - async setConsentState(consentEntry: ConsentListEntry): Promise { + async setConsentState(consentRecord: ConsentRecord): Promise { return await XMTPModule.setConsentState( this.client.inboxId, - consentEntry.value, - consentEntry.entryType, - consentEntry.permissionType + consentRecord.value, + consentRecord.entryType, + consentRecord.permissionType ) } async syncConsent(): Promise { return await XMTPModule.syncConsent(this.client.inboxId) } + + /** + * This method streams consent. + * @returns {Promise} A Promise that resolves to an array of ConsentRecord objects. + */ + async streamConsent( + callback: (consent: ConsentRecord) => Promise + ): Promise { + 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) + } } diff --git a/src/lib/types/EventTypes.ts b/src/lib/types/EventTypes.ts index f6cbed4b3..9f2c0d3f3 100644 --- a/src/lib/types/EventTypes.ts +++ b/src/lib/types/EventTypes.ts @@ -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', }