Skip to content

Commit

Permalink
implement search for AddressBookIdentityProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
topocount committed Sep 23, 2019
1 parent 4aa6065 commit e9ee7d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const addressBookAppIds = [
/**
* An identity provider for Address Book Entries
*
* @class AddressIdentityProvider
* @extends AddressIdentityProvider
*/
export default class AddressBookIdentityProvider extends AddressIdentityProvider {
constructor (apps, cache) {
Expand Down Expand Up @@ -51,6 +51,45 @@ export default class AddressBookIdentityProvider extends AddressIdentityProvider
}, null)
}

async search (searchTerm = '') {
const isAddressSearch = searchTerm.substring(0, 2).toLowerCase() === '0x'
const identities = await this.getAll()
console.log('identities ', identities)
const results = Object.entries(identities)
.filter(
([address, { name }]) =>
(isAddressSearch &&
searchTerm.length > 3 &&
address.toLowerCase().indexOf(searchTerm.toLowerCase()) === 0) ||
name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1
)
.map(([address, { name }]) => ({ name, address }))
return results
}

/**
* get all identities from all installed address book instances
*/
async getAll () {
const addressBookApps = await this.apps.pipe(
first(),
map(apps => apps.filter(app => addressBookAppIds.includes(app.appId)))
).toPromise()

return addressBookApps.reduce(async (allEntries, app) => {
console.log("app: ", app)
const cacheKey = getCacheKey(app.proxyAddress, 'state')
const { entries = [] } = await this.cache.get(cacheKey)
const allEntriesResolved = await allEntries
const entriesObject = entries.reduce((obj, entry) => {
return { ...obj, [entry.addr.toLowerCase()]: entry.data }
}, {})
console.log('entries:', entriesObject, 'all entries: ', allEntries)
// ensure the entries retrieved from the first-installed address book aren't overwritten
return { ...entriesObject, ...allEntriesResolved }
}, Promise.resolve({}))
}

/**
* Modify the identity metadata of an address
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ test.before(async t => {

cache = new Cache('stubbedAddressBook')
await cache.init()
cache.set('0x0.state', { entries: [{ addr: '0x3', data: { name: 'testEntity' } }] })
cache.set('0x11.state', { entries: [{ addr: '0x3', data: { name: 'testEntity2' } }] })
cache.set('0x0.state', { entries: [{ addr: '0x3a', data: { name: 'testEntity' } }, { addr: '0x33', data: { name: 'testDude' } } ] })
cache.set('0x11.state', { entries: [{ addr: '0x3a', data: { name: 'testEntity2' } }] })
})

test.beforeEach(async t => {
Expand All @@ -33,7 +33,7 @@ test.beforeEach(async t => {

test('should resolve identity from first address book in app array', async t => {
const provider = t.context.addressBookIdentityProvider
const identityMetadata = await provider.resolve('0x3')
const identityMetadata = await provider.resolve('0x3a')
t.is(identityMetadata.name, 'testEntity')
})

Expand All @@ -47,3 +47,29 @@ test('should throw error on any modify attempt', async t => {
const provider = t.context.addressBookIdentityProvider
await t.throwsAsync(() => provider.modify('0x9', { name: 'newEntity' }))
})

test('getAll should return a combined Object containing all entries', async t => {
const provider = t.context.addressBookIdentityProvider
const allIdentities = await provider.getAll()
t.deepEqual(allIdentities, {
'0x3a': { name: 'testEntity' },
'0x33': { name: 'testDude' }
})
})

test('search should return an aray of results of freely matching identities', async t => {
t.plan(3)
const provider = t.context.addressBookIdentityProvider
let result = await provider.search('0x3a')
console.log('result: ', result)
t.deepEqual(result, [ { name: 'testEntity', address: '0x3a' } ])

result = await provider.search('test')
console.log('next result', result)
t.deepEqual(result, [ { name: 'testEntity', address: '0x3a' },
{ name: 'testDude', address: '0x33' } ])

result = await provider.search('testd')
console.log('next result', result)
t.deepEqual(result, [{ name: 'testDude', address: '0x33' } ])
})

0 comments on commit e9ee7d4

Please sign in to comment.