Skip to content

Commit

Permalink
Merge pull request #97 from WebOfTrust/feat/messages
Browse files Browse the repository at this point in the history
Feat/messages
  • Loading branch information
rodolfomiranda authored Aug 28, 2023
2 parents 33ded8e + fe7d738 commit 2b82aba
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
57 changes: 57 additions & 0 deletions src/keri/app/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,14 @@ export class SignifyClient {
escrows(): Escrows {
return new Escrows(this)
}

/**
* Get messages resource
* @returns {Messages}
* */
messages(): Messages {
return new Messages(this)
}
}

/** Arguments required to create an identfier */
Expand Down Expand Up @@ -1801,3 +1809,52 @@ export class Escrows {
return await res.json()
}
}


/**
* Messages
*/

export class Messages {
client: SignifyClient

/**
* Messages
* @param {SignifyClient} client
*/
constructor(client: SignifyClient) {
this.client = client
}

/**
* List messages
* @async
* @param {string} [sender] sender prefix to filter messages
* @param {string} [recipient] recipient prefix to filter messages
* @param {number} [start=0] Start index of list of messages, defaults to 0
* @param {number} [end=9] End index of list of messages, defaults to 9
*/
async list(sender:string, recipient:string, start:number=0, end:number=9): Promise<any> {
let extraHeaders = new Headers()
extraHeaders.append('Range', `messages=${start}-${end}`)
let params = new URLSearchParams()
//check sender and recipient are send otherwise return error
if (sender !== undefined) {params.append('sender', sender)}
if (recipient !== undefined) {params.append('recipient', recipient)}


let path = `/messages`+ '?' + params.toString()
let method = 'GET'
let res = await this.client.fetch(path, method, null, extraHeaders)
let cr = res.headers.get('content-range')
let range = parseRangeHeaders(cr,"messages")
let msgs = await res.json()

return {
start: range.start,
end: range.end,
total: range.total,
messages: msgs
}
}
}
21 changes: 20 additions & 1 deletion test/app/signify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,5 +723,24 @@ describe('SignifyClient', () => {
assert.equal(lastCall[1]!.method,'GET')

})


it('Messages', async () => {
await libsodium.ready;
const bran = "0123456789abcdefghijk"

let client = new SignifyClient(url,bran,Tier.low,boot_url)

await client.boot()
await client.connect()

let messages = client.messages()

await messages.list('ELI7pg979AdhmvrjDeam2eAO2SR5niCgnjAJXJHtJose','EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1',0, 10)
let lastCall = fetchMock.mock.calls[fetchMock.mock.calls.length-1]!
assert.equal(lastCall[0]!,url+'/messages?sender=ELI7pg979AdhmvrjDeam2eAO2SR5niCgnjAJXJHtJose&recipient=EMQQpnSkgfUOgWdzQTWfrgiVHKIDAhvAZIPQ6z3EAfz1')
assert.equal(lastCall[1]!.method,'GET')

let lastHeaders = new Headers((lastCall[1]!.headers!))
assert.equal(lastHeaders.get('Range'),'messages=0-10')
})
})

0 comments on commit 2b82aba

Please sign in to comment.