github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/useredit.go (about) 1 // Copyright (c) 2017-2019 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "fmt" 9 "strconv" 10 "strings" 11 12 v1 "github.com/decred/politeia/politeiawww/api/www/v1" 13 "github.com/decred/politeia/politeiawww/cmd/shared" 14 ) 15 16 // userEditCmd edits the preferences of the logged in user. 17 type userEditCmd struct { 18 Args struct { 19 NotifType string `long:"emailnotifications"` // Email notification bit field 20 } `positional-args:"true" required:"true"` 21 } 22 23 // Execute executes the userEditCmd command. 24 // 25 // This function satisfies the go-flags Commander interface. 26 func (cmd *userEditCmd) Execute(args []string) error { 27 emailNotifs := map[string]v1.EmailNotificationT{ 28 "userproposalchange": v1.NotificationEmailMyProposalStatusChange, 29 "userproposalvotingstarted": v1.NotificationEmailMyProposalVoteStarted, 30 "proposalvetted": v1.NotificationEmailRegularProposalVetted, 31 "proposaledited": v1.NotificationEmailRegularProposalEdited, 32 "votingstarted": v1.NotificationEmailRegularProposalVoteStarted, 33 "newproposal": v1.NotificationEmailAdminProposalNew, 34 "userauthorizedvote": v1.NotificationEmailAdminProposalVoteAuthorized, 35 "commentonproposal": v1.NotificationEmailCommentOnMyProposal, 36 "commentoncomment": v1.NotificationEmailCommentOnMyComment, 37 } 38 39 var notif v1.EmailNotificationT 40 a, err := strconv.ParseUint(cmd.Args.NotifType, 10, 64) 41 if err == nil { 42 // Numeric action code found 43 notif = v1.EmailNotificationT(a) 44 } else if a, ok := emailNotifs[cmd.Args.NotifType]; ok { 45 // Human readable action code found 46 notif = a 47 } else if strings.Contains(cmd.Args.NotifType, ",") { 48 // List of human readable action codes found 49 50 notif = a 51 // Parse list of strings and calculate associated integer 52 s := strings.Split(cmd.Args.NotifType, ",") 53 for _, v := range s { 54 a, ok := emailNotifs[v] 55 if !ok { 56 return fmt.Errorf("Invalid edituser option. Type " + 57 "'help edituser' for list of valid options") 58 } 59 notif |= a 60 } 61 } else { 62 return fmt.Errorf("Invalid edituser option. Type 'help edituser' " + 63 "for list of valid options") 64 } 65 66 // Setup request 67 helper := uint64(notif) 68 eu := &v1.EditUser{ 69 EmailNotifications: &helper, 70 } 71 72 // Print request details 73 err = shared.PrintJSON(eu) 74 if err != nil { 75 return err 76 } 77 78 // Send request 79 eur, err := client.EditUser(eu) 80 if err != nil { 81 return err 82 } 83 84 // Print response details 85 return shared.PrintJSON(eur) 86 } 87 88 // userEditHelpMsg is the output of the help command when 'edituser' is 89 // specified. 90 const userEditHelpMsg = `useredit "emailnotifications" 91 92 Edit user settings for the logged in user. 93 94 Arguments: 95 1. emailnotifications (string, required) Email notification bit field 96 97 Valid options are: 98 99 1. userproposalchange Notify when status of my proposal changes 100 2. userproposalvotingstarted Notify when my proposal vote has started 101 4. proposalvetted Notify when any proposal is vetted 102 8. proposaledited Notify when any proposal is edited 103 16. votingstarted Notify when voting on any proposal has started 104 32. newproposal Notify when proposal is submitted (admin only) 105 64. userauthorizedvote Notify when user authorizes vote (admin only) 106 128. commentonproposal Notify when comment is made on my proposal 107 256. commentoncomment Notify when comment is made on my comment`