From 832befd727a257581d774bb6bbdeb0f81cd658e2 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 10 Oct 2022 15:52:49 +0200 Subject: [chore] Make paging logic more generic (#901) * make paging logic more generic not just for timelines! * linty linterson --- internal/processing/account/account.go | 4 +- internal/processing/account/getstatuses.go | 73 ++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 25 deletions(-) (limited to 'internal/processing/account') diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index b18e705ca..d2c3cddbe 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -60,10 +60,10 @@ type Processor interface { Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, gtserror.WithCode) // StatusesGet fetches a number of statuses (in time descending order) from the given account, filtered by visibility for // the account given in authed. - StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinned bool, mediaOnly bool, publicOnly bool) (*apimodel.TimelineResponse, gtserror.WithCode) + StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinned bool, mediaOnly bool, publicOnly bool) (*apimodel.PageableResponse, gtserror.WithCode) // WebStatusesGet fetches a number of statuses (in descending order) from the given account. It selects only // statuses which are suitable for showing on the public web profile of an account. - WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.TimelineResponse, gtserror.WithCode) + WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.PageableResponse, gtserror.WithCode) // FollowersGet fetches a list of the target account's followers. FollowersGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]apimodel.Account, gtserror.WithCode) // FollowingGet fetches a list of the accounts that target account is following. diff --git a/internal/processing/account/getstatuses.go b/internal/processing/account/getstatuses.go index 7dbe68462..9c78917c3 100644 --- a/internal/processing/account/getstatuses.go +++ b/internal/processing/account/getstatuses.go @@ -26,11 +26,10 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" - "github.com/superseriousbusiness/gotosocial/internal/timeline" "github.com/superseriousbusiness/gotosocial/internal/util" ) -func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinnedOnly bool, mediaOnly bool, publicOnly bool) (*apimodel.TimelineResponse, gtserror.WithCode) { +func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string, limit int, excludeReplies bool, excludeReblogs bool, maxID string, minID string, pinnedOnly bool, mediaOnly bool, publicOnly bool) (*apimodel.PageableResponse, gtserror.WithCode) { if requestingAccount != nil { if blocked, err := p.db.IsBlocked(ctx, requestingAccount.ID, targetAccountID, true); err != nil { return nil, gtserror.NewErrorInternalError(err) @@ -42,7 +41,7 @@ func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel statuses, err := p.db.GetAccountStatuses(ctx, targetAccountID, limit, excludeReplies, excludeReblogs, maxID, minID, pinnedOnly, mediaOnly, publicOnly) if err != nil { if err == db.ErrNoEntries { - return util.EmptyTimelineResponse(), nil + return util.EmptyPageableResponse(), nil } return nil, gtserror.NewErrorInternalError(err) } @@ -55,25 +54,37 @@ func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel } } - if len(filtered) == 0 { - return util.EmptyTimelineResponse(), nil + count := len(filtered) + + if count == 0 { + return util.EmptyPageableResponse(), nil } - timelineables := []timeline.Timelineable{} - for _, i := range filtered { - apiStatus, err := p.tc.StatusToAPIStatus(ctx, i, requestingAccount) + items := []interface{}{} + nextMaxIDValue := "" + prevMinIDValue := "" + for i, s := range filtered { + item, err := p.tc.StatusToAPIStatus(ctx, s, requestingAccount) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status to api: %s", err)) } - timelineables = append(timelineables, apiStatus) + if i == count-1 { + nextMaxIDValue = item.GetID() + } + + if i == 0 { + prevMinIDValue = item.GetID() + } + + items = append(items, item) } - return util.PackageTimelineableResponse(util.TimelineableResponseParams{ - Items: timelineables, + return util.PackagePageableResponse(util.PageableResponseParams{ + Items: items, Path: fmt.Sprintf("/api/v1/accounts/%s/statuses", targetAccountID), - NextMaxIDValue: timelineables[len(timelineables)-1].GetID(), - PrevMinIDValue: timelineables[0].GetID(), + NextMaxIDValue: nextMaxIDValue, + PrevMinIDValue: prevMinIDValue, Limit: limit, ExtraQueryParams: []string{ fmt.Sprintf("exclude_replies=%t", excludeReplies), @@ -85,7 +96,7 @@ func (p *processor) StatusesGet(ctx context.Context, requestingAccount *gtsmodel }) } -func (p *processor) WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.TimelineResponse, gtserror.WithCode) { +func (p *processor) WebStatusesGet(ctx context.Context, targetAccountID string, maxID string) (*apimodel.PageableResponse, gtserror.WithCode) { acct, err := p.db.GetAccountByID(ctx, targetAccountID) if err != nil { if err == db.ErrNoEntries { @@ -103,26 +114,42 @@ func (p *processor) WebStatusesGet(ctx context.Context, targetAccountID string, statuses, err := p.db.GetAccountWebStatuses(ctx, targetAccountID, 10, maxID) if err != nil { if err == db.ErrNoEntries { - return util.EmptyTimelineResponse(), nil + return util.EmptyPageableResponse(), nil } return nil, gtserror.NewErrorInternalError(err) } - timelineables := []timeline.Timelineable{} - for _, i := range statuses { - apiStatus, err := p.tc.StatusToAPIStatus(ctx, i, nil) + count := len(statuses) + + if count == 0 { + return util.EmptyPageableResponse(), nil + } + + items := []interface{}{} + nextMaxIDValue := "" + prevMinIDValue := "" + for i, s := range statuses { + item, err := p.tc.StatusToAPIStatus(ctx, s, nil) if err != nil { return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting status to api: %s", err)) } - timelineables = append(timelineables, apiStatus) + if i == count-1 { + nextMaxIDValue = item.GetID() + } + + if i == 0 { + prevMinIDValue = item.GetID() + } + + items = append(items, item) } - return util.PackageTimelineableResponse(util.TimelineableResponseParams{ - Items: timelineables, + return util.PackagePageableResponse(util.PageableResponseParams{ + Items: items, Path: "/@" + acct.Username, - NextMaxIDValue: timelineables[len(timelineables)-1].GetID(), - PrevMinIDValue: timelineables[0].GetID(), + NextMaxIDValue: nextMaxIDValue, + PrevMinIDValue: prevMinIDValue, ExtraQueryParams: []string{}, }) } -- cgit v1.2.3