chiark / gitweb /
Show account relationships.
authorSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 11:58:03 +0000 (11:58 +0000)
committerSimon Tatham <anakin@pobox.com>
Mon, 1 Jan 2024 11:58:03 +0000 (11:58 +0000)
src/client.rs
src/text.rs
src/types.rs

index f5c96956a9553ad3937d982b5ff2250632247349..6a68b903d5fe1adf07a45cc330cf733232a1e009 100644 (file)
@@ -253,6 +253,32 @@ impl Client {
         Ok(ac)
     }
 
+    pub fn account_relationship_by_id(&mut self, id: &str) ->
+        Result<Relationship, ClientError>
+    {
+        let (url, req) = self.api_request(
+            Req::get("accounts/relationships").param("id", id))?;
+        let rsp = req.send()?;
+        let rspstatus = rsp.status();
+        let rels: Vec<Relationship> = if !rspstatus.is_success() {
+            Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
+        } else {
+            match serde_json::from_str(dbg!(&rsp.text()?)) {
+                Ok(ac) => Ok(ac),
+                Err(e) => Err(ClientError::UrlError(
+                    url.clone(), e.to_string())),
+            }
+        }?;
+        for rel in rels {
+            if rel.id == id {
+                return Ok(rel);
+            }
+        }
+        Err(ClientError::UrlError(
+            url.clone(), format!(
+                "request did not return expected account id {}", id)))
+    }
+
     pub fn status_by_id(&mut self, id: &str) -> Result<Status, ClientError> {
         if let Some(st) = self.statuses.get(id) {
             let mut st = st.clone();
index 4cfcb81b89ae782f487ea8413edc5a8b95f89c2f..1833dbd4465c48dbc17a3046771841dea47ae0a5 100644 (file)
@@ -1492,6 +1492,7 @@ pub struct ExamineUserDisplay {
     post_count: Paragraph,
     followers_count: Paragraph,
     following_count: Paragraph,
+    relationships: Vec<Paragraph>,
     blank: BlankLine,
 }
 
@@ -1544,6 +1545,77 @@ impl ExamineUserDisplay {
             .add(&ColouredString::plain(
                 &format!("Number of users followed: {}", ac.following_count)));
 
+        let mut relationships = Vec::new();
+        if ac.id == client.our_account_id() {
+            relationships.push(Paragraph::new().set_indent(2, 2).add(
+                &ColouredString::general("You are this user!",
+                                         "    ___           ")));
+        }
+        match client.account_relationship_by_id(&ac.id) {
+            Ok(rs) => {
+                if rs.following && rs.showing_reblogs {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You follow this user.", 'f')));
+                } else if rs.following {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You follow this user (but without boosts).",
+                            'f')));
+                }
+                if rs.followed_by {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "This user follows you.", 'f')));
+                }
+                if rs.requested {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "This user has requested to follow you!", 'F')));
+                }
+                if rs.notifying {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::plain(
+                            "You have enabled notifications for this user.")));
+                }
+                if rs.blocking {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You have blocked this user.", 'r')));
+                }
+                if rs.blocked_by {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "This user has blocked you.", 'r')));
+                }
+                if rs.muting {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You have muted this user.", 'r')));
+                }
+                if rs.muting_notifications {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You have muted notifications from this user.",
+                            'r')));
+                }
+                if rs.domain_blocking {
+                    relationships.push(Paragraph::new().set_indent(2, 2).add(
+                        &ColouredString::uniform(
+                            "You have blocked this user's domain.",
+                            'r')));
+                }
+            }
+            Err(e) => relationships.push(Paragraph::new().set_indent(2, 2).add(
+                &ColouredString::uniform(
+                    &format!("Unable to retrieve relationships: {}", e),
+                    '!'))),
+        }
+        if relationships.len() > 0 {
+            relationships.insert(0, Paragraph::new().add(
+                &ColouredString::plain("Relationships to this user:")));
+        }
+
         Ok(ExamineUserDisplay {
             name,
             webaccount,
@@ -1558,6 +1630,7 @@ impl ExamineUserDisplay {
             post_count,
             followers_count,
             following_count,
+            relationships,
             blank: BlankLine::new(),
         })
     }
@@ -1596,7 +1669,14 @@ impl TextFragment for ExamineUserDisplay {
             push_fragment(&mut lines, self.blank.render(width));
         }
 
-        // FIXME: relationships list
+        // FIXME: flags
+
+        if !self.relationships.is_empty() {
+            for para in &self.relationships {
+                push_fragment(&mut lines, para.render(width));
+            }
+            push_fragment(&mut lines, self.blank.render(width));
+        }
 
         push_fragment(&mut lines, self.id.render(width));
         push_fragment(&mut lines, self.creation.render(width));
index 0ff35e7aee5c85f52572875e05fd0ad574802fa2..d708f2303ba41832c4a0daeab9bc0d73487f8ed4 100644 (file)
@@ -212,3 +212,21 @@ pub struct Notification {
     pub status: Option<Status>,
     // pub report: Option<AdminReport>,
 }
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct Relationship {
+    pub id: String,
+    pub following: bool,
+    pub showing_reblogs: bool,
+    pub notifying: bool,
+    pub languages: Option<Vec<String>>,
+    pub followed_by: bool,
+    pub blocking: bool,
+    pub blocked_by: bool,
+    pub muting: bool,
+    pub muting_notifications: bool,
+    pub requested: bool,
+    pub domain_blocking: bool,
+    pub endorsed: bool,
+    pub note: String,
+}