From decf9c177b7f7462f78f860e2d6c4135a5b450d9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 8 Jan 2018 19:35:42 -0600 Subject: [PATCH] add counts to list --- app/rest/server.go | 1 - app/store/bolt.go | 16 ++++++++++++++-- app/store/bolt_test.go | 2 +- app/store/store.go | 8 +++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/rest/server.go b/app/rest/server.go index 1aed94c6..225bad02 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -96,7 +96,6 @@ func (s *Server) Run() { s.mod = admin{dataService: s.DataService, exporter: s.Exporter, respCache: s.respCache} rauth.Mount("/admin", s.mod.routes()) }) - }) // add robots and file server for static content from /web diff --git a/app/store/bolt.go b/app/store/bolt.go index 34404d71..6a3f0978 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -350,21 +350,33 @@ func (b *BoltDB) IsBlocked(siteID string, userID string) (blocked bool) { } // List returns list of buckets, which is list of all commented posts -func (b BoltDB) List(siteID string) (list []string, err error) { +func (b BoltDB) List(siteID string) (list []PostInfo, err error) { bdb, err := b.db(siteID) if err != nil { return nil, err } + posts := []string{} err = bdb.View(func(tx *bolt.Tx) error { return tx.ForEach(func(name []byte, _ *bolt.Bucket) error { if string(name) != lastBucketName && string(name) != userBucketName { - list = append(list, string(name)) + posts = append(posts, string(name)) } return nil }) }) + if err != nil { + return nil, errors.Wrapf(err, "failed to get list of posts for %s", siteID) + } + + for _, post := range posts { + count, err := b.Count(Locator{SiteID: siteID, URL: post}) + if err != nil { + return nil, errors.Wrapf(err, "failed to get count of comments for posts for %s", post) + } + list = append(list, PostInfo{URL: post, Count: count}) + } return list, err } diff --git a/app/store/bolt_test.go b/app/store/bolt_test.go index 1048eba1..d32939a9 100644 --- a/app/store/bolt_test.go +++ b/app/store/bolt_test.go @@ -116,7 +116,7 @@ func TestBoltDB_List(t *testing.T) { res, err := b.List("radio-t") assert.Nil(t, err) - assert.Equal(t, []string{"https://radio-t.com", "https://radio-t.com/2"}, res) + assert.Equal(t, []PostInfo{{URL: "https://radio-t.com", Count: 2}, {URL: "https://radio-t.com/2", Count: 1}}, res) } func TestBoltDB_GetForUser(t *testing.T) { diff --git a/app/store/store.go b/app/store/store.go index 52e4eacc..7f622381 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -51,6 +51,12 @@ type Edit struct { Summary string `json:"summary"` } +// PostInfo holds summary for given post url +type PostInfo struct { + URL string `json:"url"` + Count int `json:"count"` +} + // Interface combines all store interfaces type Interface interface { Accessor @@ -67,7 +73,7 @@ type Accessor interface { GetByID(siteID string, commentID string) (Comment, error) // comment by id GetByUser(siteID string, userID string) ([]Comment, error) // comment by user Count(locator Locator) (int, error) // number of comments for the post - List(siteID string) ([]string, error) // list of commented posts + List(siteID string) ([]PostInfo, error) // list of commented posts } // Admin defines all store ops avail for admin only