Skip to content

Commit

Permalink
Add initial support for pagination in ListComments, ListEvents.
Browse files Browse the repository at this point in the history
Only implemented in fs service implementation for now.

Helps github.com/shurcooL/home/idiomaticgo rendering performance.
Helps https://dmitri.shuralyov.com/issues/github.com/shurcooL/issuesapp/14.
  • Loading branch information
dmitshur committed Dec 29, 2016
1 parent 5cb49a3 commit d5974e9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
8 changes: 6 additions & 2 deletions asanaapi/asanaapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (s service) canEdit(isCollaborator bool, isCollaboratorErr error, authorID
}
}

func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) {
func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) {
// TODO: Pagination. Respect opt.Start and opt.Length, if given.

var comments []issues.Comment

// TODO: Figure this out.
Expand Down Expand Up @@ -215,7 +217,9 @@ func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64,
return comments, nil
}

func (s service) ListEvents(ctx context.Context, _ issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) {
func (s service) ListEvents(ctx context.Context, _ issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) {
// TODO: Pagination. Respect opt.Start and opt.Length, if given.

stories, err := s.cl.ListTaskStories(ctx, int64(id), &asana.Filter{OptExpand: []string{"created_by"}})
if err != nil {
return nil, err
Expand Down
23 changes: 19 additions & 4 deletions fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s service) Get(ctx context.Context, repo issues.RepoSpec, id uint64) (issu
}, nil
}

func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) {
func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) {
currentUser, err := s.users.GetAuthenticated(ctx)
if err != nil {
return nil, err
Expand All @@ -162,7 +162,7 @@ func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint
if err != nil {
return comments, err
}
for _, fi := range fis {
for _, fi := range paginate(fis, opt) {
var comment comment
err = jsonDecodeFile(s.fs, issueCommentPath(repo, id, fi.ID), &comment)
if err != nil {
Expand Down Expand Up @@ -203,14 +203,14 @@ func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint
return comments, nil
}

func (s service) ListEvents(ctx context.Context, repo issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) {
func (s service) ListEvents(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) {
var events []issues.Event

fis, err := readDirIDs(s.fs, issueEventsDir(repo, id))
if err != nil {
return events, err
}
for _, fi := range fis {
for _, fi := range paginate(fis, opt) {
var event event
err = jsonDecodeFile(s.fs, issueEventPath(repo, id, fi.ID), &event)
if err != nil {
Expand Down Expand Up @@ -697,6 +697,21 @@ func (s service) EditComment(ctx context.Context, repo issues.RepoSpec, id uint6
}, nil
}

func paginate(fis []fileInfoID, opt *issues.ListOptions) []fileInfoID {
if opt == nil {
return fis
}
start := opt.Start
if start > len(fis) {
start = len(fis)
}
end := opt.Start + opt.Length
if end > len(fis) {
end = len(fis)
}
return fis[start:end]
}

// toggleReaction toggles reaction emojiID to comment c for specified user u.
// If user is creating a new reaction, they get added to the end of reaction authors.
func toggleReaction(c *comment, u users.UserSpec, emojiID reactions.EmojiID) error {
Expand Down
10 changes: 7 additions & 3 deletions githubapi/githubapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func (s service) Get(ctx context.Context, rs issues.RepoSpec, id uint64) (issues
}, nil
}

func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) {
func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) {
// TODO: Respect opt.Start and opt.Length, if given.

repo, err := ghRepoSpec(rs)
if err != nil {
return nil, err
Expand Down Expand Up @@ -296,14 +298,16 @@ func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64
return comments, nil
}

func (s service) ListEvents(_ context.Context, rs issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) {
func (s service) ListEvents(_ context.Context, rs issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) {
// TODO: Pagination. Respect opt.Start and opt.Length, if given.

repo, err := ghRepoSpec(rs)
if err != nil {
return nil, err
}
var events []issues.Event

ghEvents, _, err := s.cl.Issues.ListIssueEvents(repo.Owner, repo.Repo, int(id), nil) // TODO: Pagination.
ghEvents, _, err := s.cl.Issues.ListIssueEvents(repo.Owner, repo.Repo, int(id), nil)
if err != nil {
return events, err
}
Expand Down
4 changes: 2 additions & 2 deletions issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type Service interface {
Get(ctx context.Context, repo RepoSpec, id uint64) (Issue, error)

// ListComments lists comments for specified issue id.
ListComments(ctx context.Context, repo RepoSpec, id uint64, opt interface{}) ([]Comment, error)
ListComments(ctx context.Context, repo RepoSpec, id uint64, opt *ListOptions) ([]Comment, error)
// ListEvents lists events for specified issue id.
ListEvents(ctx context.Context, repo RepoSpec, id uint64, opt interface{}) ([]Event, error)
ListEvents(ctx context.Context, repo RepoSpec, id uint64, opt *ListOptions) ([]Event, error)

// Create a new issue.
Create(ctx context.Context, repo RepoSpec, issue Issue) (Issue, error)
Expand Down
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ const (
// AllStates is a state filter that includes all issues.
AllStates StateFilter = "all"
)

// ListOptions controls pagination.
type ListOptions struct {
// Start is the index of first result to retrieve, zero-indexed.
Start int

// Length is the number of results to include.
Length int
}

0 comments on commit d5974e9

Please sign in to comment.