Skip to content

Commit

Permalink
pictl: commenttimestamps improvements.
Browse files Browse the repository at this point in the history
This diff fixes a bug in `pictl commenttimestamps` where no timestamps
were fetched if the no comment IDs were provided, this is not the
expected behavior as it says in the `commenttimestamps` command help
message:

> If comment IDs are not provided then the timestamps for all comments will be
returned.

In addition, this commit adds pagination to the `commenttimestamps`
command to allow fetching all comment timestamps page by page when
a proposal has more than one page of comments. This also allows users to
request the timestamps of more than one page using the `commentIDs`
command argument.
  • Loading branch information
amass01 authored Jan 12, 2022
1 parent cf53aa7 commit 2668d50
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
4 changes: 1 addition & 3 deletions politeiawww/cmd/pictl/cmdcomments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package main

import (
"fmt"

cmv1 "github.com/decred/politeia/politeiawww/api/comments/v1"
pclient "github.com/decred/politeia/politeiawww/client"
)
Expand Down Expand Up @@ -47,7 +45,7 @@ func (c *cmdComments) Execute(args []string) error {
// Print comments
for _, v := range cr.Comments {
printComment(v)
fmt.Printf("\n")
printf("\n")
}

return nil
Expand Down
79 changes: 69 additions & 10 deletions politeiawww/cmd/pictl/cmdcommenttimestamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,82 @@ func (c *cmdCommentTimestamps) Execute(args []string) error {
return err
}

// Get timestamps
t := cmv1.Timestamps{
Token: c.Args.Token,
CommentIDs: c.Args.CommentIDs,
// If given comment IDs is empty fetch all IDs and request timestamps
// page by page.
commentIDs := c.Args.CommentIDs
if len(commentIDs) == 0 {
// Fetch all comments
cm := cmv1.Comments{
Token: c.Args.Token,
}
cmr, err := pc.Comments(cm)
if err != nil {
return err
}

// Collect comment IDs
for _, c := range cmr.Comments {
commentIDs = append(commentIDs, c.CommentID)
}
}
tr, err := pc.CommentTimestamps(t)
if err != nil {
return err

// If the proposal has no comments yet, nothing to do.
if len(commentIDs) == 0 {
printf("Proposal has no comments \n")
return nil
}

// Verify timestamps
notTimestamped, err := pclient.CommentTimestampsVerify(*tr)
// Get timestamps page size
pr, err := pc.CommentPolicy()
if err != nil {
return err
}
pageSize := pr.TimestampsPageSize

// Timestamps route is paginated, request timestamps page by page.
var (
pageStartIdx int
fetched int
totalNotTimestamped int
)
for pageStartIdx < len(commentIDs) {
pageEndIdx := pageStartIdx + int(pageSize)
if pageEndIdx > len(commentIDs) {
// We've reached the end of the slice
pageEndIdx = len(commentIDs)
}

// pageStartIdx is included. pageEndIdx is excluded.
page := commentIDs[pageStartIdx:pageEndIdx]

// Get timestamps
t := cmv1.Timestamps{
Token: c.Args.Token,
CommentIDs: page,
}
tr, err := pc.CommentTimestamps(t)
if err != nil {
return err
}
fetched = fetched + len(page)

// Verify timestamps
notTimestamped, err := pclient.CommentTimestampsVerify(*tr)
if err != nil {
return err
}
if len(notTimestamped) > 0 {
totalNotTimestamped = totalNotTimestamped + len(notTimestamped)
}

printf("Total number of comments: %v, fetched: %v, timestamped: %v, "+
"not timestamped: %v \n", len(commentIDs), fetched,
fetched-totalNotTimestamped, totalNotTimestamped)

// Next page start index
pageStartIdx = pageEndIdx
}

printf("Not timestamped yet: %v", notTimestamped)
return nil
}

Expand Down

0 comments on commit 2668d50

Please sign in to comment.