Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix(api): return attachment page token only when more results exist (#6452)
kediyalua91d4a93ec8ae
diff --git a/server/api/attachment_service.go b/server/api/attachment_service.goindex b14c2382d..c9a6182f8 100644--- a/server/api/attachment_service.go+++ b/server/api/attachment_service.go@@ -310,9 +310,15 @@ func (s *APIService) ListAttachments(ctx context.Context, request *apipb.ListAtt}}+ // Request one extra row so we can tell whether another page exists. The+ // extra row is dropped before returning, matching the pattern used by+ // ListUsers. An exact full page that is also the last page must not+ // advertise a next page token, since the API contract states the token is+ // empty when there are no more pages.+ limitPlusOne := pageSize + 1findAttachment := &store.FindAttachment{Access: newMemoAccessScope(user, true),- Limit: &pageSize,+ Limit: &limitPlusOne,Offset: &offset,}// Parse filter if provided@@ -330,13 +336,15 @@ func (s *APIService) ListAttachments(ctx context.Context, request *apipb.ListAttresponse := &apipb.ListAttachmentsResponse{}- for _, attachment := range attachments {- response.Attachments = append(response.Attachments, convertAttachmentFromStore(attachment))+ // Only emit a next page token when we actually fetched the extra row, which+ // proves at least one more result exists.+ if len(attachments) == limitPlusOne {+ attachments = attachments[:pageSize]+ response.NextPageToken = fmt.Sprintf("%d", offset+pageSize)}- // Set next page token if we got the full page size (indicating there might be more)- if len(attachments) == pageSize {- response.NextPageToken = fmt.Sprintf("%d", offset+pageSize)+ for _, attachment := range attachments {+ response.Attachments = append(response.Attachments, convertAttachmentFromStore(attachment))}return response, nildiff --git a/server/api/test/attachment_service_test.go b/server/api/test/attachment_service_test.goindex 75d3a1b75..d5ec1c857 100644--- a/server/api/test/attachment_service_test.go+++ b/server/api/test/attachment_service_test.go@@ -2,6 +2,7 @@ package testimport ("context"+ "fmt""os""path/filepath""testing"@@ -1031,3 +1032,61 @@ func TestListAttachmentsRejectsNegativePageToken(t *testing.T) {_, err = ts.Service.ListAttachments(ts.CreateUserContext(ctx, user.ID), &apipb.ListAttachmentsRequest{PageToken: "-5"})require.Equal(t, codes.InvalidArgument, status.Code(err))}++func TestListAttachmentsNextPageToken(t *testing.T) {+ ctx := context.Background()+ ts := NewTestService(t)+ defer ts.Cleanup()++ const pageSize = 3++ t.Run("exact full page has no next token", func(t *testing.T) {+ user, err := ts.CreateRegularUser(ctx, "attachment-pager-exact")+ require.NoError(t, err)+ userCtx := ts.CreateUserContext(ctx, user.ID)++ for i := range pageSize {+ _, createErr := ts.Store.CreateAttachment(ctx, &store.Attachment{+ UID: fmt.Sprintf("attachment-exact-%d", i),+ CreatorID: user.ID,+ Filename: fmt.Sprintf("attachment-exact-%d.txt", i),+ Type: "text/plain",+ })+ require.NoError(t, createErr)+ }++ resp, err := ts.Service.ListAttachments(userCtx, &apipb.ListAttachmentsRequest{PageSize: pageSize})+ require.NoError(t, err)+ require.Len(t, resp.Attachments, pageSize)+ require.Empty(t, resp.NextPageToken, "an exact full page must not advertise a next page")+ })++ t.Run("overflow page advertises exactly one more", func(t *testing.T) {+ user, err := ts.CreateRegularUser(ctx, "attachment-pager-overflow")+ require.NoError(t, err)+ userCtx := ts.CreateUserContext(ctx, user.ID)++ for i := range pageSize + 1 {+ _, createErr := ts.Store.CreateAttachment(ctx, &store.Attachment{+ UID: fmt.Sprintf("attachment-overflow-%d", i),+ CreatorID: user.ID,+ Filename: fmt.Sprintf("attachment-overflow-%d.txt", i),+ Type: "text/plain",+ })+ require.NoError(t, createErr)+ }++ first, err := ts.Service.ListAttachments(userCtx, &apipb.ListAttachmentsRequest{PageSize: pageSize})+ require.NoError(t, err)+ require.Len(t, first.Attachments, pageSize)+ require.NotEmpty(t, first.NextPageToken, "a page with a remaining row must advertise a next page")++ second, err := ts.Service.ListAttachments(userCtx, &apipb.ListAttachmentsRequest{+ PageSize: pageSize,+ PageToken: first.NextPageToken,+ })+ require.NoError(t, err)+ require.Len(t, second.Attachments, 1, "the trailing page holds exactly the overflow row")+ require.Empty(t, second.NextPageToken, "the final page must not advertise a next page")+ })+}