diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index 3713ef53..3657bacd 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -1,7 +1,7 @@ package api import ( - "crypto/sha1" //nolint + "crypto/sha1" // nolint "encoding/base64" "io" "net/http" @@ -275,12 +275,9 @@ func (s *Rest) countMultiCtrl(w http.ResponseWriter, r *http.Request) { // key could be long for multiple posts, make it sha1 k := URLKey(r) + strings.Join(posts, ",") - hasher := sha1.New() //nolint - if _, err := hasher.Write([]byte(k)); err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "can't make sha1 for list of urls", rest.ErrInternal) - return - } - sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil)) + h := sha1.Sum([]byte(k)) //nolint + sha := base64.URLEncoding.EncodeToString(h[:]) + key := cache.NewKey(siteID).ID(sha).Scopes(siteID) data, err := s.Cache.Get(key, func() ([]byte, error) { counts, e := s.DataService.Counts(siteID, posts) diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 788f34e2..e3343a8f 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -377,6 +377,37 @@ func TestRest_List(t *testing.T) { assert.Equal(t, 3, pi[1].Count) } +func TestRest_ListWithSkipAndLimit(t *testing.T) { + ts, _, teardown := startupT(t) + defer teardown() + + c1 := store.Comment{Text: "test test #1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} + c2 := store.Comment{Text: "test test #2", ParentID: "p1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah2"}} + c3 := store.Comment{Text: "test test #3", ParentID: "p1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah3"}} + + addComment(t, c1, ts) + addComment(t, c1, ts) + addComment(t, c1, ts) + addComment(t, c2, ts) + addComment(t, c2, ts) + addComment(t, c3, ts) + addComment(t, c3, ts) + + body, code := get(t, ts.URL+"/api/v1/list?site=radio-t&skip=1&limit=2") + assert.Equal(t, 200, code) + pi := []store.PostInfo{} + err := json.Unmarshal([]byte(body), &pi) + assert.Nil(t, err) + require.Equal(t, 2, len(pi)) + assert.Equal(t, "https://radio-t.com/blah2", pi[0].URL) + assert.Equal(t, 2, pi[0].Count) + assert.Equal(t, "https://radio-t.com/blah1", pi[1].URL) + assert.Equal(t, 3, pi[1].Count) +} + func TestRest_Config(t *testing.T) { ts, _, teardown := startupT(t) defer teardown()