Skip to content

Commit

Permalink
fix: add startTime option for loading consent list
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Oct 27, 2023
1 parent cc89dbb commit 0d92c5c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
22 changes: 8 additions & 14 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export type PreEventCallbackOptions = {
preEnableIdentityCallback?: PreEventCallback
}

export type ConsentListOptions = {
enableConsentList: boolean
export type ConsentOptions = {
enableConsent: boolean
}

/**
Expand All @@ -211,7 +211,7 @@ export type ClientOptions = Flatten<
ContentOptions &
LegacyOptions &
PreEventCallbackOptions &
ConsentListOptions
ConsentOptions
>

/**
Expand All @@ -234,7 +234,7 @@ export function defaultOptions(opts?: Partial<ClientOptions>): ClientOptions {
disablePersistenceEncryption: false,
keystoreProviders: defaultKeystoreProviders(),
apiClientFactory: createHttpApiClientFromOptions,
enableConsentList: false,
enableConsent: false,
}

if (opts?.codecs) {
Expand Down Expand Up @@ -272,14 +272,14 @@ export default class Client<ContentTypes = any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _codecs: Map<string, ContentCodec<any>>
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<
Expand All @@ -295,7 +295,7 @@ export default class Client<ContentTypes = any> {
this._maxContentSize = MaxContentSize
this.apiClient = apiClient
this._backupClient = backupClient
this._enableConsentList = enableConsentList
this.consentEnabled = enableConsent
}

/**
Expand Down Expand Up @@ -340,13 +340,7 @@ export default class Client<ContentTypes = any> {
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
}
Expand Down
17 changes: 10 additions & 7 deletions src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ export class ConsentList {
return this._identifier
}

static async load(client: Client): Promise<ConsentList> {
static async load(client: Client, startTime?: Date): Promise<ConsentList> {
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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/conversations/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ export default class Conversations<ContentTypes = any> {
])

// add peer address to allow list
if (this.client._enableConsentList) {
if (this.client.consentEnabled) {
this.client.contacts.allow([peerAddress])
}

Expand Down
8 changes: 4 additions & 4 deletions test/Contacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 0d92c5c

Please sign in to comment.