Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VFS pagination on root dir (skipping properly the trash) #1134

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions web/files/paginated.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,63 @@ func newDir(doc *vfs.DirDoc) *dir {
return &dir{doc: doc}
}

func dirData(c echo.Context, statusCode int, doc *vfs.DirDoc) error {
func getDirData(c echo.Context, doc *vfs.DirDoc) (int, couchdb.Cursor, []vfs.DirOrFileDoc, error) {
instance := middlewares.GetInstance(c)
fs := instance.VFS()

cursor, err := jsonapi.ExtractPaginationCursor(c, defPerPage)
if err != nil {
return err
return 0, nil, nil, err
}

count, err := fs.DirLength(doc)
if err != nil {
return err
return 0, nil, nil, err
}
// Do not count the trash folder when listing the root directory.
if count > 0 && doc.ID() == consts.RootDirID {
count--

// Hide the trash folder when listing the root directory.
var limit int
if doc.ID() == consts.RootDirID {
if count > 0 {
count--
}
switch c := cursor.(type) {
case *couchdb.StartKeyCursor:
limit = c.Limit
if c.NextKey == nil {
c.Limit++
}
case *couchdb.SkipCursor:
limit = c.Limit
if c.Skip == 0 {
c.Limit++
} else {
c.Skip++
}
}
}

children, err := fs.DirBatch(doc, cursor)
if err != nil {
return 0, nil, nil, err
}

if doc.ID() == consts.RootDirID {
switch c := cursor.(type) {
case *couchdb.StartKeyCursor:
c.Limit = limit
case *couchdb.SkipCursor:
c.Limit = limit
c.Skip--
}
}

return count, cursor, children, nil
}

func dirData(c echo.Context, statusCode int, doc *vfs.DirDoc) error {
instance := middlewares.GetInstance(c)
count, cursor, children, err := getDirData(c, doc)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,6 +149,7 @@ func dirData(c echo.Context, statusCode int, doc *vfs.DirDoc) error {
}
next := "/files/" + doc.DocID + "/relationships/contents?" + params.Encode()
rel["contents"].Links.Next = next
links.Next = "/files/" + doc.DocID + "?" + params.Encode()
}

d := &dir{
Expand All @@ -123,24 +162,8 @@ func dirData(c echo.Context, statusCode int, doc *vfs.DirDoc) error {
}

func dirDataList(c echo.Context, statusCode int, doc *vfs.DirDoc) error {
cursor, err := jsonapi.ExtractPaginationCursor(c, defPerPage)
if err != nil {
return err
}

instance := middlewares.GetInstance(c)
fs := instance.VFS()

count, err := fs.DirLength(doc)
if err != nil {
return err
}
// Do not count the trash folder when listing the root directory.
if count > 0 && doc.ID() == consts.RootDirID {
count--
}

children, err := fs.DirBatch(doc, cursor)
count, cursor, children, err := getDirData(c, doc)
if err != nil {
return err
}
Expand Down
76 changes: 76 additions & 0 deletions web/files/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"testing"

"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/web/jsonapi"
"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +22,81 @@ func getJSON(t *testing.T, url string, out interface{}) error {

}

func TestTrashIsSkipped(t *testing.T) {
nb := 15
body := "foo"
for i := 0; i < nb; i++ {
name := "foo" + strconv.Itoa(i)
upload(t, "/files/io.cozy.files.root-dir?Type=file&Name="+name, "text/plain", body, "rL0Y20zC+Fzt72VPzMSk2A==")
}

var opts = &url.Values{}
opts.Add("page[limit]", "5")
var result struct {
Data struct {
Relationships struct {
Contents struct {
Links *jsonapi.LinksList
Data []couchdb.DocReference
}
}
}
Included []interface{}
Links *jsonapi.LinksList
}

ids := []string{}

getJSON(t, "/files/io.cozy.files.root-dir?"+opts.Encode(), &result)
assert.Len(t, result.Data.Relationships.Contents.Data, 5)
assert.Len(t, result.Included, 5)

for i, ref := range result.Data.Relationships.Contents.Data {
id := result.Included[i].(map[string]interface{})["id"].(string)
assert.Equal(t, id, ref.ID)
assert.NotEqual(t, id, consts.TrashDirID)
for _, seen := range ids {
assert.NotEqual(t, id, seen)
}
ids = append(ids, id)
}

next := result.Links.Next
assert.NotEmpty(t, next)

getJSON(t, next, &result)
assert.Len(t, result.Data.Relationships.Contents.Data, 5)
assert.Len(t, result.Included, 5)

for i, ref := range result.Data.Relationships.Contents.Data {
id := result.Included[i].(map[string]interface{})["id"].(string)
assert.Equal(t, id, ref.ID)
assert.NotEqual(t, id, consts.TrashDirID)
for _, seen := range ids {
assert.NotEqual(t, id, seen)
}
ids = append(ids, id)
}

next = result.Links.Next
assert.NotEmpty(t, next)

opts.Add("page[skip]", "10")
getJSON(t, "/files/io.cozy.files.root-dir?"+opts.Encode(), &result)
assert.Len(t, result.Data.Relationships.Contents.Data, 5)
assert.Len(t, result.Included, 5)

for i, ref := range result.Data.Relationships.Contents.Data {
id := result.Included[i].(map[string]interface{})["id"].(string)
assert.Equal(t, id, ref.ID)
assert.NotEqual(t, id, consts.TrashDirID)
for _, seen := range ids {
assert.NotEqual(t, id, seen)
}
ids = append(ids, id)
}
}

func TestZeroCountIsPresent(t *testing.T) {
_, dirdata := createDir(t, "/files/?Type=directory&Name=emptydirectory")
dirdata, ok := dirdata["data"].(map[string]interface{})
Expand Down
12 changes: 5 additions & 7 deletions web/jsonapi/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,19 @@ func PaginationCursorToParams(cursor couchdb.Cursor) (url.Values, error) {
return v, nil
}

switch cursor.(type) {
switch c := cursor.(type) {
case *couchdb.StartKeyCursor:
skc := cursor.(*couchdb.StartKeyCursor)
cursorObj := []interface{}{skc.NextKey, skc.NextDocID}
cursorObj := []interface{}{c.NextKey, c.NextDocID}
cursorBytes, err := json.Marshal(cursorObj)
if err != nil {
return nil, err
}
v.Set("page[limit]", strconv.Itoa(skc.Limit))
v.Set("page[limit]", strconv.Itoa(c.Limit))
v.Set("page[cursor]", string(cursorBytes))

case *couchdb.SkipCursor:
sc := cursor.(*couchdb.SkipCursor)
v.Set("page[limit]", strconv.Itoa(sc.Limit))
v.Set("page[skip]", strconv.Itoa(sc.Skip))
v.Set("page[limit]", strconv.Itoa(c.Limit))
v.Set("page[skip]", strconv.Itoa(c.Skip))
}

return v, nil
Expand Down