From 34ce659b208f40847341aeb9e85ce76d6f0a4db9 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 1 Jan 2024 15:25:23 +0000 Subject: [PATCH] Support retrieving the Instance object. Not much of it is decoded at the moment, but we'll need a few pieces of configuration to inform the post composer about important things like how to tell when it's hit its length limit. --- src/client.rs | 29 ++++++++++++++++++++++++++--- src/types.rs | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index a56a8d6..14fbed5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -54,6 +54,7 @@ pub struct Client { statuses: HashMap, notifications: HashMap, feeds: HashMap, + instance: Option, permit_write: bool, } @@ -159,6 +160,7 @@ impl Client { statuses: HashMap::new(), notifications: HashMap::new(), feeds: HashMap::new(), + instance: None, permit_write: false, }) } @@ -209,16 +211,37 @@ impl Client { self.api_request_cl(&self.client, req) } - pub fn cache_account(&mut self, ac: &Account) { + pub fn instance(&mut self) -> Result { + if let Some(ref inst) = self.instance { + return Ok(inst.clone()); + } + + let (url, req) = self.api_request(Req::get("instance"))?; + let rsp = req.send()?; + let rspstatus = rsp.status(); + let inst: Instance = if !rspstatus.is_success() { + Err(ClientError::UrlError(url.clone(), rspstatus.to_string())) + } else { + match serde_json::from_str(&rsp.text()?) { + Ok(ac) => Ok(ac), + Err(e) => Err(ClientError::UrlError( + url.clone(), e.to_string())), + } + }?; + self.instance = Some(inst.clone()); + Ok(inst) + } + + fn cache_account(&mut self, ac: &Account) { self.accounts.insert(ac.id.to_string(), ac.clone()); } - pub fn cache_status(&mut self, st: &Status) { + fn cache_status(&mut self, st: &Status) { self.cache_account(&st.account); self.statuses.insert(st.id.to_string(), st.clone()); } - pub fn cache_notification(&mut self, n: &Notification) { + fn cache_notification(&mut self, n: &Notification) { self.cache_account(&n.account); if let Some(st) = &n.status { self.cache_status(&st); diff --git a/src/types.rs b/src/types.rs index d708f23..ef6a454 100644 --- a/src/types.rs +++ b/src/types.rs @@ -230,3 +230,27 @@ pub struct Relationship { pub endorsed: bool, pub note: String, } + +#[derive(Deserialize, Debug, Clone)] +pub struct InstanceStatusConfig { + pub max_characters: usize, + pub max_media_attachments: usize, + pub characters_reserved_per_url: usize, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct InstanceConfig { + pub statuses: InstanceStatusConfig, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Instance { + // At least one major live instance (hachyderm.io) uses the key + // "uri" for its instance domain. But the Mastodon test instance + // uses "domain". Bah. + #[serde(alias="uri")] pub domain: Option, + + pub configuration: InstanceConfig, + + // FIXME: lots of things are missing from here! +} -- 2.30.2