From: Simon Tatham Date: Sat, 13 Jan 2024 11:27:51 +0000 (+0000) Subject: Honour server-side defaults when composing a new post. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=06f59c1ccd758e43ce5dc2270d91a88052379e67;p=mastodonochrome.git Honour server-side defaults when composing a new post. Now after you set your default language, visibility and sensitivity in [ESC][Y][O] (or indeed via any other client authenticated to the same account), they will be used by the posting UI. --- diff --git a/src/posting.rs b/src/posting.rs index 5915e55..28b17b7 100644 --- a/src/posting.rs +++ b/src/posting.rs @@ -7,7 +7,7 @@ use super::tui::{ ActivityState, CursorPosition, LogicalAction, OurKey, OurKey::*, }; use super::text::*; -use super::types::Visibility; +use super::types::{Account, Visibility}; use super::editor::EditableMenuLine; #[derive(Debug, PartialEq, Eq, Clone)] @@ -24,25 +24,48 @@ pub struct Post { pub m: PostMetadata, } -fn default_language() -> String { - get_locale().as_deref() - .and_then(|s| s.split('-').next()) - .map(|s| if s.is_empty() { "en" } else { s }) - .unwrap_or("en") - .to_owned() +fn default_language(ac: &Account) -> String { + ac.source.as_ref().and_then(|s| s.language.clone()).unwrap_or_else( + || get_locale().as_deref() + .and_then(|s| s.split('-').next()) + .map(|s| if s.is_empty() { "en" } else { s }) + .unwrap_or("en") + .to_owned()) } impl Post { - pub fn new() -> Self { - Post { + pub fn new(client: &mut Client) -> Result { + let ac = client.account_by_id(&client.our_account_id())?; + + // Take the default visibility from your account settings + let visibility = ac.source.as_ref().map_or( + Visibility::Public, |s| s.privacy); + + // Set the 'sensitive' flag if the account is marked as + // 'posts are sensitive by default'. + // + // I don't really approve of _just_ setting the sensitive flag + // without also giving a textual content warning saying why, + // so it would be nice here to encourage the user to write + // one, or fill in a default one. But the former is horribly + // intrusive (to the code structure _as well_ as to the UX!), + // and the latter is language-dependent (software shouldn't + // auto-fill an English message if the user is posting in + // Korean). So if the user has set that as their defaults, we + // sigh, and go with 'sensitive but no message'. + let content_warning = ac.source.as_ref().and_then(|s| { + if s.sensitive { Some("".to_owned()) } else { None } + }); + + Ok(Post { text: "".to_owned(), m: PostMetadata { in_reply_to_id: None, - visibility: Visibility::Public, - content_warning: None, - language: default_language(), + visibility, + content_warning, + language: default_language(&ac), }, - } + }) } #[cfg(test)] @@ -53,7 +76,7 @@ impl Post { in_reply_to_id: None, visibility: Visibility::Public, content_warning: None, - language: default_language(), + language: "dummy".to_owned(), }, } } @@ -61,6 +84,7 @@ impl Post { pub fn reply_to(id: &str, client: &mut Client) -> Result { + let ac = client.account_by_id(&client.our_account_id())?; let st = client.status_by_id(id)?.strip_boost(); let ourself = client.our_account_fq(); @@ -77,7 +101,7 @@ impl Post { in_reply_to_id: Some(id.to_owned()), visibility: st.visibility, // match the existing vis content_warning: None, - language: default_language(), + language: default_language(&ac), }, }) } diff --git a/src/tui.rs b/src/tui.rs index b6997be..53e8946 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -804,8 +804,13 @@ impl TuiLogicalState { examine_user(client, name), Activity::Util(UtilityActivity::InfoStatus(ref id)) => view_single_post(self.unfolded_posts.clone(), client, id), - Activity::NonUtil(NonUtilityActivity::ComposeToplevel) => - compose_post(client, post.unwrap_or_else(Post::new)), + Activity::NonUtil(NonUtilityActivity::ComposeToplevel) => (|| { + let post = match post { + Some(post) => post, + None => Post::new(client)?, + }; + compose_post(client, post) + })(), Activity::NonUtil(NonUtilityActivity::PostComposeMenu) => Ok(post_menu(post.expect( "how did we get here without a Post?"))),