Add flag to reply with same visibility as original message
authorMikael Berthe <mikael@lilotux.net>
Sun, 16 Jul 2017 10:33:40 +0200
changeset 168 ce4000ac7294
parent 167 1341bacd01c9
child 169 26a1f0e705c9
Add flag to reply with same visibility as original message Introduce --same-visibility to the toot (or status post) command. It will reduce a "public" visibility to "unlisted" if not overriden on the command line.
cmd/accounts.go
cmd/status.go
cmd/toot.go
--- a/cmd/accounts.go	Sat Jul 15 22:25:46 2017 +0200
+++ b/cmd/accounts.go	Sun Jul 16 10:33:40 2017 +0200
@@ -75,7 +75,7 @@
 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.avatar, "avatar", "", "User avatar image")
 	accountUpdateSubcommand.Flags().StringVar(&accountsOpts.header, "header", "", "User header image")
 
-	// This one will be used to check if the options were explicitely set or not
+	// This one will be used to check if the options were explicitly set or not
 	updateFlags = accountUpdateSubcommand.Flags()
 }
 
--- a/cmd/status.go	Sat Jul 15 22:25:46 2017 +0200
+++ b/cmd/status.go	Sun Jul 16 10:33:40 2017 +0200
@@ -21,15 +21,16 @@
 	unset    bool
 
 	// The following fields are used for the post/toot command
-	visibility    string
-	sensitive     bool
-	spoiler       string
-	inReplyToID   int64
-	mediaIDs      string
-	mediaFilePath string
-	textFilePath  string
-	stdin         bool
-	addMentions   bool
+	visibility     string
+	sensitive      bool
+	spoiler        string
+	inReplyToID    int64
+	mediaIDs       string
+	mediaFilePath  string
+	textFilePath   string
+	stdin          bool
+	addMentions    bool
+	sameVisibility bool
 
 	// Used for several subcommands to limit the number of results
 	limit, keep uint
@@ -65,12 +66,16 @@
 	statusPostSubcommand.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
 	statusPostSubcommand.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
 	statusPostSubcommand.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
+	statusPostSubcommand.Flags().BoolVar(&statusOpts.sameVisibility, "same-visibility", false, "Use same visibility as original message (for replies)")
 
 	// Flag completion
 	annotation := make(map[string][]string)
 	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
 
 	statusPostSubcommand.Flags().Lookup("visibility").Annotations = annotation
+
+	// This one will be used to check if the options were explicitly set or not
+	updateFlags = statusPostSubcommand.Flags()
 }
 
 // statusCmd represents the status command
--- a/cmd/toot.go	Sat Jul 15 22:25:46 2017 +0200
+++ b/cmd/toot.go	Sun Jul 16 10:33:40 2017 +0200
@@ -29,12 +29,16 @@
 	tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
 	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
 	tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
+	tootAliasCmd.Flags().BoolVar(&statusOpts.sameVisibility, "same-visibility", false, "Use same visibility as original message (for replies)")
 
 	// Flag completion
 	annotation := make(map[string][]string)
 	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
 
 	tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation
+
+	// This one will be used to check if the options were explicitly set or not
+	updateFlags = tootAliasCmd.Flags()
 }
 
 var tootAliasCmd = &cobra.Command{
@@ -90,12 +94,37 @@
 		return nil, errors.New("toot is empty")
 	}
 
-	if opt.addMentions && opt.inReplyToID > 0 {
-		mentions, err := mentionsList(opt.inReplyToID)
-		if err != nil {
-			return nil, err
+	if opt.inReplyToID > 0 {
+		var initialStatus *madon.Status
+		var preserveVis bool
+		if opt.sameVisibility && !updateFlags.Lookup("visibility").Changed {
+			// Preserve visibility unless the --visibility flag
+			// has been used in the command line.
+			preserveVis = true
+		}
+		if preserveVis || opt.addMentions {
+			// Fetch original status message
+			initialStatus, err = gClient.GetStatus(opt.inReplyToID)
+			if err != nil {
+				return nil, errors.Wrap(err, "cannot get original message")
+			}
 		}
-		tootText = mentions + tootText
+		if preserveVis {
+			v := initialStatus.Visibility
+			// We do not set public visibility unless explicitly requested
+			if v == "public" {
+				opt.visibility = "unlisted"
+			} else {
+				opt.visibility = v
+			}
+		}
+		if opt.addMentions {
+			mentions, err := mentionsList(initialStatus)
+			if err != nil {
+				return nil, err
+			}
+			tootText = mentions + tootText
+		}
 	}
 
 	// Uploading media file last
@@ -116,17 +145,12 @@
 	return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility)
 }
 
-func mentionsList(statusID int64) (string, error) {
+func mentionsList(s *madon.Status) (string, error) {
 	a, err := gClient.GetCurrentAccount()
 	if err != nil {
 		return "", errors.Wrap(err, "cannot check account details")
 	}
 
-	s, err := gClient.GetStatus(statusID)
-	if err != nil {
-		return "", errors.Wrap(err, "cannot get mentions")
-	}
-
 	var mentions []string
 	// Add the sender if she is not the connected user
 	if s.Account.Acct != a.Acct {