From 8754add87495e7795e0d9c3e98ee245ddbd775cd Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 29 Aug 2021 12:45:35 -0500 Subject: [PATCH] add skip to user's comment rest request #1085 --- backend/.golangci.yml | 4 +- backend/app/rest/api/rest_public.go | 15 ++++--- backend/app/rest/api/rest_public_test.go | 52 +++++++++++++++++------- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/backend/.golangci.yml b/backend/.golangci.yml index 8577f376..cedcc08c 100644 --- a/backend/.golangci.yml +++ b/backend/.golangci.yml @@ -36,7 +36,7 @@ linters: enable: - bodyclose - megacheck - - golint + - revive - govet - unconvert - megacheck @@ -76,4 +76,4 @@ issues: exclude-use-default: false service: - golangci-lint-version: 1.39.x + golangci-lint-version: 1.41.x diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index 16ca2527..422f7394 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -227,17 +227,22 @@ func (s *public) commentByIDCtrl(w http.ResponseWriter, r *http.Request) { } } -// GET /comments?site=siteID&user=id - returns comments for given userID +// GET /comments?site=siteID&user=id&limit=123&skip=10 - returns comments for given userID func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) { userID := r.URL.Query().Get("user") siteID := r.URL.Query().Get("site") - limit, err := strconv.Atoi(r.URL.Query().Get("limit")) - if err != nil { - limit = 0 + getNumWithDef := func(key string) int { + res, err := strconv.Atoi(r.URL.Query().Get(key)) + if err != nil { + res = 0 + } + return res } + limit, skip := getNumWithDef("limit"), getNumWithDef("skip") + resp := struct { Comments []store.Comment `json:"comments,omitempty"` Count int `json:"count,omitempty"` @@ -247,7 +252,7 @@ func (s *public) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) { key := cache.NewKey(siteID).ID(URLKeyWithUser(r)).Scopes(userID, siteID) data, err := s.cache.Get(key, func() ([]byte, error) { - comments, e := s.dataService.User(siteID, userID, limit, 0, rest.GetUserOrEmpty(r)) + comments, e := s.dataService.User(siteID, userID, limit, skip, rest.GetUserOrEmpty(r)) if e != nil { return nil, e } diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 0e17575c..50ca62a4 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -397,13 +397,15 @@ func TestRest_FindUserComments(t *testing.T) { c1 := store.Comment{Text: "test test #1", Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah1"}} - c2 := store.Comment{Text: "test test #3", ParentID: "p1", + c2 := store.Comment{Text: "test test #2", ParentID: "p1", Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah2"}} + c3 := store.Comment{Text: "test test #3", ParentID: "p1", + Locator: store.Locator{SiteID: "remark42", URL: "https://radio-t.com/blah3"}} // add 3 comments addComment(t, c1, ts) addComment(t, c2, ts) - addComment(t, c2, ts) + addComment(t, c3, ts) // add one deleted id := addComment(t, c2, ts) @@ -413,22 +415,42 @@ func TestRest_FindUserComments(t *testing.T) { _, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=blah") assert.Equal(t, 400, code, "noting for user blah") - res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev") - assert.Equal(t, 200, code) + { + res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev") + assert.Equal(t, 200, code) - resp := struct { - Comments []store.Comment - Count int - }{} + resp := struct { + Comments []store.Comment + Count int + }{} - err = json.Unmarshal([]byte(res), &resp) - assert.NoError(t, err) - require.Equal(t, 3, len(resp.Comments), "should have 3 comments") - assert.Equal(t, 4, resp.Count, "should have 3 count") + err = json.Unmarshal([]byte(res), &resp) + assert.NoError(t, err) + require.Equal(t, 3, len(resp.Comments), "should have 3 comments") + assert.Equal(t, 4, resp.Count, "should have 3+1 count") // TODO: fix as we start to skip deleted - // user comment sorted with -time - assert.True(t, resp.Comments[0].Timestamp.After(resp.Comments[1].Timestamp)) - assert.True(t, resp.Comments[1].Timestamp.After(resp.Comments[2].Timestamp)) + // user comment sorted with -time + assert.True(t, resp.Comments[0].Timestamp.After(resp.Comments[1].Timestamp)) + assert.True(t, resp.Comments[1].Timestamp.After(resp.Comments[2].Timestamp)) + } + + { + res, code := get(t, ts.URL+"/api/v1/comments?site=remark42&user=dev&skip=1&limit=2") + assert.Equal(t, 200, code) + + resp := struct { + Comments []store.Comment + Count int + }{} + + err = json.Unmarshal([]byte(res), &resp) + assert.NoError(t, err) + require.Equal(t, 2, len(resp.Comments), "should have 2 comments due to the limit") + assert.Equal(t, 4, resp.Count, "should have 4 count") + + assert.Equal(t, "https://radio-t.com/blah3", resp.Comments[0].Locator.URL) + assert.Equal(t, "https://radio-t.com/blah2", resp.Comments[1].Locator.URL) + } } func TestRest_UserInfo(t *testing.T) {