From 64bfdec8b7c2a51745bb13122d4fb6665d7385ea Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 16 Jun 2018 14:05:47 -0500 Subject: [PATCH] add post info to plain response #55 --- app/rest/api/admin_test.go | 54 ++++++++++++++++---------------- app/rest/api/rest.go | 5 +++ app/rest/api/rest_public.go | 6 +++- app/rest/api/rest_public_test.go | 18 ++++++----- 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/app/rest/api/admin_test.go b/app/rest/api/admin_test.go index cffa1a32..b9ed1d33 100644 --- a/app/rest/api/admin_test.go +++ b/app/rest/api/admin_test.go @@ -79,27 +79,27 @@ func TestAdmin_DeleteUser(t *testing.T) { // all 3 comments here, but for id2 they deleted res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah&sort=+time") assert.Equal(t, 200, code) - comments := []store.Comment{} - err = json.Unmarshal([]byte(res), &comments) + commentsWithInfo := commentsWithInfo{} + err = json.Unmarshal([]byte(res), &commentsWithInfo) assert.Nil(t, err) - assert.Equal(t, 3, len(comments), "should have 3 comment") + assert.Equal(t, 3, len(commentsWithInfo.Comments), "should have 3 comment") // id1 comment untouched - assert.Equal(t, id1, comments[0].ID) - assert.Equal(t, "o test test #1", comments[0].Orig) - assert.False(t, comments[0].Deleted) - t.Logf("%+v", comments[0].User) + assert.Equal(t, id1, commentsWithInfo.Comments[0].ID) + assert.Equal(t, "o test test #1", commentsWithInfo.Comments[0].Orig) + assert.False(t, commentsWithInfo.Comments[0].Deleted) + t.Logf("%+v", commentsWithInfo.Comments[0].User) // id2 comments fully deleted - assert.Equal(t, "", comments[1].Text) - assert.Equal(t, "", comments[1].Orig) - assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[1].User) - assert.True(t, comments[1].Deleted) + assert.Equal(t, "", commentsWithInfo.Comments[1].Text) + assert.Equal(t, "", commentsWithInfo.Comments[1].Orig) + assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, commentsWithInfo.Comments[1].User) + assert.True(t, commentsWithInfo.Comments[1].Deleted) - assert.Equal(t, "", comments[2].Text) - assert.Equal(t, "", comments[2].Orig) - assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, comments[1].User) - assert.True(t, comments[2].Deleted) + assert.Equal(t, "", commentsWithInfo.Comments[2].Text) + assert.Equal(t, "", commentsWithInfo.Comments[2].Orig) + assert.Equal(t, store.User{Name: "deleted", ID: "deleted", Picture: "", Admin: false, Blocked: false, IP: ""}, commentsWithInfo.Comments[1].User) + assert.True(t, commentsWithInfo.Comments[2].Deleted) } func TestAdmin_Pin(t *testing.T) { @@ -186,12 +186,12 @@ func TestAdmin_Block(t *testing.T) { res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah&sort=+time") assert.Equal(t, 200, code) - comments := []store.Comment{} + comments := commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") - assert.Equal(t, "", comments[0].Text) - assert.True(t, comments[0].Deleted) + assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + assert.Equal(t, "", comments.Comments[0].Text) + assert.True(t, comments.Comments[0].Deleted) code, body = block(-1) require.Equal(t, 200, code) @@ -351,12 +351,12 @@ func TestAdmin_Verify(t *testing.T) { res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah&sort=+time") assert.Equal(t, 200, code) - comments := []store.Comment{} + comments := commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") - assert.Equal(t, "test test #1", comments[0].Text) - assert.True(t, comments[0].User.Verified) + assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + assert.Equal(t, "test test #1", comments.Comments[0].Text) + assert.True(t, comments.Comments[0].User.Verified) req, err = http.NewRequest(http.MethodPut, fmt.Sprintf("%s/api/v1/admin/verify/user1?site=radio-t&verified=0", ts.URL), nil) @@ -369,12 +369,12 @@ func TestAdmin_Verify(t *testing.T) { res, code = get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah&sort=+time") assert.Equal(t, 200, code) - comments = []store.Comment{} + comments = commentsWithInfo{} err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") - assert.Equal(t, "test test #1", comments[0].Text) - assert.False(t, comments[0].User.Verified) + assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + assert.Equal(t, "test test #1", comments.Comments[0].Text) + assert.False(t, comments.Comments[0].User.Verified) } diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 6be7ceda..cfef028b 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -57,6 +57,11 @@ var mdExt = blackfriday.NoIntraEmphasis | blackfriday.Tables | blackfriday.Fence blackfriday.Strikethrough | blackfriday.SpaceHeadings | blackfriday.HardLineBreak | blackfriday.BackslashLineBreak | blackfriday.Autolink +type commentsWithInfo struct { + Comments []store.Comment `json:"comments"` + Info store.PostInfo `json:"info,omitempty"` +} + // Run the lister and request's router, activate rest server func (s *Rest) Run(port int) { log.Printf("[INFO] activate rest server on port %d", port) diff --git a/app/rest/api/rest_public.go b/app/rest/api/rest_public.go index 1eecdcef..17800afa 100644 --- a/app/rest/api/rest_public.go +++ b/app/rest/api/rest_public.go @@ -42,7 +42,11 @@ func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { } b, e = encodeJSONWithHTML(tree) default: - b, e = encodeJSONWithHTML(maskedComments) + withInfo := commentsWithInfo{Comments: maskedComments} + if info, ee := s.DataService.Info(locator, s.ReadOnlyAge); ee == nil { + withInfo.Info = info + } + b, e = encodeJSONWithHTML(withInfo) } return b, e }) diff --git a/app/rest/api/rest_public_test.go b/app/rest/api/rest_public_test.go index 3e68c712..1763df9c 100644 --- a/app/rest/api/rest_public_test.go +++ b/app/rest/api/rest_public_test.go @@ -88,21 +88,25 @@ func TestRest_Find(t *testing.T) { // get sorted by +time res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time") assert.Equal(t, 200, code) - comments := []store.Comment{} + comments := commentsWithInfo{} err := json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") - assert.Equal(t, id1, comments[0].ID) - assert.Equal(t, id2, comments[1].ID) + 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, "https://radio-t.com/blah1", comments.Info.URL) + assert.Equal(t, 2, comments.Info.Count) + assert.Equal(t, false, comments.Info.ReadOnly) + assert.True(t, comments.Info.FirstTS.Before(comments.Info.LastTS)) // get sorted by -time res, code = get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=-time") assert.Equal(t, 200, code) err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) - assert.Equal(t, 2, len(comments), "should have 2 comments") - assert.Equal(t, id1, comments[1].ID) - assert.Equal(t, id2, comments[0].ID) + assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") + assert.Equal(t, id1, comments.Comments[1].ID) + assert.Equal(t, id2, comments.Comments[0].ID) // get in tree mode tree := rest.Tree{}