Skip to content

Commit

Permalink
politeiawww: Add page sizes to policy replies.
Browse files Browse the repository at this point in the history
The page sizes for various routes in the `records`, `comments`, and
`ticketvote` APIs are hardcoded in the APIs as variables. This allows
golang clients to references the variables. 

This commit adds the page sizes to the policy replies so that the gui
can reference the policy response rather than hardcode the page sizes.

This diff adds a new policy route to the records API to return the API's
page sizes.  In adittion, this adds a new pictl command 
`pictl recordpolicy` which calls the new route mentioned above.
  • Loading branch information
amass01 authored Sep 23, 2021
1 parent 9b51e89 commit b8348c2
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 12 deletions.
6 changes: 4 additions & 2 deletions politeiawww/api/comments/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ type Policy struct{}

// PolicyReply is the reply to the policy command.
type PolicyReply struct {
LengthMax uint32 `json:"lengthmax"` // In characters
VoteChangesMax uint32 `json:"votechangesmax"`
LengthMax uint32 `json:"lengthmax"` // In characters
VoteChangesMax uint32 `json:"votechangesmax"`
CountPageSize uint32 `json:"countpagesize"`
TimestampsPageSize uint32 `json:"timestampspagesize"`
}

// RecordStateT represents the state of a record.
Expand Down
12 changes: 12 additions & 0 deletions politeiawww/api/records/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const (
// APIRoute is prefixed onto all routes defined in this package.
APIRoute = "/records/v1"

// RoutePolicy returns the policy for the records API.
RoutePolicy = "/policy"

// RouteNew adds a new record.
RouteNew = "/new"

Expand Down Expand Up @@ -197,6 +200,15 @@ func (e ServerErrorReply) Error() string {
return fmt.Sprintf("server error: %v", e.ErrorCode)
}

// Policy requests the policy settings for the records API.
type Policy struct{}

// PolicyReply is the reply to the Policy command.
type PolicyReply struct {
RecordsPageSize uint32 `json:"recordspagesize"`
InventoryPageSize uint32 `json:"inventorypagesize"`
}

// RecordStateT represents the state of a record.
type RecordStateT uint32

Expand Down
11 changes: 7 additions & 4 deletions politeiawww/api/ticketvote/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ type Policy struct{}

// PolicyReply is the reply to the Policy command.
type PolicyReply struct {
LinkByPeriodMin int64 `json:"linkbyperiodmin"` // In seconds
LinkByPeriodMax int64 `json:"linkbyperiodmax"` // In seconds
VoteDurationMin uint32 `json:"votedurationmin"` // In blocks
VoteDurationMax uint32 `json:"votedurationmax"` // In blocks
LinkByPeriodMin int64 `json:"linkbyperiodmin"` // In seconds
LinkByPeriodMax int64 `json:"linkbyperiodmax"` // In seconds
VoteDurationMin uint32 `json:"votedurationmin"` // In blocks
VoteDurationMax uint32 `json:"votedurationmax"` // In blocks
SummariesPageSize uint32 `json:"summariespagesize"`
InventoryPageSize uint32 `json:"inventorypagesize"`
TimestampsPageSize uint32 `json:"timestampspagesize"`
}

// AuthActionT represents an Authorize action.
Expand Down
17 changes: 17 additions & 0 deletions politeiawww/client/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ import (
"github.com/google/uuid"
)

// RecordPolicy sends a records v1 Policy request to politeiawww.
func (c *Client) RecordPolicy() (*rcv1.PolicyReply, error) {
resBody, err := c.makeReq(http.MethodPost,
rcv1.APIRoute, rcv1.RoutePolicy, nil)
if err != nil {
return nil, err
}

var pr rcv1.PolicyReply
err = json.Unmarshal(resBody, &pr)
if err != nil {
return nil, err
}

return &pr, nil
}

// RecordNew sends a records v1 New request to politeiawww.
func (c *Client) RecordNew(n rcv1.New) (*rcv1.NewReply, error) {
resBody, err := c.makeReq(http.MethodPost,
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions politeiawww/cmd/pictl/cmdhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (c *cmdHelp) Execute(args []string) error {
case "userproposals":
fmt.Printf("%s\n", userProposalsHelpMsg)

// Record commands
case "recordpolicy":
fmt.Printf("%s\n", recordPolicyHelpMsg)

// Comment commands
case "commentpolicy":
fmt.Printf("%s\n", commentPolicyHelpMsg)
Expand Down
44 changes: 44 additions & 0 deletions politeiawww/cmd/pictl/cmdrecordpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
pclient "github.com/decred/politeia/politeiawww/client"
)

// cmdRecordPolicy retrieves the records API policy.
type cmdRecordPolicy struct{}

// Execute executes the cmdRecordPolicy command.
//
// This function satisfies the go-flags Commander interface.
func (c *cmdRecordPolicy) Execute(args []string) error {
// Setup client
opts := pclient.Opts{
HTTPSCert: cfg.HTTPSCert,
Verbose: cfg.Verbose,
RawJSON: cfg.RawJSON,
}
pc, err := pclient.New(cfg.Host, opts)
if err != nil {
return err
}

// Get policy
pr, err := pc.RecordPolicy()
if err != nil {
return err
}

// Print policy
printJSON(pr)

return nil
}

// recordPolicyHelpMsg is the printed to stdout by the help command.
const recordPolicyHelpMsg = `recordpolicy
Fetch the records API policy.`
7 changes: 7 additions & 0 deletions politeiawww/cmd/pictl/pictl.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type pictl struct {
ProposalInvOrdered cmdProposalInvOrdered `command:"proposalinvordered"`
UserProposals cmdUserProposals `command:"userproposals"`

// Records commands
RecordPolicy cmdRecordPolicy `command:"recordpolicy"`

// Comments commands
CommentsPolicy cmdCommentPolicy `command:"commentpolicy"`
CommentNew cmdCommentNew `command:"commentnew"`
Expand Down Expand Up @@ -162,10 +165,14 @@ Proposal commands
proposaldetails (public) Get a full proposal record
proposaltimestamps (public) Get timestamps for a proposal
proposals (public) Get proposals without their files
proposalsummaries (public) Get proposal summaries
proposalinv (public) Get inventory by proposal status
proposalinvordered (public) Get inventory ordered chronologically
userproposals (public) Get proposals submitted by a user
Record commands
recordpolicy (public) Get the records api policy
Comment commands
commentpolicy (public) Get the comments api policy
commentnew (user) Submit a new comment
Expand Down
6 changes: 4 additions & 2 deletions politeiawww/comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ func New(cfg *config.Config, pdc *pdclient.Client, udb user.Database, s *session
sessions: s,
events: e,
policy: &v1.PolicyReply{
LengthMax: lengthMax,
VoteChangesMax: voteChangesMax,
LengthMax: lengthMax,
VoteChangesMax: voteChangesMax,
CountPageSize: v1.CountPageSize,
TimestampsPageSize: v1.TimestampsPageSize,
},
}, nil
}
3 changes: 3 additions & 0 deletions politeiawww/pi.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (p *politeiawww) setupPiRoutes(r *records.Records, c *comments.Comments, t
permissionPublic)

// Record routes
p.addRoute(http.MethodPost, rcv1.APIRoute,
rcv1.RoutePolicy, r.HandlePolicy,
permissionPublic)
p.addRoute(http.MethodPost, rcv1.APIRoute,
rcv1.RouteNew, r.HandleNew,
permissionLogin)
Expand Down
12 changes: 12 additions & 0 deletions politeiawww/records/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ type Records struct {
userdb user.Database
sessions *sessions.Sessions
events *events.Manager
policy *v1.PolicyReply
}

// HandlePolicy is the request handler for the records v1 Policy route.
func (c *Records) HandlePolicy(w http.ResponseWriter, r *http.Request) {
log.Tracef("HandlePolicy")

util.RespondWithJSON(w, http.StatusOK, c.policy)
}

// HandleNew is the request handler for the records v1 New route.
Expand Down Expand Up @@ -328,5 +336,9 @@ func New(cfg *config.Config, pdc *pdclient.Client, udb user.Database, s *session
userdb: udb,
sessions: s,
events: e,
policy: &v1.PolicyReply{
RecordsPageSize: v1.RecordsPageSize,
InventoryPageSize: v1.InventoryPageSize,
},
}
}
11 changes: 7 additions & 4 deletions politeiawww/ticketvote/ticketvote.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,13 @@ func New(cfg *config.Config, pdc *pdclient.Client, s *sessions.Sessions, e *even
sessions: s,
events: e,
policy: &v1.PolicyReply{
LinkByPeriodMin: linkByPeriodMin,
LinkByPeriodMax: linkByPeriodMax,
VoteDurationMin: voteDurationMin,
VoteDurationMax: voteDurationMax,
LinkByPeriodMin: linkByPeriodMin,
LinkByPeriodMax: linkByPeriodMax,
VoteDurationMin: voteDurationMin,
VoteDurationMax: voteDurationMax,
SummariesPageSize: v1.SummariesPageSize,
InventoryPageSize: v1.InventoryPageSize,
TimestampsPageSize: v1.VoteTimestampsPageSize,
},
}, nil
}

0 comments on commit b8348c2

Please sign in to comment.