chiark / gitweb /
Honour server-side defaults when composing a new post.
authorSimon Tatham <anakin@pobox.com>
Sat, 13 Jan 2024 11:27:51 +0000 (11:27 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 13 Jan 2024 13:59:16 +0000 (13:59 +0000)
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.

src/posting.rs
src/tui.rs

index 5915e55eaf87848538547f53cedc265274a4d43f..28b17b752f4f0b254ef7fa34dfa308efcfca3a9c 100644 (file)
@@ -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<Self, ClientError> {
+        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<Self, ClientError>
     {
+        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),
             },
         })
     }
index b6997be17c68aaf3c826849d3c5cfd91276585bb..53e89464d07e55b4d8c28ce11dc874e7b6cb0805 100644 (file)
@@ -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?"))),