Skip to content

Commit

Permalink
Refactor action processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Sep 6, 2024
1 parent 29cb855 commit 8b74f03
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 87 deletions.
19 changes: 9 additions & 10 deletions packages/js-sdk/src/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,30 @@ export class ConsentList {
* Process actions and update internal consent list
*/
processActions(actionsMap: ActionsMap) {
const entries: ConsentListEntry[] = []
// actions to process
const actions = Array.from(actionsMap.values())

// update the consent list
actions.forEach((action) => {
action.allowAddress?.walletAddresses.forEach((address) => {
entries.push(this.allow(address))
this.allow(address)
})
action.denyAddress?.walletAddresses.forEach((address) => {
entries.push(this.deny(address))
this.deny(address)
})
action.allowGroup?.groupIds.forEach((groupId) => {
entries.push(this.allowGroup(groupId))
this.allowGroup(groupId)
})
action.denyGroup?.groupIds.forEach((groupId) => {
entries.push(this.denyGroup(groupId))
this.denyGroup(groupId)
})
action.allowInboxId?.inboxIds.forEach((inboxId) => {
entries.push(this.allowInboxId(inboxId))
this.allowInboxId(inboxId)
})
action.denyInboxId?.inboxIds.forEach((inboxId) => {
entries.push(this.denyInboxId(inboxId))
this.denyInboxId(inboxId)
})
})

return entries
}

async stream(onConnectionLost?: OnConnectionLostCallback) {
Expand Down Expand Up @@ -261,7 +258,9 @@ export class ConsentList {
this.reset()

// process actions and update consent list
return this.processActions(actionsMap)
this.processActions(actionsMap)

return this.entries
}

async publish(entries: ConsentListEntry[]) {
Expand Down
93 changes: 16 additions & 77 deletions packages/js-sdk/test/Contacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Contacts', () => {
it('should retrieve consent state', async () => {
const entries = await bobClient.contacts.refreshConsentList()

expect(entries.length).toBe(0)
expect(entries.size).toBe(0)

await bobClient.contacts.deny([alice.address])
await bobClient.contacts.allow([carol.address])
Expand All @@ -143,6 +143,7 @@ describe('Contacts', () => {
await bobClient.contacts.allowGroups(['foo', 'bar'])
await bobClient.contacts.denyGroups(['foo'])
await bobClient.contacts.allowGroups(['foo'])
await bobClient.contacts.denyGroups(['bar'])
await bobClient.contacts.allowInboxes(['baz', 'qux'])
await bobClient.contacts.denyInboxes(['baz'])
await bobClient.contacts.allowInboxes(['baz'])
Expand All @@ -158,86 +159,24 @@ describe('Contacts', () => {
expect(bobClient.contacts.inboxConsentState('baz')).toBe('unknown')
expect(bobClient.contacts.inboxConsentState('qux')).toBe('unknown')

const latestEntries = await bobClient.contacts.refreshConsentList()

expect(latestEntries.length).toBe(14)
expect(latestEntries).toEqual([
{
entryType: 'address',
permissionType: 'denied',
value: alice.address,
},
{
entryType: 'address',
permissionType: 'allowed',
value: carol.address,
},
{
entryType: 'address',
permissionType: 'allowed',
value: alice.address,
},
{
entryType: 'address',
permissionType: 'denied',
value: carol.address,
},
{
entryType: 'address',
permissionType: 'denied',
value: alice.address,
},
{
entryType: 'address',
permissionType: 'allowed',
value: carol.address,
},
{
entryType: 'groupId',
permissionType: 'allowed',
value: 'foo',
},
{
entryType: 'groupId',
permissionType: 'allowed',
value: 'bar',
},
{
entryType: 'groupId',
permissionType: 'denied',
value: 'foo',
},
{
entryType: 'groupId',
permissionType: 'allowed',
value: 'foo',
},
{
entryType: 'inboxId',
permissionType: 'allowed',
value: 'baz',
},
{
entryType: 'inboxId',
permissionType: 'allowed',
value: 'qux',
},
{
entryType: 'inboxId',
permissionType: 'denied',
value: 'baz',
},
{
entryType: 'inboxId',
permissionType: 'allowed',
value: 'baz',
},
])
const latestEntries = await bobClient.contacts.loadConsentList()

expect(latestEntries.size).toBe(6)
expect(latestEntries).toEqual(
new Map([
[`address-${alice.address}`, 'denied'],
[`address-${carol.address}`, 'allowed'],
[`groupId-foo`, 'allowed'],
[`groupId-bar`, 'denied'],
[`inboxId-baz`, 'allowed'],
[`inboxId-qux`, 'allowed'],
])
)

expect(bobClient.contacts.consentState(alice.address)).toBe('denied')
expect(bobClient.contacts.consentState(carol.address)).toBe('allowed')
expect(bobClient.contacts.groupConsentState('foo')).toBe('allowed')
expect(bobClient.contacts.groupConsentState('bar')).toBe('allowed')
expect(bobClient.contacts.groupConsentState('bar')).toBe('denied')
expect(bobClient.contacts.inboxConsentState('baz')).toBe('allowed')
expect(bobClient.contacts.inboxConsentState('qux')).toBe('allowed')
})
Expand Down

0 comments on commit 8b74f03

Please sign in to comment.