From 336f3a40e3e35810153099c5cb1ad4f410994eef Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 1 Jan 2024 11:58:03 +0000 Subject: [PATCH] Show account relationships. --- src/client.rs | 26 ++++++++++++++++ src/text.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/types.rs | 18 +++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index f5c9695..6a68b90 100644 --- a/src/client.rs +++ b/src/client.rs @@ -253,6 +253,32 @@ impl Client { Ok(ac) } + pub fn account_relationship_by_id(&mut self, id: &str) -> + Result + { + let (url, req) = self.api_request( + Req::get("accounts/relationships").param("id", id))?; + let rsp = req.send()?; + let rspstatus = rsp.status(); + let rels: Vec = 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 { if let Some(st) = self.statuses.get(id) { let mut st = st.clone(); diff --git a/src/text.rs b/src/text.rs index 4cfcb81..1833dbd 100644 --- a/src/text.rs +++ b/src/text.rs @@ -1492,6 +1492,7 @@ pub struct ExamineUserDisplay { post_count: Paragraph, followers_count: Paragraph, following_count: Paragraph, + relationships: Vec, 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)); diff --git a/src/types.rs b/src/types.rs index 0ff35e7..d708f23 100644 --- a/src/types.rs +++ b/src/types.rs @@ -212,3 +212,21 @@ pub struct Notification { pub status: Option, // pub report: Option, } + +#[derive(Deserialize, Debug, Clone)] +pub struct Relationship { + pub id: String, + pub following: bool, + pub showing_reblogs: bool, + pub notifying: bool, + pub languages: Option>, + 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, +} -- 2.30.2