add name to blocked response
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
+15
-3
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user