github.com/decred/politeia@v1.4.0/politeiawww/cmd/pictl/cmdproposalinvordered.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  	rcv1 "github.com/decred/politeia/politeiawww/api/records/v1"
     9  	pclient "github.com/decred/politeia/politeiawww/client"
    10  )
    11  
    12  // cmdProposalInvOrdered retrieves a page of chronologically ordered censorship
    13  // record tokens. The tokens will include records of all statuses.
    14  type cmdProposalInvOrdered struct {
    15  	Args struct {
    16  		State string `positional-arg-name:"state"`
    17  		Page  uint32 `positional-arg-name:"page"`
    18  	} `positional-args:"true" optional:"true"`
    19  }
    20  
    21  // Execute executes the cmdProposalInvOrdered command.
    22  //
    23  // This function satisfies the go-flags Commander interface.
    24  func (c *cmdProposalInvOrdered) Execute(args []string) error {
    25  	_, err := proposalInvOrdered(c)
    26  	if err != nil {
    27  		return err
    28  	}
    29  	return nil
    30  }
    31  
    32  // proposalInvOrdered retrieves a page of chronologically ordered proposal
    33  // tokens. This function has been pulled out of the Execute method so that it
    34  // can be used in test commands.
    35  func proposalInvOrdered(c *cmdProposalInvOrdered) (*rcv1.InventoryOrderedReply, error) {
    36  	// Setup client
    37  	opts := pclient.Opts{
    38  		HTTPSCert:  cfg.HTTPSCert,
    39  		Cookies:    cfg.Cookies,
    40  		HeaderCSRF: cfg.CSRF,
    41  		Verbose:    cfg.Verbose,
    42  		RawJSON:    cfg.RawJSON,
    43  	}
    44  	pc, err := pclient.New(cfg.Host, opts)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	// Setup state
    50  	var state rcv1.RecordStateT
    51  	if c.Args.State != "" {
    52  		// A state was provided. This can be either the numeric state
    53  		// code or the human readable equivalent.
    54  		state, err = parseRecordState(c.Args.State)
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  	} else {
    59  		// No state was provided. Default to vetted.
    60  		state = rcv1.RecordStateVetted
    61  	}
    62  
    63  	// If a status was given but no page number was given, default to
    64  	// page 1.
    65  	if c.Args.Page == 0 {
    66  		c.Args.Page = 1
    67  	}
    68  
    69  	// Get inventory
    70  	i := rcv1.InventoryOrdered{
    71  		State: state,
    72  		Page:  c.Args.Page,
    73  	}
    74  	ir, err := pc.RecordInventoryOrdered(i)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	// Print inventory
    80  	printJSON(ir)
    81  
    82  	return ir, nil
    83  }
    84  
    85  // proposalInvOrderedHelpMsg is printed to stdout by the help command.
    86  const proposalInvOrderedHelpMsg = `proposalinvordered
    87  
    88  Inventory ordered returns a page of record tokens ordered by the timestamp of
    89  their most recent status change from newest to oldest. The reply will include
    90  tokens for all record statuses. Unvetted tokens will only be returned to
    91  admins.
    92  
    93  If no state is provided this command defaults to requesting vetted tokens.
    94  
    95  If no page number is provided this command defaults to requesting page 1.
    96  
    97  Valid states:
    98    (1) unvetted
    99    (2) vetted
   100  
   101  Arguments:
   102  1. state  (string, optional) State of tokens being requested.
   103  2. page   (uint32, optional) Page number.
   104  `