cmd/toot.go
changeset 168 ce4000ac7294
parent 129 11966471aac3
child 176 7efbbed5cf3c
equal deleted inserted replaced
167:1341bacd01c9 168:ce4000ac7294
    27 	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
    27 	tootAliasCmd.Flags().StringVarP(&statusOpts.mediaFilePath, "file", "f", "", "Media attachment file name")
    28 	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
    28 	tootAliasCmd.Flags().StringVar(&statusOpts.textFilePath, "text-file", "", "Text file name (message content)")
    29 	tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
    29 	tootAliasCmd.Flags().Int64VarP(&statusOpts.inReplyToID, "in-reply-to", "r", 0, "Status ID to reply to")
    30 	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
    30 	tootAliasCmd.Flags().BoolVar(&statusOpts.stdin, "stdin", false, "Read message content from standard input")
    31 	tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
    31 	tootAliasCmd.Flags().BoolVar(&statusOpts.addMentions, "add-mentions", false, "Add mentions when replying")
       
    32 	tootAliasCmd.Flags().BoolVar(&statusOpts.sameVisibility, "same-visibility", false, "Use same visibility as original message (for replies)")
    32 
    33 
    33 	// Flag completion
    34 	// Flag completion
    34 	annotation := make(map[string][]string)
    35 	annotation := make(map[string][]string)
    35 	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
    36 	annotation[cobra.BashCompCustom] = []string{"__madonctl_visibility"}
    36 
    37 
    37 	tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation
    38 	tootAliasCmd.Flags().Lookup("visibility").Annotations = annotation
       
    39 
       
    40 	// This one will be used to check if the options were explicitly set or not
       
    41 	updateFlags = tootAliasCmd.Flags()
    38 }
    42 }
    39 
    43 
    40 var tootAliasCmd = &cobra.Command{
    44 var tootAliasCmd = &cobra.Command{
    41 	Use:     "toot",
    45 	Use:     "toot",
    42 	Aliases: []string{"post", "pouet"},
    46 	Aliases: []string{"post", "pouet"},
    88 
    92 
    89 	if tootText == "" && len(ids) == 0 && opt.spoiler == "" && opt.mediaFilePath == "" {
    93 	if tootText == "" && len(ids) == 0 && opt.spoiler == "" && opt.mediaFilePath == "" {
    90 		return nil, errors.New("toot is empty")
    94 		return nil, errors.New("toot is empty")
    91 	}
    95 	}
    92 
    96 
    93 	if opt.addMentions && opt.inReplyToID > 0 {
    97 	if opt.inReplyToID > 0 {
    94 		mentions, err := mentionsList(opt.inReplyToID)
    98 		var initialStatus *madon.Status
    95 		if err != nil {
    99 		var preserveVis bool
    96 			return nil, err
   100 		if opt.sameVisibility && !updateFlags.Lookup("visibility").Changed {
       
   101 			// Preserve visibility unless the --visibility flag
       
   102 			// has been used in the command line.
       
   103 			preserveVis = true
    97 		}
   104 		}
    98 		tootText = mentions + tootText
   105 		if preserveVis || opt.addMentions {
       
   106 			// Fetch original status message
       
   107 			initialStatus, err = gClient.GetStatus(opt.inReplyToID)
       
   108 			if err != nil {
       
   109 				return nil, errors.Wrap(err, "cannot get original message")
       
   110 			}
       
   111 		}
       
   112 		if preserveVis {
       
   113 			v := initialStatus.Visibility
       
   114 			// We do not set public visibility unless explicitly requested
       
   115 			if v == "public" {
       
   116 				opt.visibility = "unlisted"
       
   117 			} else {
       
   118 				opt.visibility = v
       
   119 			}
       
   120 		}
       
   121 		if opt.addMentions {
       
   122 			mentions, err := mentionsList(initialStatus)
       
   123 			if err != nil {
       
   124 				return nil, err
       
   125 			}
       
   126 			tootText = mentions + tootText
       
   127 		}
    99 	}
   128 	}
   100 
   129 
   101 	// Uploading media file last
   130 	// Uploading media file last
   102 	if opt.mediaFilePath != "" {
   131 	if opt.mediaFilePath != "" {
   103 		if len(ids) > 3 {
   132 		if len(ids) > 3 {
   114 	}
   143 	}
   115 
   144 
   116 	return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility)
   145 	return gClient.PostStatus(tootText, opt.inReplyToID, ids, opt.sensitive, opt.spoiler, opt.visibility)
   117 }
   146 }
   118 
   147 
   119 func mentionsList(statusID int64) (string, error) {
   148 func mentionsList(s *madon.Status) (string, error) {
   120 	a, err := gClient.GetCurrentAccount()
   149 	a, err := gClient.GetCurrentAccount()
   121 	if err != nil {
   150 	if err != nil {
   122 		return "", errors.Wrap(err, "cannot check account details")
   151 		return "", errors.Wrap(err, "cannot check account details")
   123 	}
       
   124 
       
   125 	s, err := gClient.GetStatus(statusID)
       
   126 	if err != nil {
       
   127 		return "", errors.Wrap(err, "cannot get mentions")
       
   128 	}
   152 	}
   129 
   153 
   130 	var mentions []string
   154 	var mentions []string
   131 	// Add the sender if she is not the connected user
   155 	// Add the sender if she is not the connected user
   132 	if s.Account.Acct != a.Acct {
   156 	if s.Account.Acct != a.Acct {