chiark / gitweb /
Support retrieving the Instance object.
authorSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 15:25:23 +0000 (15:25 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 15:47:32 +0000 (15:47 +0000)
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
src/types.rs

index a56a8d605b7ce3e8912a5f35b850a57de9b95fba..14fbed536c78fab7de6a73be089f3128d1ea88a8 100644 (file)
@@ -54,6 +54,7 @@ pub struct Client {
     statuses: HashMap<String, Status>,
     notifications: HashMap<String, Notification>,
     feeds: HashMap<FeedId, Feed>,
+    instance: Option<Instance>,
     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<Instance, ClientError> {
+        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);
index d708f2303ba41832c4a0daeab9bc0d73487f8ed4..ef6a4549b2289afa950c34d86b7c625117a80187 100644 (file)
@@ -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<String>,
+
+    pub configuration: InstanceConfig,
+
+    // FIXME: lots of things are missing from here!
+}