[bugfix] Fix paging for empty items (#2236)

* use minID properly for public timeline

* return paged response properly even when 0 items

* use gtserror

* page more consistently (for now)

* test

* aaa
This commit is contained in:
tobi
2023-09-29 15:31:10 +02:00
committed by GitHub
parent 736cd37caf
commit 2b6b9cdf83
7 changed files with 75 additions and 62 deletions

View File

@@ -25,7 +25,6 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
@@ -34,15 +33,23 @@ import (
// Paging for this response is done based on bookmark ID rather than status ID.
func (p *Processor) BookmarksGet(ctx context.Context, requestingAccount *gtsmodel.Account, limit int, maxID string, minID string) (*apimodel.PageableResponse, gtserror.WithCode) {
bookmarks, err := p.state.DB.GetStatusBookmarks(ctx, requestingAccount.ID, limit, maxID, minID)
if err != nil {
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(err)
}
count := len(bookmarks)
if count == 0 {
return util.EmptyPageableResponse(), nil
}
var (
count = len(bookmarks)
items = make([]interface{}, 0, count)
nextMaxIDValue = id.Highest
prevMinIDValue = id.Lowest
items = make([]interface{}, 0, count)
// Set next + prev values before filtering and API
// converting, so caller can still page properly.
// Page based on bookmark ID, not status ID.
nextMaxIDValue = bookmarks[count-1].ID
prevMinIDValue = bookmarks[0].ID
)
for _, bookmark := range bookmarks {
@@ -73,23 +80,6 @@ func (p *Processor) BookmarksGet(ctx context.Context, requestingAccount *gtsmode
continue
}
items = append(items, item)
// Page based on bookmark ID, not status ID.
// Note that we only set these values here
// when we're certain that the caller is able
// to see the status, *and* we're sure that
// we can produce an api model representation.
if bookmark.ID < nextMaxIDValue {
nextMaxIDValue = bookmark.ID // Lowest ID (for paging down).
}
if bookmark.ID > prevMinIDValue {
prevMinIDValue = bookmark.ID // Highest ID (for paging up).
}
}
if len(items) == 0 {
return util.EmptyPageableResponse(), nil
}
return util.PackagePageableResponse(util.PageableResponseParams{

View File

@@ -74,21 +74,8 @@ func (p *Processor) StatusesGet(
return nil, gtserror.NewErrorInternalError(err)
}
if len(statuses) == 0 {
return util.EmptyPageableResponse(), nil
}
// Filtering + serialization process is the same for
// both pinned status queries and 'normal' ones.
filtered, err := p.filter.StatusesVisible(ctx, requestingAccount, statuses)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
count := len(filtered)
count := len(statuses)
if count == 0 {
// After filtering there were
// no statuses left to serve.
return util.EmptyPageableResponse(), nil
}
@@ -97,10 +84,17 @@ func (p *Processor) StatusesGet(
// Set next + prev values before filtering and API
// converting, so caller can still page properly.
nextMaxIDValue = filtered[count-1].ID
prevMinIDValue = filtered[0].ID
nextMaxIDValue = statuses[count-1].ID
prevMinIDValue = statuses[0].ID
)
// Filtering + serialization process is the same for
// both pinned status queries and 'normal' ones.
filtered, err := p.filter.StatusesVisible(ctx, requestingAccount, statuses)
if err != nil {
return nil, gtserror.NewErrorInternalError(err)
}
for _, s := range filtered {
// Convert filtered statuses to API statuses.
item, err := p.converter.StatusToAPIStatus(ctx, s, requestingAccount)

View File

@@ -19,6 +19,7 @@ package admin
import (
"context"
"errors"
"fmt"
"strconv"
"time"
@@ -45,17 +46,20 @@ func (p *Processor) ReportsGet(
limit int,
) (*apimodel.PageableResponse, gtserror.WithCode) {
reports, err := p.state.DB.GetReports(ctx, resolved, accountID, targetAccountID, maxID, sinceID, minID, limit)
if err != nil {
if err == db.ErrNoEntries {
return util.EmptyPageableResponse(), nil
}
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(err)
}
count := len(reports)
items := make([]interface{}, 0, count)
nextMaxIDValue := reports[count-1].ID
prevMinIDValue := reports[0].ID
if count == 0 {
return util.EmptyPageableResponse(), nil
}
var (
items = make([]interface{}, 0, count)
nextMaxIDValue = reports[count-1].ID
prevMinIDValue = reports[0].ID
)
for _, r := range reports {
item, err := p.converter.ReportToAdminAPIReport(ctx, r, account)
@@ -65,7 +69,7 @@ func (p *Processor) ReportsGet(
items = append(items, item)
}
extraQueryParams := []string{}
extraQueryParams := make([]string, 0, 3)
if resolved != nil {
extraQueryParams = append(extraQueryParams, "resolved="+strconv.FormatBool(*resolved))
}

View File

@@ -20,7 +20,6 @@ package timeline
import (
"context"
"errors"
"fmt"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -33,7 +32,7 @@ import (
func (p *Processor) PublicTimelineGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, minID string, limit int, local bool) (*apimodel.PageableResponse, gtserror.WithCode) {
statuses, err := p.state.DB.GetPublicTimeline(ctx, maxID, sinceID, minID, limit, local)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("PublicTimelineGet: db error getting statuses: %w", err)
err = gtserror.Newf("db error getting statuses: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}