From ad46b3f72fa5c3c722094b9ef045c55a040d13df Mon Sep 17 00:00:00 2001 From: Umputun Date: Sun, 18 Mar 2018 18:47:32 -0500 Subject: [PATCH] add name to blocked response --- app/rest/api/rest.go | 2 +- app/store/bolt.go | 18 +++++++++++++++--- app/store/bolt_test.go | 9 ++++++++- app/store/comment.go | 1 + app/store/store.go | 16 ++++++++-------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 38d22439..2d45810b 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -297,7 +297,7 @@ func (s *Rest) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) { log.Printf("[DEBUG] get comments for userID %s, %s", userID, siteID) data, err := s.Cache.Get(rest.URLKey(r), time.Hour, func() ([]byte, error) { - comments, count, e := s.DataService.User(siteID, userID) + comments, count, e := s.DataService.User(siteID, userID, 0) if e != nil { return nil, e } diff --git a/app/store/bolt.go b/app/store/bolt.go index 4f4557ab..5d0f9c81 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -337,7 +337,14 @@ func (b *BoltDB) Blocked(siteID string) (users []BlockedUser, err error) { if e != nil { return errors.Wrap(e, "can't parse block ts") } - users = append(users, BlockedUser{ID: string(k), Timestamp: ts}) + + // get user name from comment user section + userName := "" + if userComments, _, e := b.User(siteID, string(k), 1); e == nil && len(userComments) > 0 { + userName = userComments[0].User.Name + } + + users = append(users, BlockedUser{ID: string(k), Name: userName, Timestamp: ts}) return nil }) }) @@ -382,7 +389,7 @@ func (b BoltDB) List(siteID string, limit, skip int) (list []PostInfo, err error // User extracts all comments for given site and given userID // "users" bucket has sub-bucket for each userID, and keeps it as ts:ref -func (b *BoltDB) User(siteID string, userID string) (comments []Comment, totalComments int, err error) { +func (b *BoltDB) User(siteID string, userID string, limit int) (comments []Comment, totalComments int, err error) { comments = []Comment{} commentRefs := []string{} @@ -391,6 +398,11 @@ func (b *BoltDB) User(siteID string, userID string) (comments []Comment, totalCo if err != nil { return nil, 0, err } + + if limit == 0 || limit > userLimit { + limit = userLimit + } + // get list of references to comments err = bdb.View(func(tx *bolt.Tx) error { usersBkt := tx.Bucket([]byte(userBucketName)) @@ -403,7 +415,7 @@ func (b *BoltDB) User(siteID string, userID string) (comments []Comment, totalCo totalComments = 0 for k, v := c.Last(); k != nil; k, v = c.Prev() { totalComments++ - if len(commentRefs) <= userLimit { + if len(commentRefs) < limit { commentRefs = append(commentRefs, string(v)) } } diff --git a/app/store/bolt_test.go b/app/store/bolt_test.go index 95db7822..5e5f9dfe 100644 --- a/app/store/bolt_test.go +++ b/app/store/bolt_test.go @@ -175,11 +175,18 @@ func TestBoltDB_GetForUser(t *testing.T) { defer os.Remove(testDb) b := prep(t) - res, count, err := b.User("radio-t", "user1") + res, count, err := b.User("radio-t", "user1", 5) assert.Nil(t, err) assert.Equal(t, 2, len(res)) assert.Equal(t, 2, count) assert.Equal(t, "some text2", res[0].Text, "sorted by -time") + + res, count, err = b.User("radio-t", "user1", 1) + assert.Nil(t, err) + assert.Equal(t, 1, len(res), "allow 1 comment") + assert.Equal(t, 2, count) + assert.Equal(t, "some text2", res[0].Text, "sorted by -time") + } // makes new boltdb, put two records diff --git a/app/store/comment.go b/app/store/comment.go index 4133e80e..1c030229 100644 --- a/app/store/comment.go +++ b/app/store/comment.go @@ -55,6 +55,7 @@ type PostInfo struct { // BlockedUser holds id and ts for blocked user type BlockedUser struct { ID string `json:"id"` + Name string `json:"name"` Timestamp time.Time `json:"time"` } diff --git a/app/store/store.go b/app/store/store.go index 03b95621..2bb8f365 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -15,14 +15,14 @@ type Interface interface { // Accessor defines all usual access ops avail for regular user type Accessor interface { - Create(comment Comment) (commentID string, err error) // create new comment, avoid dups by id - Get(locator Locator, commentID string) (comment Comment, err error) // get comment by id - Put(locator Locator, comment Comment) error // update comment, mutable parts only - Find(locator Locator, sort string) ([]Comment, error) // find comments for locator - Last(siteID string, max int) ([]Comment, error) // last comments for given site, sorted by time - User(siteID string, userID string) ([]Comment, int, error) // comments by user, sorted by time - Count(locator Locator) (int, error) // number of comments for the post - List(siteID string, limit int, skip int) ([]PostInfo, error) // list of commented posts + Create(comment Comment) (commentID string, err error) // create new comment, avoid dups by id + Get(locator Locator, commentID string) (comment Comment, err error) // get comment by id + Put(locator Locator, comment Comment) error // update comment, mutable parts only + Find(locator Locator, sort string) ([]Comment, error) // find comments for locator + Last(siteID string, limit int) ([]Comment, error) // last comments for given site, sorted by time + User(siteID string, userID string, limit int) ([]Comment, int, error) // comments by user, sorted by time + Count(locator Locator) (int, error) // number of comments for the post + List(siteID string, limit int, skip int) ([]PostInfo, error) // list of commented posts } // Admin defines all store ops avail for admin only