aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--client/Cargo.toml1
-rw-r--r--client/src/lib.rs59
-rw-r--r--tool/src/cli.rs3
-rw-r--r--tool/src/main.rs8
5 files changed, 63 insertions, 9 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d1985f6..ed04edd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1732,6 +1732,7 @@ dependencies = [
"anyhow",
"jellycommon",
"log",
+ "percent-encoding",
"reqwest",
"serde",
"serde_json",
diff --git a/client/Cargo.toml b/client/Cargo.toml
index 7368ac3..772de57 100644
--- a/client/Cargo.toml
+++ b/client/Cargo.toml
@@ -11,3 +11,4 @@ anyhow = "1.0.95"
serde_json = "1.0.138"
serde = { version = "1.0.217", features = ["derive"] }
tokio = { workspace = true }
+percent-encoding = "2.3.1"
diff --git a/client/src/lib.rs b/client/src/lib.rs
index e0ab440..96c39b6 100644
--- a/client/src/lib.rs
+++ b/client/src/lib.rs
@@ -4,8 +4,12 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use anyhow::Result;
-use jellycommon::user::CreateSessionParams;
+use jellycommon::{
+ api::{ApiHomeResponse, ApiNodeResponse, ApiSearchResponse},
+ user::CreateSessionParams,
+};
use log::{debug, info};
+use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::{
header::{HeaderMap, HeaderValue},
Client,
@@ -71,11 +75,50 @@ impl Session {
format!("session={}", self.session_token)
}
- pub async fn node(&self, id: NodeIDOrSlug) -> Result<Node> {
+ pub async fn node(
+ &self,
+ id: NodeIDOrSlug,
+ children: bool,
+ parents: bool,
+ ) -> Result<ApiNodeResponse> {
debug!("downloading node {id}");
+ let params = match (children, parents) {
+ (true, true) => "?children&parents",
+ (true, false) => "?children",
+ (false, true) => "?parents",
+ (false, false) => "",
+ };
+ Ok(self
+ .client
+ .get(format!("{}/n/{id}{params}", self.instance.base(),))
+ .send()
+ .await?
+ .error_for_status()?
+ .json()
+ .await?)
+ }
+
+ pub async fn search(&self, query: &str, page: usize) -> Result<ApiSearchResponse> {
+ debug!("searching for {query:?}");
+ Ok(self
+ .client
+ .get(format!(
+ "{}/search?query={}&page={page}",
+ utf8_percent_encode(query, NON_ALPHANUMERIC),
+ self.instance.base(),
+ ))
+ .send()
+ .await?
+ .error_for_status()?
+ .json()
+ .await?)
+ }
+
+ pub async fn home(&self) -> Result<ApiHomeResponse> {
+ debug!("home page");
Ok(self
.client
- .get(format!("{}/n/{id}", self.instance.base()))
+ .get(format!("{}/home", self.instance.base(),))
.send()
.await?
.error_for_status()?
@@ -105,7 +148,7 @@ impl Session {
debug!("downloading asset {token:?} (w={width})");
self.download_url(
writer,
- format!("{}/asset/{token}?width={width}", self.instance.base(),),
+ format!("{}/asset/{token}?width={width}", self.instance.base()),
)
.await
}
@@ -138,11 +181,13 @@ impl Session {
Ok(())
}
- pub async fn reimport(&self) -> Result<()> {
- // TODO error handling
+ pub async fn reimport(&self, incremental: bool) -> Result<()> {
info!("reimport");
self.client
- .post(format!("{}/admin/import", self.instance.base()))
+ .post(format!(
+ "{}/admin/import?incremental={incremental}",
+ self.instance.base()
+ ))
.send()
.await?;
info!("done");
diff --git a/tool/src/cli.rs b/tool/src/cli.rs
index b51b135..a01c9dd 100644
--- a/tool/src/cli.rs
+++ b/tool/src/cli.rs
@@ -34,6 +34,9 @@ pub enum Action {
/// Disable TLS. Dont use this.
#[arg(long)]
no_tls: bool,
+ /// Perform full import
+ #[arg(long)]
+ no_incremental: bool,
},
}
diff --git a/tool/src/main.rs b/tool/src/main.rs
index 0a0e84e..9ce7cf1 100644
--- a/tool/src/main.rs
+++ b/tool/src/main.rs
@@ -30,7 +30,11 @@ fn main() -> anyhow::Result<()> {
.unwrap()
.block_on(add(a)),
a @ Action::Migrate { .. } => migrate(a),
- Action::Reimport { hostname, no_tls } => tokio::runtime::Builder::new_multi_thread()
+ Action::Reimport {
+ hostname,
+ no_tls,
+ no_incremental,
+ } => tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
@@ -52,7 +56,7 @@ fn main() -> anyhow::Result<()> {
})
.await?;
- session.reimport().await?;
+ session.reimport(!no_incremental).await?;
Ok(())
}),
}