chiark / gitweb /
Client request to update your account details.
authorSimon Tatham <anakin@pobox.com>
Sat, 13 Jan 2024 10:30:26 +0000 (10:30 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 13 Jan 2024 13:15:58 +0000 (13:15 +0000)
src/client.rs

index bd771f2b9b1e5187229dd4f5f59dbcccb24660eb..ee4e61b4238dba331a4e23bc6b2bae20aa3808e5 100644 (file)
@@ -84,6 +84,21 @@ impl Followness {
     }
 }
 
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub struct AccountDetails {
+    pub display_name: String,
+    // pub bio: String,
+    pub default_visibility: Visibility,
+    pub default_sensitive: bool,
+    pub default_language: Option<String>,
+    pub locked: bool,
+    pub bot: bool,
+    pub discoverable: bool,
+    pub hide_collections: bool,
+    pub indexable: bool,
+    // pub fields: Vec<(String, String)>,
+}
+
 #[derive(Debug, PartialEq, Eq, Clone, Copy)]
 pub enum AccountFlag { Block, Mute }
 
@@ -166,6 +181,14 @@ impl Req {
         }
     }
 
+    pub fn patch(url_suffix: &str) -> Self {
+        Req {
+            method: reqwest::Method::PATCH,
+            url_suffix: url_suffix.to_owned(),
+            parameters: Vec::new(),
+        }
+    }
+
     pub fn param<T>(mut self, key: &str, value: T) -> Self
         where T: ReqParam
     {
@@ -1264,4 +1287,44 @@ impl Client {
             Ok(())
         }
     }
+
+    pub fn set_account_details(&mut self, id: &str, details: AccountDetails)
+                               -> Result<(), ClientError>
+    {
+        // TODO: add "note" with details.bio, and "fields_attributes"
+        // for the variable info fields
+        let req = Req::patch("v1/accounts/update_credentials")
+            .param("display_name", &details.display_name)
+            .param("locked", details.locked)
+            .param("bot", details.bot)
+            .param("hide_collections", details.hide_collections)
+            .param("indexable", details.indexable)
+            .param("discoverable", details.discoverable)
+            .param("source[privacy]", details.default_visibility)
+            .param("source[sensitive]", details.default_sensitive);
+        let req = if let Some(ref lang) = details.default_language {
+            req.param("source[language]", lang)
+        } else {
+            req
+        };
+
+        let (url, rsp) = self.api_request(req)?;
+        let rspstatus = rsp.status();
+        let ac: Account = 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())),
+            }
+        }?;
+        if ac.id != id {
+            return Err(ClientError::UrlError(
+                url, format!("request returned wrong account id {}",
+                                     &ac.id)));
+        }
+        self.cache_account(&ac);
+        Ok(())
+    }
 }