github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdcommentvotes.go (about)

     1  // Copyright (c) 2020-2021 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  	cmv1 "github.com/decred/politeia/politeiawww/api/comments/v1"
     9  	pclient "github.com/decred/politeia/politeiawww/client"
    10  )
    11  
    12  // cmdCommentVotes retrieves the comment upvotes/downvotes for a user on a
    13  // record.
    14  type cmdCommentVotes struct {
    15  	Args struct {
    16  		Token string `positional-arg-name:"token" required:"true"`
    17  	} `positional-args:"true"`
    18  
    19  	// Filtering options
    20  	UserID string `long:"userid"`
    21  	Page   uint32 `long:"page"`
    22  }
    23  
    24  // Execute executes the cmdCommentVotes command.
    25  //
    26  // This function satisfies the go-flags Commander interface.
    27  func (c *cmdCommentVotes) Execute(args []string) error {
    28  	// Unpack args & filtering options
    29  	var (
    30  		token  = c.Args.Token
    31  		userID = c.UserID
    32  		page   = c.Page
    33  	)
    34  
    35  	// Setup client
    36  	opts := pclient.Opts{
    37  		HTTPSCert:  cfg.HTTPSCert,
    38  		Cookies:    cfg.Cookies,
    39  		HeaderCSRF: cfg.CSRF,
    40  		Verbose:    cfg.Verbose,
    41  		RawJSON:    cfg.RawJSON,
    42  	}
    43  	pc, err := pclient.New(cfg.Host, opts)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	// Get comment votes
    49  	v := cmv1.Votes{
    50  		Token:  token,
    51  		UserID: userID,
    52  		Page:   page,
    53  	}
    54  	vr, err := pc.CommentVotes(v)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	// Print votes or an empty message if no votes were found
    60  	switch {
    61  	case len(vr.Votes) > 0:
    62  		printCommentVotes(vr.Votes)
    63  
    64  	case userID != "":
    65  		printf("No comment votes found for user %v\n", userID)
    66  
    67  	case userID == "":
    68  		printf("No comment votes found for proposal %v\n", token)
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  // commentVotesHelpMsg is printed to stdout by the help command.
    75  const commentVotesHelpMsg = `commentvotes "token"
    76  
    77  Get paginated comment up/downvotes of a proposal. The --userid flag can be 
    78  used to retrieve the votes of a specific user. The --page flag can be used 
    79  to retrieve a specific page, if no page is provided then the first page is 
    80  returned.
    81  
    82  Arguments:
    83  1. token  (string, required)  Proposal censorship token
    84  
    85  Flags:
    86    --userid  (string, optional)  User ID
    87    --page    (uint32, optional)  Requested page`