Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add startTime option, remove auto-fetch and auto-allow consent calls #479

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ export type PreEventCallbackOptions = {
preEnableIdentityCallback?: PreEventCallback
}

export type ConsentListOptions = {
enableConsentList: boolean
}

/**
* Aggregate type for client options. Optional properties are used when the default value is calculated on invocation, and are computed
* as needed by each function. All other defaults are specified in defaultOptions.
Expand All @@ -210,8 +206,7 @@ export type ClientOptions = Flatten<
KeyStoreOptions &
ContentOptions &
LegacyOptions &
PreEventCallbackOptions &
ConsentListOptions
PreEventCallbackOptions
>

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

if (opts?.codecs) {
Expand Down Expand Up @@ -272,14 +266,12 @@ 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

constructor(
publicKeyBundle: PublicKeyBundle,
apiClient: ApiClient,
backupClient: BackupClient,
keystore: Keystore,
enableConsentList: boolean = false
keystore: Keystore
) {
this.contacts = new Contacts(this)
this.knownPublicKeyBundles = new Map<
Expand All @@ -295,7 +287,6 @@ export default class Client<ContentTypes = any> {
this._maxContentSize = MaxContentSize
this.apiClient = apiClient
this._backupClient = backupClient
this._enableConsentList = enableConsentList
}

/**
Expand Down Expand Up @@ -340,13 +331,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)
await client.init(options)
return client
}
Expand Down
33 changes: 15 additions & 18 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,10 +174,8 @@ export class Contacts {
this.client = client
}

async refreshConsentList() {
if (this.client._enableConsentList) {
this.consentList = await ConsentList.load(this.client)
}
async refreshConsentList(startTime?: Date) {
this.consentList = await ConsentList.load(this.client, startTime)
}

isAllowed(address: string) {
Expand All @@ -190,20 +191,16 @@ export class Contacts {
}

async allow(addresses: string[]) {
if (this.client._enableConsentList) {
await ConsentList.publish(
addresses.map((address) => this.consentList.allow(address)),
this.client
)
}
await ConsentList.publish(
addresses.map((address) => this.consentList.allow(address)),
this.client
)
}

async block(addresses: string[]) {
if (this.client._enableConsentList) {
await ConsentList.publish(
addresses.map((address) => this.consentList.block(address)),
this.client
)
}
await ConsentList.publish(
addresses.map((address) => this.consentList.block(address)),
this.client
)
}
}
7 changes: 1 addition & 6 deletions src/conversations/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export default class Conversations<ContentTypes = any> {
this.listV2Conversations(),
])

// fetch allow list if enabled
this.client.contacts.refreshConsentList()

const conversations = v1Convos.concat(v2Convos)

conversations.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
Expand Down Expand Up @@ -571,9 +568,7 @@ export default class Conversations<ContentTypes = any> {
])

// add peer address to allow list
if (this.client._enableConsentList) {
this.client.contacts.allow([peerAddress])
rygine marked this conversation as resolved.
Show resolved Hide resolved
}
this.client.contacts.allow([peerAddress])

return this.conversationReferenceToV2(conversation)
}
Expand Down
1 change: 0 additions & 1 deletion src/crypto/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ async function hmacSha256Verify(key: Buffer, msg: Buffer, sig: Buffer) {
/**
* Generate a new valid private key. Will use the window.crypto or window.msCrypto as source
* depending on your browser.
*
* @returns {Buffer} A 32-byte private key.
* @function
*/
Expand Down
3 changes: 1 addition & 2 deletions src/message-backup/BackupClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import TopicStoreBackupClient from './TopicStoreBackupClient'
* Creates a backup client of the correct provider type (e.g. xmtp backup, no backup, etc).
* Uses an existing user preference from the backend if it exists, else prompts for a new
* one using the `providerSelector`
*
* @param walletAddress The public address of the user's wallet
* @param selectBackupProvider A callback for determining the provider to use, in the event there is no
* existing user preference. The app can define the policy to use here (e.g. prompt the user,
* or default to a certain provider type).
* @returns A backup client of the correct type
* @returns {Promise<BackupClient>} A backup client of the correct type
*/
export async function createBackupClient(
walletAddress: string,
Expand Down
4 changes: 0 additions & 4 deletions test/Contacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ describe('Contacts', () => {
beforeEach(async () => {
aliceClient = await Client.create(alice, {
env: 'local',
enableConsentList: true,
})
bobClient = await Client.create(bob, {
env: 'local',
enableConsentList: true,
})
carolClient = await Client.create(carol, {
env: 'local',
enableConsentList: true,
})
})

Expand Down Expand Up @@ -102,7 +99,6 @@ describe('Contacts', () => {

aliceClient = await Client.create(alice, {
env: 'local',
enableConsentList: true,
})

expect(aliceClient.contacts.consentState(bob.address)).toBe('unknown')
Expand Down
Loading