From bccf716a8dd039c0eb23e2de6c747cfe269d97bc Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 31 May 2019 16:09:40 -0500 Subject: [PATCH] support view=user #323 --- backend/app/rest/api/rest_public.go | 20 ++++++++++++- backend/app/rest/api/rest_public_test.go | 38 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index c24e3e8a..3200ca07 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -48,7 +48,7 @@ type pubStore interface { Counts(siteID string, postIDs []string) ([]store.PostInfo, error) } -// GET /find?site=siteID&url=post-url&format=[tree|plain]&sort=[+/-time|+/-score|+/-controversy ] +// GET /find?site=siteID&url=post-url&format=[tree|plain]&sort=[+/-time|+/-score|+/-controversy ]&view=[user|all] // find comments for given post. Returns in tree or plain formats, sorted func (s *public) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { locator := store.Locator{SiteID: r.URL.Query().Get("site"), URL: r.URL.Query().Get("url")} @@ -56,6 +56,8 @@ func (s *public) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(sort, " ") { // restore + replaced by " " sort = "+" + sort[1:] } + + view := r.URL.Query().Get("view") log.Printf("[DEBUG] get comments for %+v, sort %s, format %s", locator, sort, r.URL.Query().Get("format")) key := cache.NewKey(locator.SiteID).ID(URLKeyWithUser(r)).Scopes(locator.SiteID, locator.URL) @@ -64,6 +66,7 @@ func (s *public) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { if e != nil { comments = []store.Comment{} // error should clear comments and continue for post info } + comments = s.applyView(comments, view) var b []byte switch r.URL.Query().Get("format") { case "tree": @@ -392,3 +395,18 @@ func (s *public) robotsCtrl(w http.ResponseWriter, r *http.Request) { } render.PlainText(w, r, "User-agent: *\nDisallow: /auth/\nDisallow: /api/\n"+strings.Join(allowed, "\n")+"\n") } + +func (s *public) applyView(comments []store.Comment, view string) []store.Comment { + if strings.EqualFold(view, "user") { + projection := make([]store.Comment, len(comments)) + for i, c := range comments { + p := store.Comment{ + ID: c.ID, + User: c.User, + } + projection[i] = p + } + return projection + } + return comments +} diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 91a54313..b0f23ce7 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -96,6 +96,8 @@ func TestRest_Find(t *testing.T) { assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, id1, comments.Comments[0].ID) assert.Equal(t, id2, comments.Comments[1].ID) + assert.Equal(t, "

test test #1

\n", comments.Comments[0].Text) + assert.Equal(t, "

test test #2

\n", comments.Comments[1].Text) assert.Equal(t, "https://radio-t.com/blah1", comments.Info.URL) assert.Equal(t, 2, comments.Info.Count) assert.Equal(t, false, comments.Info.ReadOnly) @@ -195,6 +197,42 @@ func TestRest_FindReadOnly(t *testing.T) { assert.False(t, tree.Info.ReadOnly, "post is writable") } +func TestRest_FindUserView(t *testing.T) { + ts, _, teardown := startupT(t) + defer teardown() + + res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&view=user") + assert.Equal(t, 200, code) + comments := commentsWithInfo{} + err := json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 0, len(comments.Comments), "should have 0 comments") + + c1 := store.Comment{Text: "test test #1", ParentID: "", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} + id1 := addComment(t, c1, ts) + + c2 := store.Comment{Text: "test test #2", ParentID: id1, + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} + id2 := addComment(t, c2, ts) + + assert.NotEqual(t, id1, id2) + + // get sorted by +time with view=user + res, code = get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time&view=user") + assert.Equal(t, 200, code) + comments = commentsWithInfo{} + err = json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + assert.Equal(t, id1, comments.Comments[0].ID) + assert.Equal(t, id2, comments.Comments[1].ID) + assert.Equal(t, "dev", comments.Comments[0].User.ID) + assert.Equal(t, "dev", comments.Comments[1].User.ID) + assert.Equal(t, "", comments.Comments[0].Text) + assert.Equal(t, "", comments.Comments[1].Text) +} + func TestRest_Last(t *testing.T) { ts, srv, teardown := startupT(t) defer teardown()