fix tree count with deleted

This commit is contained in:
Umputun
2018-06-03 13:29:12 -05:00
parent 1f47f51806
commit b6ae956c79
2 changed files with 9 additions and 8 deletions
+6 -5
View File
@@ -41,13 +41,14 @@ func MakeTree(comments []store.Comment, sortType string, readOnlyAge int) *Tree
res := Tree{
Info: store.PostInfo{
URL: comments[0].Locator.URL,
Count: len(comments), // TODO: includes deleted?
FirstTS: comments[0].Timestamp,
LastTS: comments[0].Timestamp,
},
}
res.Info.Count = len(res.filter(comments, func(c store.Comment) bool { return !c.Deleted }))
topComments := res.filter(comments, func(c store.Comment) bool { return c.ParentID == "" })
topComments := res.filter(comments, "")
res.Nodes = []*Node{}
for _, rootComment := range topComments {
node := Node{Comment: rootComment}
@@ -84,7 +85,7 @@ func (t *Tree) proc(comments []store.Comment, node *Node, rd *recurData, parentI
rd.tsModified, rd.tsCreated = node.Comment.Timestamp, node.Comment.Timestamp
}
repComments := t.filter(comments, parentID)
repComments := t.filter(comments, func(comment store.Comment) bool { return comment.ParentID == parentID })
for _, rc := range repComments {
if rc.Timestamp.After(rd.tsModified) {
rd.tsModified = rc.Timestamp
@@ -107,10 +108,10 @@ func (t *Tree) proc(comments []store.Comment, node *Node, rd *recurData, parentI
}
// filter returns comments for parentID
func (t *Tree) filter(comments []store.Comment, parentID string) (f []store.Comment) {
func (t *Tree) filter(comments []store.Comment, fn func(comment store.Comment) bool) (f []store.Comment) {
for _, c := range comments {
if c.ParentID == parentID {
if fn(c) {
f = append(f, c)
}
}
+3 -3
View File
@@ -49,13 +49,13 @@ func TestMakeTree(t *testing.T) {
assert.Nil(t, err)
expected, actual := cleanFormatting(expJSON, buf.String())
assert.Equal(t, expected, actual)
assert.Equal(t, store.PostInfo{URL: "url", Count: 17, FirstTS: ts(46, 1), LastTS: ts(47, 22)}, res.Info)
assert.Equal(t, store.PostInfo{URL: "url", Count: 12, FirstTS: ts(46, 1), LastTS: ts(47, 22)}, res.Info)
res = MakeTree([]store.Comment{}, "time", 0)
assert.Equal(t, &Tree{}, res)
res = MakeTree(comments, "time", 10)
assert.Equal(t, store.PostInfo{URL: "url", Count: 17, FirstTS: ts(46, 1), LastTS: ts(47, 22), ReadOnly: true}, res.Info)
assert.Equal(t, store.PostInfo{URL: "url", Count: 12, FirstTS: ts(46, 1), LastTS: ts(47, 22), ReadOnly: true}, res.Info)
}
func TestTreeSortNodes(t *testing.T) {
@@ -376,7 +376,7 @@ const expJSON = `{
],
"info": {
"url": "url",
"count": 17,
"count": 12,
"first_time": "2017-12-25T19:46:01Z",
"last_time": "2017-12-25T19:47:22Z"
}