From f448af5e0c7a970c3b8e17b21a65281daaa46b66 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 5 Feb 2018 11:22:23 -0600 Subject: [PATCH] block list with ts --- app/store/bolt.go | 12 ++++++++---- app/store/bolt_test.go | 6 +++++- app/store/comment.go | 6 ++++++ app/store/store.go | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/store/bolt.go b/app/store/bolt.go index 3bf971a1..f950b2e4 100644 --- a/app/store/bolt.go +++ b/app/store/bolt.go @@ -312,8 +312,8 @@ func (b *BoltDB) IsBlocked(siteID string, userID string) (blocked bool) { } // Blocked get lists of blocked users for given site -func (b *BoltDB) Blocked(siteID string) (userIDs []string, err error) { - userIDs = []string{} +func (b *BoltDB) Blocked(siteID string) (users []BlockedUser, err error) { + users = []BlockedUser{} bdb, err := b.db(siteID) if err != nil { return nil, err @@ -325,12 +325,16 @@ func (b *BoltDB) Blocked(siteID string) (userIDs []string, err error) { return nil } return bucket.ForEach(func(k []byte, v []byte) error { - userIDs = append(userIDs, string(k)) + ts, err := time.ParseInLocation(time.RFC3339Nano, string(v), time.Local) + if err != nil { + return errors.Wrap(err, "can't parse block ts") + } + users = append(users, BlockedUser{ID: string(k), Timestamp: ts}) return nil }) }) - return userIDs, err + return users, err } // List returns list of buckets, which is list of all commented posts diff --git a/app/store/bolt_test.go b/app/store/bolt_test.go index aac447a8..de17618e 100644 --- a/app/store/bolt_test.go +++ b/app/store/bolt_test.go @@ -139,7 +139,11 @@ func TestBoltDB_BlockList(t *testing.T) { ids, err := b.Blocked("radio-t") assert.NoError(t, err) - assert.Equal(t, []string{"user1", "user2"}, ids) + + assert.Equal(t, 2, len(ids)) + assert.Equal(t, "user1", ids[0].ID) + assert.Equal(t, "user2", ids[1].ID) + t.Logf("%+v", ids) } func TestBoltDB_List(t *testing.T) { diff --git a/app/store/comment.go b/app/store/comment.go index ea29cca7..7c34d945 100644 --- a/app/store/comment.go +++ b/app/store/comment.go @@ -56,6 +56,12 @@ type PostInfo struct { Count int `json:"count"` } +// BlockedUser holds id and ts for blocked user +type BlockedUser struct { + ID string `json:"id"` + Timestamp time.Time `json:"time"` +} + // GenID generates sha1(random) string func (c *Comment) GenID() error { b := make([]byte, 64) diff --git a/app/store/store.go b/app/store/store.go index 96e49dca..551ae62e 100644 --- a/app/store/store.go +++ b/app/store/store.go @@ -30,7 +30,7 @@ type Admin interface { Delete(locator Locator, commentID string) error // delete comment by id SetBlock(siteID string, userID string, status bool) error // block or unblock user IsBlocked(siteID string, userID string) bool // check if user blocked - Blocked(siteID string) (userIDs []string, err error) // get list of blocked users + Blocked(siteID string) ([]BlockedUser, error) // get list of blocked users } func sortComments(comments []Comment, sortFld string) []Comment {