From 0d92c5c8f0a20c0cdec0c9bbbae339c37a89fe43 Mon Sep 17 00:00:00 2001 From: Ry Racherbaumer Date: Fri, 27 Oct 2023 13:05:00 -0500 Subject: [PATCH] fix: add `startTime` option for loading consent list --- src/Client.ts | 22 ++++++++-------------- src/Contacts.ts | 17 ++++++++++------- src/conversations/Conversations.ts | 2 +- test/Contacts.test.ts | 8 ++++---- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index cd336cbe8..e2e17dded 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -197,8 +197,8 @@ export type PreEventCallbackOptions = { preEnableIdentityCallback?: PreEventCallback } -export type ConsentListOptions = { - enableConsentList: boolean +export type ConsentOptions = { + enableConsent: boolean } /** @@ -211,7 +211,7 @@ export type ClientOptions = Flatten< ContentOptions & LegacyOptions & PreEventCallbackOptions & - ConsentListOptions + ConsentOptions > /** @@ -234,7 +234,7 @@ export function defaultOptions(opts?: Partial): ClientOptions { disablePersistenceEncryption: false, keystoreProviders: defaultKeystoreProviders(), apiClientFactory: createHttpApiClientFromOptions, - enableConsentList: false, + enableConsent: false, } if (opts?.codecs) { @@ -272,14 +272,14 @@ export default class Client { // eslint-disable-next-line @typescript-eslint/no-explicit-any private _codecs: Map> private _maxContentSize: number - readonly _enableConsentList: boolean + readonly consentEnabled: boolean constructor( publicKeyBundle: PublicKeyBundle, apiClient: ApiClient, backupClient: BackupClient, keystore: Keystore, - enableConsentList: boolean = false + enableConsent: boolean = false ) { this.contacts = new Contacts(this) this.knownPublicKeyBundles = new Map< @@ -295,7 +295,7 @@ export default class Client { this._maxContentSize = MaxContentSize this.apiClient = apiClient this._backupClient = backupClient - this._enableConsentList = enableConsentList + this.consentEnabled = enableConsent } /** @@ -340,13 +340,7 @@ export default class Client { const backupClient = await Client.setupBackupClient(address, options.env) const client = new Client< ExtractDecodedType<[...ContentCodecs, TextCodec][number]> | undefined - >( - publicKeyBundle, - apiClient, - backupClient, - keystore, - opts?.enableConsentList - ) + >(publicKeyBundle, apiClient, backupClient, keystore, opts?.enableConsent) await client.init(options) return client } diff --git a/src/Contacts.ts b/src/Contacts.ts index 74bfa6d7f..c51d1dca8 100644 --- a/src/Contacts.ts +++ b/src/Contacts.ts @@ -67,14 +67,17 @@ export class ConsentList { return this._identifier } - static async load(client: Client): Promise { + static async load(client: Client, startTime?: Date): Promise { const consentList = new ConsentList() const identifier = await this.getIdentifier(client) const contentTopic = buildUserPrivatePreferencesTopic(identifier) const messages = await client.listEnvelopes( contentTopic, - async ({ message }: EnvelopeWithMessage) => message + async ({ message }: EnvelopeWithMessage) => message, + { + startTime, + } ) // decrypt messages @@ -171,9 +174,9 @@ export class Contacts { this.client = client } - async refreshConsentList() { - if (this.client._enableConsentList) { - this.consentList = await ConsentList.load(this.client) + async refreshConsentList(startTime?: Date) { + if (this.client.consentEnabled) { + this.consentList = await ConsentList.load(this.client, startTime) } } @@ -190,7 +193,7 @@ export class Contacts { } async allow(addresses: string[]) { - if (this.client._enableConsentList) { + if (this.client.consentEnabled) { await ConsentList.publish( addresses.map((address) => this.consentList.allow(address)), this.client @@ -199,7 +202,7 @@ export class Contacts { } async block(addresses: string[]) { - if (this.client._enableConsentList) { + if (this.client.consentEnabled) { await ConsentList.publish( addresses.map((address) => this.consentList.block(address)), this.client diff --git a/src/conversations/Conversations.ts b/src/conversations/Conversations.ts index 74433ed02..597965b7e 100644 --- a/src/conversations/Conversations.ts +++ b/src/conversations/Conversations.ts @@ -571,7 +571,7 @@ export default class Conversations { ]) // add peer address to allow list - if (this.client._enableConsentList) { + if (this.client.consentEnabled) { this.client.contacts.allow([peerAddress]) } diff --git a/test/Contacts.test.ts b/test/Contacts.test.ts index c19839d0b..451c28c63 100644 --- a/test/Contacts.test.ts +++ b/test/Contacts.test.ts @@ -14,15 +14,15 @@ describe('Contacts', () => { beforeEach(async () => { aliceClient = await Client.create(alice, { env: 'local', - enableConsentList: true, + enableConsent: true, }) bobClient = await Client.create(bob, { env: 'local', - enableConsentList: true, + enableConsent: true, }) carolClient = await Client.create(carol, { env: 'local', - enableConsentList: true, + enableConsent: true, }) }) @@ -102,7 +102,7 @@ describe('Contacts', () => { aliceClient = await Client.create(alice, { env: 'local', - enableConsentList: true, + enableConsent: true, }) expect(aliceClient.contacts.consentState(bob.address)).toBe('unknown')