From 5eb54402d25a4444d7f3f26630635f4a80f58edb Mon Sep 17 00:00:00 2001 From: nazeh Date: Wed, 7 Aug 2024 19:05:29 +0300 Subject: [PATCH] feat(js): add list shallow option --- pubky/pkg/test/public.js | 68 ++++++++++++++++++++++++++++++++ pubky/src/shared/list_builder.rs | 8 ++-- pubky/src/shared/public.rs | 12 +++--- pubky/src/wasm.rs | 3 ++ 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/pubky/pkg/test/public.js b/pubky/pkg/test/public.js index 9ceaa5b..4b301e7 100644 --- a/pubky/pkg/test/public.js +++ b/pubky/pkg/test/public.js @@ -243,3 +243,71 @@ test("list", async (t) => { ); } }) + +test('list shallow', async (t) => { + const client = PubkyClient.testnet(); + + const keypair = Keypair.random() + const publicKey = keypair.publicKey() + const pubky = publicKey.z32() + + const homeserver = PublicKey.from('8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo') + await client.signup(keypair, homeserver) + + + + let urls = [ + `pubky://${pubky}/pub/a.com/a.txt`, + `pubky://${pubky}/pub/example.com/a.txt`, + `pubky://${pubky}/pub/example.com/b.txt`, + `pubky://${pubky}/pub/example.com/c.txt`, + `pubky://${pubky}/pub/example.com/d.txt`, + `pubky://${pubky}/pub/example.con/d.txt`, + `pubky://${pubky}/pub/example.con`, + `pubky://${pubky}/pub/file`, + `pubky://${pubky}/pub/file2`, + `pubky://${pubky}/pub/z.com/a.txt`, + ] + + for (let url of urls) { + await client.put(url, Buffer.from("")); + } + + let url = `pubky://${pubky}/pub/`; + + { + let list = await client.list(url, null, false, null, true); + + t.deepEqual( + list, + [ + `pubky://${pubky}/pub/a.com/`, + `pubky://${pubky}/pub/example.com/`, + `pubky://${pubky}/pub/example.con`, + `pubky://${pubky}/pub/example.con/`, + `pubky://${pubky}/pub/file`, + `pubky://${pubky}/pub/file2`, + `pubky://${pubky}/pub/z.com/`, + ], + "normal list shallow" + ); + } + + { + let list = await client.list(url, null, true, null, true); + + t.deepEqual( + list, + [ + `pubky://${pubky}/pub/z.com/`, + `pubky://${pubky}/pub/file2`, + `pubky://${pubky}/pub/file`, + `pubky://${pubky}/pub/example.con/`, + `pubky://${pubky}/pub/example.con`, + `pubky://${pubky}/pub/example.com/`, + `pubky://${pubky}/pub/a.com/`, + ], + "normal list shallow" + ); + } +}) diff --git a/pubky/src/shared/list_builder.rs b/pubky/src/shared/list_builder.rs index 5eccc7e..8c5cde0 100644 --- a/pubky/src/shared/list_builder.rs +++ b/pubky/src/shared/list_builder.rs @@ -27,8 +27,8 @@ impl<'a> ListBuilder<'a> { } /// Set the `reverse` option. - pub fn reverse(mut self) -> Self { - self.reverse = true; + pub fn reverse(mut self, reverse: bool) -> Self { + self.reverse = reverse; self } @@ -46,8 +46,8 @@ impl<'a> ListBuilder<'a> { self } - pub fn shallow(mut self) -> Self { - self.shallow = true; + pub fn shallow(mut self, shallow: bool) -> Self { + self.shallow = shallow; self } diff --git a/pubky/src/shared/public.rs b/pubky/src/shared/public.rs index 0f74874..9bd099f 100644 --- a/pubky/src/shared/public.rs +++ b/pubky/src/shared/public.rs @@ -319,7 +319,7 @@ mod tests { let list = client .list(url.as_str()) .unwrap() - .reverse() + .reverse(true) .send() .await .unwrap(); @@ -340,7 +340,7 @@ mod tests { let list = client .list(url.as_str()) .unwrap() - .reverse() + .reverse(true) .limit(2) .send() .await @@ -360,7 +360,7 @@ mod tests { let list = client .list(url.as_str()) .unwrap() - .reverse() + .reverse(true) .limit(2) .cursor("d.txt") .send() @@ -412,7 +412,7 @@ mod tests { let list = client .list(url.as_str()) .unwrap() - .shallow() + .shallow(true) .send() .await .unwrap(); @@ -436,8 +436,8 @@ mod tests { let list = client .list(url.as_str()) .unwrap() - .shallow() - .reverse() + .shallow(true) + .reverse(true) .send() .await .unwrap(); diff --git a/pubky/src/wasm.rs b/pubky/src/wasm.rs index 2e54332..07b6749 100644 --- a/pubky/src/wasm.rs +++ b/pubky/src/wasm.rs @@ -165,6 +165,7 @@ impl PubkyClient { cursor: Option, reverse: Option, limit: Option, + shallow: Option, ) -> Result { // TODO: try later to return Vec from async function. @@ -174,6 +175,7 @@ impl PubkyClient { .reverse(reverse.unwrap_or(false)) .limit(limit.unwrap_or(u16::MAX)) .cursor(&cursor) + .shallow(shallow.unwrap_or(false)) .send() .await .map(|urls| { @@ -191,6 +193,7 @@ impl PubkyClient { self.inner_list(url)? .reverse(reverse.unwrap_or(false)) .limit(limit.unwrap_or(u16::MAX)) + .shallow(shallow.unwrap_or(false)) .send() .await .map(|urls| {